description: You are given n segments on a number line, numbered from 1 to n. The i-th segments covers all integer points from l_i to r_i and has a value w_i.
  
  You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two integer points if there exists a selected segment that covers both of them. A subset is good if it's possible to reach point m starting from point 1 in arbitrary number of moves.
  
  The cost of the subset is the difference between the maximum and the minimum values of segments in it. Find the minimum cost of a good subset.
  
  In every test there exists at least one good subset.
  
  Input
  
  The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5; 2 ≤ m ≤ 10^6) — the number of segments and the number of integer points.
  
  Each of the next n lines contains three integers l_i, r_i and w_i (1 ≤ l_i < r_i ≤ m; 1 ≤ w_i ≤ 10^6) — the description of the i-th segment.
  
  In every test there exists at least one good subset.
  
  Output
  
  Print a single integer — the minimum cost of a good subset.
  
  Examples
  
  Input
  
  
  5 12
  1 5 5
  3 4 10
  4 10 6
  11 12 5
  10 12 3
  
  
  Output
  
  
  3
  
  
  Input
  
  
  1 10
  1 10 23
  
  
  Output
  
  
  0
solution: #include <bits/stdc++.h>
  using namespace std;
  const int MOD = 1e9 + 7;
  const int inf = 2147483647;
  const int maxN = 3e5 + 2;
  const int offset = (1 << 21);
  int n, m, l[maxN], r[maxN], w[maxN];
  int st[offset], lazy[offset];
  int ans = inf, sum;
  void prop(int x, bool state) {
    if (lazy[x]) st[x] += lazy[x];
    if (!state) {
      lazy[x * 2 + 1] += lazy[x];
      lazy[x * 2 + 2] += lazy[x];
    }
    lazy[x] = 0;
  }
  void upd(int x, int l, int r, int ql, int qr, int val) {
    prop(x, l == r);
    if (ql <= l && r <= qr) {
      st[x] += val;
      if (l != r) {
        lazy[x * 2 + 1] += val;
        lazy[x * 2 + 2] += val;
      }
      return;
    }
    if (ql > r || qr < l) return;
    int mid = (l + r) >> 1;
    upd(x * 2 + 1, l, mid, ql, qr, val);
    upd(x * 2 + 2, mid + 1, r, ql, qr, val);
    st[x] = min(st[x * 2 + 1], st[x * 2 + 2]);
  }
  struct Seg {
    int l;
    int r;
    int w;
    inline void load() {
      int a, b, c;
      cin >> a >> b >> c;
      l = a - 1;
      r = b - 2;
      w = c;
    }
  } a[maxN];
  bool cmp(Seg x, Seg y) { return x.w < y.w; }
  int main() {
    ios_base::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
      a[i].load();
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; ++i) {
      l[i] = a[i].l;
      r[i] = a[i].r;
      w[i] = a[i].w;
    }
    int p1 = 0, p2 = 0;
    while (p2 < n) {
      if (st[0] == 0) {
        upd(0, 0, m - 2, l[p2], r[p2], 1);
        sum += w[p2];
        p2++;
      } else {
        upd(0, 0, m - 2, l[p1], r[p1], -1);
        ans = min(ans, w[p2 - 1] - w[p1]);
        sum -= w[p1];
        p1++;
      }
    }
    while (p1 < n) {
      if (st[0]) ans = min(ans, w[p2 - 1] - w[p1]);
      upd(0, 0, m - 2, l[p1], r[p1], -1);
      sum -= w[p1];
      p1++;
    }
    cout << ans;
    return 0;
  }
  