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;
  constexpr int N = 1e6 + 10;
  constexpr int MOD = 1e9 + 7;
  int tree[8 * N], lazy[8 * N], n, m, ans = 1e9;
  vector<pair<int, pair<int, int>>> vec;
  void upd(int l, int r, int mid, int idx) {
    int lidx = idx << 1, ridx = lidx | 1;
    if (lazy[idx] == 0) return;
    lazy[lidx] = lazy[idx];
    lazy[ridx] = lazy[idx];
    tree[lidx] = lazy[idx];
    tree[ridx] = lazy[idx];
    lazy[idx] = 0;
  }
  void update(int b, int e, int l, int r, int val, int idx) {
    if (r <= b || l >= e) return;
    if (b <= l && r <= e) {
      tree[idx] = val;
      lazy[idx] = val;
      return;
    }
    int mid = (l + r) >> 1, lidx = idx << 1, ridx = lidx | 1;
    upd(l, r, mid, idx);
    update(b, e, l, mid, val, lidx);
    update(b, e, mid, r, val, ridx);
    tree[idx] = min(tree[lidx], tree[ridx]);
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      int l, r, w;
      cin >> l >> r >> w;
      l = 2 * l - 1;
      r = 2 * r - 1;
      vec.push_back({w, {l - 1, r}});
    }
    sort(vec.begin(), vec.end());
    int le = -1, ri = 0;
    while (le < n) {
      le++;
      while (tree[1] <= le && ri < n) {
        update(vec[ri].second.first, vec[ri].second.second, 0, 2 * m - 1, ri + 1,
               1);
        ri++;
      }
      if (tree[1] > le) {
        ans = min(ans, vec[ri - 1].first - vec[le].first);
      }
    }
    cout << ans << '\n';
    return 0;
  }
  