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 N = 1001000;
  const int M = 4 * N;
  const int inf = 1e9;
  int n, k, m;
  int tree[M], lazy[M];
  void update(int i, int j, int x, int idx = 1, int l = 0, int r = n - 1) {
    if (lazy[idx]) {
      tree[idx] += lazy[idx];
      if (r > l) {
        lazy[(idx << 1)] += lazy[idx];
        lazy[(idx << 1 | 1)] += lazy[idx];
      }
      lazy[idx] = 0;
    }
    if (j < l || i > r) return;
    if (l >= i && r <= j) {
      tree[idx] += x;
      if (r > l) {
        lazy[(idx << 1)] += x;
        lazy[(idx << 1 | 1)] += x;
      }
      return;
    }
    update(i, j, x, (idx << 1), l, ((l + r) >> 1));
    update(i, j, x, (idx << 1 | 1), ((l + r) >> 1) + 1, r);
    tree[idx] = min(tree[(idx << 1)], tree[(idx << 1 | 1)]);
  }
  int queryMin(int i, int j, int idx = 1, int l = 0, int r = n - 1) {
    if (lazy[idx] != 0) {
      tree[idx] += lazy[idx];
      if (r > l) {
        lazy[(idx << 1)] += lazy[idx];
        lazy[(idx << 1 | 1)] += lazy[idx];
      }
      lazy[idx] = 0;
    }
    if (i > r || j < l) return inf;
    if (i <= l && j >= r) return tree[idx];
    return min(queryMin(i, j, (idx << 1), l, ((l + r) >> 1)),
               queryMin(i, j, (idx << 1 | 1), ((l + r) >> 1) + 1, r));
  }
  pair<int, pair<int, int>> p[300300];
  int main() {
    cin >> m >> n;
    for (int i = 0; i < m; i++) {
      int l, r, w;
      cin >> l >> r >> w;
      r--;
      p[i].first = w;
      p[i].second.first = l;
      p[i].second.second = r;
      update(l, r, 1);
    }
    sort(p, p + m);
    long long l = 0, r = m - 1;
    for (int i = 0; i < m; i++) {
      update(p[i].second.first, p[i].second.second, -1);
      if (queryMin(1, n - 1) == 0) {
        update(p[i].second.first, p[i].second.second, 1);
        l = i - 1;
        break;
      }
    }
    int ans = p[m - 1].first - p[l + 1].first;
    while (l < r) {
      if (queryMin(1, n - 1) == 0) {
        if (l < 0) break;
        update(p[l].second.first, p[l].second.second, 1);
        l--;
      } else {
        ans = min(ans, p[r].first - p[l + 1].first);
        update(p[r].second.first, p[r].second.second, -1);
        r--;
      }
    }
    cout << ans << endl;
    return 0;
  }
  