Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, ans[N], a[N], b[N], t[N << 2], t1[N << 2], t2[N << 2];
void update(int l, int r, int k) {
  t1[k] = t[k] + r - l;
  t2[k] = t[k];
  if (l != r) {
    int m = l + r >> 1;
    t[k << 1] = t[k];
    t[k << 1 | 1] = t[k] + m + 1 - l;
  }
  t[k] = 0;
}
int query(int l, int r, int k, int x) {
  if (t[k])
    update(l, r, k);
  if (l > x)
    return m + 1;
  if (r <= x)
    return t1[k];
  int m = l + r >> 1;
  int a = query(l, m, k << 1, x), b = query(m + 1, r, k << 1 | 1, x);
  if (x <= r)
    a += x >= m ? x - m : 0;
  else
    a += r - m;
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
  return min(a, b);
}
void update(int l, int r, int k, int x, int y, int d) {
  if (t[k])
    update(l, r, k);
  if (r < x || l > y)
    return;
  if (l >= x && r <= y) {
    t[k] = d + l - x;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  update(l, m, k << 1, x, y, d);
  update(m + 1, r, k << 1 | 1, x, y, d);
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  for (int i = 1; i <= n; i++) {
    int d = m + 1;
    d = min(d, query(1, m, 1, a[i] - 1) + 1);
    update(1, m, 1, a[i], b[i], d);
    // cout<<"I: "<<i<<' '<<d<<' '<<a[i]<<' '<<b[i]<<' '<<t2[1]<<endl;
    ans[i] = t2[1];
  }
  for (int i = 1; i <= n; i++) {
    if (ans[i] >= m)
      ans[i] = -1;
    else
      ans[i] += i;
    printf("%d\\n", ans[i]);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, lg[N], tot, a[N], b[N], ans[N];
vector<pair<int, int>> v[N];
int rt[N], t1[N * 100], t2[N * 100], ls[N * 100], rs[N * 100];
void Insert(int l, int r, int x1, int x2, int v1, int v2, int &now, int pre) {
  now = ++tot;
  ls[now] = ls[pre];
  rs[now] = rs[pre];
  t1[now] = t1[pre] + v1;
  t2[now] = t2[pre] + v2;
  if (l == r)
    return;
  int m = l + r >> 1;
  if (x1 <= m || x2 <= m)
    Insert(l, m, x1, x2, v1 && x1 <= m, v2 && x2 <= m, ls[now], ls[pre]);
  if (x1 > m || x2 > m)
    Insert(m + 1, r, x1, x2, v1 && x1 > m, v2 && x2 > m, rs[now], rs[pre]);
}
int query1(int l, int r, int x, int now, int pre) {
  if (r <= x)
    return 0;
  if (l > x)
    return t1[now] - t1[pre];
  int m = l + r >> 1;
  return query1(l, m, x, ls[now], ls[pre]) +
         query1(m + 1, r, x, rs[now], rs[pre]);
}
int query2(int l, int r, int x, int now, int pre) {
  if (l >= x)
    return 0;
  if (r < x)
    return t2[now] - t2[pre];
  int m = l + r >> 1;
  return query2(l, m, x, ls[now], ls[pre]) +
         query2(m + 1, r, x, rs[now], rs[pre]);
}
bool vis[N];
void Move(int x, int y, int d) {
  if (y >= a[x] && y <= b[x]) {
    if (vis[x])
      return;
    vis[x] = true;
    d += b[x] - y + 1;
    if (b[x] + 1 > m)
      return;
    y = b[x] + 1;
    Move(x, y, d);
  } else {
    int xx = x;
    for (int i = 1 << lg[n - x]; i; i >>= 1) {
      // cout<<x+1<<' '<<xx+i<<' '<<y<<'
      // '<<query1(1,n,y,rt[xx+i],rt[x])+query2(1,n,y,rt[xx+i],rt[x])<<endl;
      if (xx + i <= n && query1(1, n, y, rt[xx + i], rt[x]) +
                                 query2(1, n, y, rt[xx + i], rt[x]) ==
                             xx + i - x)
        xx += i;
    }
    // cout<<x<<' '<<xx<<' '<<y<<endl;
    xx++;
    v[x].push_back({d + 1, xx - x - 1});
    d += xx - x;
    if (xx <= n)
      Move(xx, y, d);
  }
}
bool cmp(pair<int, int> &a, pair<int, int> &b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first < b.first;
}
int main() {
  for (int i = 1; i < N; i++)
    lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
  for (int i = 1; i < N; i++)
    lg[i]--;
  memset(ans, -1, sizeof(ans));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  for (int i = 1; i <= n; i++)
    Insert(1, n, a[i], b[i], 1, 1, rt[i], rt[i - 1]);
  for (int i = 1; i <= n; i++)
    Move(1, i, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}