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;
  vector<pair<pair<int, int>, int> > a;
  int n, m;
  int t[4 * 1001000];
  int add[4 * 1001000];
  void push(int v, int tl, int tr) {
    if (add[v] == 0) return;
    t[v] += add[v];
    if (tl == tr) {
      add[v] = 0;
      return;
    }
    add[2 * v] += add[v];
    add[2 * v + 1] += add[v];
    add[v] = 0;
  }
  void update(int v, int tl, int tr, int l, int r, int val) {
    push(v, tl, tr);
    if (tl > r || tr < l) return;
    if (l <= tl && tr <= r) {
      add[v] += val;
      return;
    }
    int mid = (tl + tr) >> 1;
    update(2 * v, tl, mid, l, r, val);
    update(2 * v + 1, mid + 1, tr, l, r, val);
    t[v] = min(t[2 * v] + add[2 * v], t[2 * v + 1] + add[2 * v + 1]);
  }
  int get(int v, int tl, int tr, int l, int r) {
    push(v, tl, tr);
    if (l > tr || r < tl) return (int)1e9;
    if (l <= tl && tr <= r) {
      return t[v] + add[v];
    }
    int mid = (tl + tr) >> 1;
    int ans1 = get(2 * v, tl, mid, l, r);
    int ans2 = get(2 * v + 1, mid + 1, tr, l, r);
    return min(ans1, ans2);
  }
  void print(int v, int tl, int tr, int tab = 0) {
    for (int i = 0; i < tab; i++) printf("  ");
    printf("(%d, %d) - [%d, %d] v=%d\n", t[v], add[v], tl, tr, v);
    if (tl == tr) return;
    int mid = (tl + tr) >> 1;
    print(2 * v, tl, mid, tab + 1);
    print(2 * v + 1, mid + 1, tr, tab + 1);
  }
  bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
    return a.second < b.second;
  }
  int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      int l, r, x;
      cin >> l >> r >> x;
      a.push_back({{l, r}, x});
    }
    sort(a.begin(), a.end(), cmp);
    int r = -1;
    int ans = (int)1e9;
    for (int l = 0; l < n; l++) {
      while (r + 1 < n && get(1, 0, m, 1, m - 1) == 0) {
        update(1, 0, m, a[r + 1].first.first, a[r + 1].first.second - 1, 1);
        r++;
      }
      if (get(1, 0, m, 1, m - 1) == 0) break;
      ans = min(ans, a[r].second - a[l].second);
      update(1, 0, m, a[l].first.first, a[l].first.second - 1, -1);
    }
    cout << ans << endl;
    return 0;
  }
  