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;
  int n, m;
  struct node {
    int l, r, w;
    bool operator<(const node &p) const { return w < p.w; }
  };
  node s[300009];
  int mn[4000009], lz[4000009];
  void dn(int a) {
    int la = a * 2, ra = a * 2 + 1, &v = lz[a];
    if (!v) return;
    lz[la] += v, lz[ra] += v;
    mn[la] += v, mn[ra] += v;
    v = 0;
  }
  void upd(int a) { mn[a] = min(mn[a * 2], mn[a * 2 + 1]); }
  void add(int a, int l, int r, int ll, int rr, int v) {
    if (l > rr || r < ll) return;
    if (l >= ll && r <= rr) {
      lz[a] += v;
      mn[a] += v;
      return;
    }
    dn(a);
    int mid = (l + r) / 2;
    add(a * 2, l, mid, ll, rr, v);
    add(a * 2 + 1, mid + 1, r, ll, rr, v);
    upd(a);
  }
  int ask(int a, int l, int r, int ll, int rr) {
    if (l > rr || r < ll) return 0x3F3F3F3F;
    ;
    if (l >= ll && r <= rr) return mn[a];
    dn(a);
    int mid = (l + r) / 2;
    return min(ask(a * 2, l, mid, ll, rr), ask(a * 2 + 1, mid + 1, r, ll, rr));
  }
  int main() {
    scanf("%d%d", &n, &m);
    m--;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].w);
      s[i].r--;
    }
    sort(s + 1, s + n + 1);
    int lp = 1, ans = 0x3F3F3F3F;
    ;
    for (int i = 1; i <= n; i++) {
      add(1, 1, m, s[i].l, s[i].r, 1);
      while (lp <= i && mn[1] > 0) {
        ans = min(ans, s[i].w - s[lp].w);
        add(1, 1, m, s[lp].l, s[lp].r, -1);
        lp++;
      }
    }
    printf("%d\n", ans);
    return 0;
  }
  