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<int, pair<int, int> > > v;
  int la[4000005], seg[4000005];
  void cal(int lv, int st, int ed) {
    int lv2 = lv * 2;
    if (st != ed) {
      la[lv2] += la[lv];
      la[lv2 + 1] += la[lv];
    }
    seg[lv] += la[lv];
    la[lv] = 0;
  }
  void up(int st, int ed, int lv, int l, int r, int val) {
    cal(lv, st, ed);
    if (st > r || ed < l) {
      return;
    }
    if (st >= l && ed <= r) {
      la[lv] += val;
      cal(lv, st, ed);
      return;
    }
    int lv2 = lv * 2;
    int md = (st + ed) / 2;
    up(st, md, lv2, l, r, val);
    up(md + 1, ed, lv2 + 1, l, r, val);
    seg[lv] = min(seg[lv2], seg[lv2 + 1]);
  }
  int main() {
    int t, p;
    scanf("%d%d", &t, &p);
    p--;
    for (int i = 0; i < t; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      b--;
      v.push_back({c, {a, b}});
    }
    sort(v.begin(), v.end());
    int mn = 1e7;
    int po = 0;
    for (int i = 0; i < t; i++) {
      up(1, p, 1, v[i].second.first, v[i].second.second, 1);
      while (seg[1] > 0) {
        up(1, p, 1, v[po].second.first, v[po].second.second, -1);
        mn = min(mn, v[i].first - v[po].first);
        po++;
      }
    }
    printf("%d\n", mn);
  }
  