Problem p02575 (C++)

Buggy submission (status: Runtime Error):
// 22:30 -
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

struct SegTree {
  static const int M = N * 4;
  int mn[M], mx[M], d[M], lazy[M];

  void Tag(int o, int L, int R, int val) {
    mn[o] = mx[o] = val;
    d[o] = val - R;
    lazy[o] = val;
  }
  void Pushdown(int o, int L, int R) {
    if (lazy[o] == 0)
      return;
    int M = (L + R) / 2;
    Tag(lch, L, M, lazy[o]);
    Tag(rch, M + 1, R, lazy[o]);
    lazy[o] = 0;
  }
  void Maintain(int o) {
    mx[o] = max(mx[lch], mx[rch]);
    mn[o] = min(mn[lch], mn[rch]);
    d[o] = min(d[lch], d[rch]);
  }
  void Build(int o, int L, int R) {
    if (L == R) {
      mx[o] = mn[o] = L;
      return;
    }
    int M = (L + R) / 2;
    Build(lch, L, M);
    Build(rch, M + 1, R);
    Maintain(o);
  }
  void Modify(int o, int L, int R, int qL, int qR, int val) {
    if (qL <= mn[o] && mx[o] <= qR) {
      Tag(o, L, R, val);
      return;
    }
    Pushdown(o, L, R);
    int M = (L + R) / 2;
    if (qL <= mx[lch])
      Modify(lch, L, M, qL, qR, val);
    if (mn[rch] <= qR)
      Modify(rch, M + 1, R, qL, qR, val);
    Maintain(o);
  }
  int Query() { return d[1]; }
};
SegTree seg;

int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  seg.Build(1, 1, m);

  for (int i = 1; i <= n; i++) {
    int r = b[i] == m ? INF : b[i] + 1;
    seg.Modify(1, 1, m, a[i], b[i], r);
    int ans = seg.Query();
    if (ans >= m)
      cout << -1 << endl;
    else
      cout << ans + i << endl;
  }

  return 0;
}


Fixed submission:
// 22:30 -
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

struct SegTree {
  static const int M = N * 4;
  int mn[M], mx[M], d[M], lazy[M];

  void Tag(int o, int L, int R, int val) {
    mn[o] = mx[o] = val;
    d[o] = val - R;
    lazy[o] = val;
  }
  void Pushdown(int o, int L, int R) {
    if (lazy[o] == 0)
      return;
    int M = (L + R) / 2;
    Tag(lch, L, M, lazy[o]);
    Tag(rch, M + 1, R, lazy[o]);
    lazy[o] = 0;
  }
  void Maintain(int o) {
    mx[o] = max(mx[lch], mx[rch]);
    mn[o] = min(mn[lch], mn[rch]);
    d[o] = min(d[lch], d[rch]);
  }
  void Build(int o, int L, int R) {
    if (L == R) {
      mx[o] = mn[o] = L;
      return;
    }
    int M = (L + R) / 2;
    Build(lch, L, M);
    Build(rch, M + 1, R);
    Maintain(o);
  }
  void Modify(int o, int L, int R, int qL, int qR, int val) {
    if (qL <= mn[o] && mx[o] <= qR) {
      Tag(o, L, R, val);
      return;
    }
    if (mx[o] < qL || qR < mn[o])
      return;
    Pushdown(o, L, R);
    int M = (L + R) / 2;
    if (qL <= mx[lch])
      Modify(lch, L, M, qL, qR, val);
    if (mn[rch] <= qR)
      Modify(rch, M + 1, R, qL, qR, val);
    Maintain(o);
  }
  int Query() { return d[1]; }
};
SegTree seg;

int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  seg.Build(1, 1, m);

  for (int i = 1; i <= n; i++) {
    int r = b[i] == m ? INF : b[i] + 1;
    seg.Modify(1, 1, m, a[i], b[i], r);
    int ans = seg.Query();
    if (ans >= m)
      cout << -1 << endl;
    else
      cout << ans + i << endl;
  }

  return 0;
}