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 mod = 1e9 + 7;
  const int maxn = 1e6 + 5;
  struct node {
    int l, r, mn, add;
  } s[maxn * 4];
  inline void pushup(int pos) {
    s[pos].mn = min(s[(pos << 1)].mn, s[(pos << 1 | 1)].mn);
  }
  inline void apptag(int pos, int add) {
    s[pos].add += add;
    s[pos].mn += add;
  }
  inline void pushdown(int pos) {
    if (!s[pos].add) return;
    apptag((pos << 1), s[pos].add);
    apptag((pos << 1 | 1), s[pos].add);
    s[pos].add = 0;
  }
  void build(int l, int r, int pos) {
    s[pos] = {l, r, 0, 0};
    if (l == r) return;
    int mid = l + r >> 1;
    build(l, mid, (pos << 1));
    build(mid + 1, r, (pos << 1 | 1));
  }
  void update(int l, int r, int add, int pos) {
    if (s[pos].l == l && s[pos].r == r) {
      apptag(pos, add);
      return;
    }
    pushdown(pos);
    int mid = s[pos].l + s[pos].r >> 1;
    if (r <= mid)
      update(l, r, add, (pos << 1));
    else if (l > mid)
      update(l, r, add, (pos << 1 | 1));
    else
      update(l, mid, add, (pos << 1)), update(mid + 1, r, add, (pos << 1 | 1));
    pushup(pos);
  }
  struct line {
    int l, r, w;
    bool operator<(const line &a) const { return w < a.w; }
  } p[maxn];
  int main() {
    int n, m;
    scanf("%d", &n);
    scanf("%d", &m);
    build(1, m - 1, 1);
    for (int i = (1); i <= (n); i++)
      scanf("%d", &p[i].l), scanf("%d", &p[i].r), scanf("%d", &p[i].w);
    sort(p + 1, p + n + 1);
    int ans = 1e9, j = 0;
    for (int i = (1); i <= (n); i++) {
      while (j + 1 <= n && s[1].mn <= 0) {
        j++;
        update(p[j].l, p[j].r - 1, 1, 1);
      }
      if (s[1].mn > 0) ans = min(ans, p[j].w - p[i].w);
      update(p[i].l, p[i].r - 1, -1, 1);
    }
    cout << ans;
  }
  