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 = 1000006, INF = 1e9, P = 998244353;
  int n, m, ans = INF, tree[4 * N], E[4 * N];
  pair<int, pair<int, int> > p[N];
  void push(int v, int tl, int tr) {
    if (E[v] != 0 && v * 2 + 1 < 4 * N) {
      E[v << 1] += E[v];
      E[v << 1 ^ 1] += E[v];
      tree[v << 1] += E[v];
      tree[v << 1 ^ 1] += E[v];
      E[v] = 0;
    }
  }
  void add(int l, int r, int val, int v, int tl, int tr) {
    if (l <= tl && tr <= r) {
      tree[v] += val;
      E[v] += val;
      return;
    }
    if (tr < l || r < tl) return;
    push(v, tl, tr);
    int tm = tl + tr >> 1;
    add(l, r, val, v << 1, tl, tm);
    add(l, r, val, v << 1 ^ 1, tm + 1, tr);
    if (tree[v << 1] < tree[v << 1 ^ 1])
      tree[v] = tree[v << 1];
    else
      tree[v] = tree[v << 1 ^ 1];
  }
  int main() {
    cin >> n >> m;
    m--;
    for (int i = 1; i <= n; i++) {
      cin >> p[i].second.first >> p[i].second.second >> p[i].first;
      p[i].second.first--;
      p[i].second.second -= 2;
    }
    sort(p + 1, p + n + 1);
    int R = 1;
    for (int i = 1; i <= n; i++) {
      while (!tree[1] && R <= n)
        add(p[R].second.first, p[R].second.second, 1, 1, 0, m - 1), R++;
      if (!tree[1]) break;
      ans = min(p[R - 1].first - p[i].first, ans);
      add(p[i].second.first, p[i].second.second, -1, 1, 0, m - 1);
    }
    cout << ans;
  }
  