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;
  int n, m;
  struct Seg {
    int l;
    int r;
    int w;
  };
  vector<int> segTree;
  vector<int> lazyTree;
  void updateSegT(int node, int st, int end, int l, int r, int val) {
    if (l > r) return;
    if (lazyTree[node]) {
      lazyTree[2 * node] += lazyTree[node];
      lazyTree[2 * node + 1] += lazyTree[node];
      segTree[node] += lazyTree[node];
      lazyTree[node] = 0;
    }
    if (l > end || r < st) return;
    if (st >= l && end <= r) {
      segTree[node] += val;
      lazyTree[2 * node] += val;
      lazyTree[2 * node + 1] += val;
    } else {
      int mid = (st + end) / 2;
      updateSegT(2 * node, st, mid, l, r, val);
      updateSegT(2 * node + 1, mid + 1, end, l, r, val);
      segTree[node] = min(segTree[2 * node], segTree[2 * node + 1]);
    }
  }
  vector<Seg> segs;
  int main() {
    cin >> n >> m;
    m--;
    int nm = 1 << (int)ceil(log2(m));
    segTree.resize(nm * 2);
    lazyTree.resize(nm * 2 * 2);
    int l, r, w;
    for (int(i) = (0); (i) < (n); ++(i)) {
      cin >> l >> r >> w;
      segs.push_back({l, r - 1, w});
    }
    sort(segs.begin(), segs.end(),
         [](const Seg& a, const Seg& b) { return (a.w < b.w); });
    updateSegT(1, 1, nm, m + 1, nm, 3000001);
    int ans = INT_MAX;
    for (int p1 = 0, p2 = 0; p1 < n; p1++) {
      updateSegT(1, 1, nm, segs[p1].l, segs[p1].r, 1);
      if (!segTree[1]) continue;
      while (segTree[1]) {
        updateSegT(1, 1, nm, segs[p2].l, segs[p2].r, -1);
        p2++;
      }
      ans = min(ans, segs[p1].w - segs[p2 - 1].w);
    }
    cout << ans;
    return 0;
  }
  