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

#include <bits/stdc++.h>
using namespace std;
#define N 200020
const int inf = 0x3f3f3f3f;
struct tree {
  int l, r, min, tag;
} t[N << 3];
int a[N], b[N];
int n, m;
int ls(int cur) { return cur << 1; }
int rs(int cur) { return cur << 1 | 1; }
void lazy(int cur, int k) {
  t[cur].tag = k;
  t[cur].min = k;
}
void pushdown(int cur) {
  if (!t[cur].tag)
    return;
  int mid = (t[cur].l + t[cur].r) >> 1;
  lazy(ls(cur), t[cur].tag);
  lazy(rs(cur), t[cur].tag + mid - t[cur].l + 1);
  t[cur].tag = 0;
  return;
}

void build(int cur, int l, int r) {
  t[cur].l = l;
  t[cur].r = r;
  t[cur].min = t[cur].tag = 0;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(ls(cur), l, mid);
  build(rs(cur), mid + 1, r);
  return;
}

void update(int cur, int ul, int ur, int k) {
  // cout<<cur<<' '<<t[cur].l<<' '<<t[cur].r<<endl;
  pushdown(cur);
  if (t[cur].l >= ul && t[cur].r <= ur) {
    lazy(cur, k);
    return;
  }
  int mid = (t[cur].l + t[cur].r) >> 1;
  if (mid >= ul)
    update(ls(cur), ul, ur, k);
  if (mid < ur)
    update(rs(cur), ul, ur, k + max(0, (mid - max(t[cur].l, ul) + 1)));
  t[cur].min = min(t[ls(cur)].min, t[rs(cur)].min);
  return;
}

int query(int cur, int ql, int qr) {
  if (t[cur].l >= ql && t[cur].r <= qr)
    return t[cur].min;
  pushdown(cur);
  int mid = (t[cur].l + t[cur].r) >> 1;
  int res = inf;
  if (mid >= ql)
    res = min(res, query(ls(cur), ql, qr));
  if (mid < qr)
    res = min(res, query(rs(cur), ql, qr));
  return res;
}

void init() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  return;
}
void solve() {
  build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    // cout<<a[i]<<' '<<b[i]<<endl;
    int k = (a[i] == 1) ? (m + 1) : query(1, a[i] - 1, a[i] - 1) + 1;
    // cout<<k<<endl;
    update(1, a[i], b[i], k);
    if (t[1].min >= m + 1)
      printf("-1\\n");
    else
      printf("%d\\n", t[1].min + i);
  }
  return;
}
int main() {
  init();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N 200020
const int inf = 0x3f3f3f3f;
struct tree {
  int l, r, min, tag;
} t[N << 2];
int a[N], b[N];
int n, m;
int ls(int cur) { return cur << 1; }
int rs(int cur) { return cur << 1 | 1; }
void lazy(int cur, int k) {
  t[cur].tag = k;
  t[cur].min = k;
}
void pushdown(int cur) {
  if (!t[cur].tag)
    return;
  int mid = (t[cur].l + t[cur].r) >> 1;
  lazy(ls(cur), t[cur].tag);
  lazy(rs(cur), t[cur].tag + mid - t[cur].l + 1);
  t[cur].tag = 0;
  return;
}

void build(int cur, int l, int r) {
  t[cur].l = l;
  t[cur].r = r;
  t[cur].min = t[cur].tag = 0;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(ls(cur), l, mid);
  build(rs(cur), mid + 1, r);
  return;
}

void update(int cur, int ul, int ur, int k) {
  // cout<<cur<<' '<<t[cur].l<<' '<<t[cur].r<<endl;
  pushdown(cur);
  if (t[cur].l >= ul && t[cur].r <= ur) {
    lazy(cur, k);
    return;
  }
  int mid = (t[cur].l + t[cur].r) >> 1;
  if (mid >= ul)
    update(ls(cur), ul, ur, k);
  if (mid < ur)
    update(rs(cur), ul, ur, k + max(0, (mid - max(t[cur].l, ul) + 1)));
  t[cur].min = min(t[ls(cur)].min, t[rs(cur)].min);
  return;
}

int query(int cur, int ql, int qr) {
  if (t[cur].l >= ql && t[cur].r <= qr)
    return t[cur].min;
  pushdown(cur);
  int mid = (t[cur].l + t[cur].r) >> 1;
  int res = inf;
  if (mid >= ql)
    res = min(res, query(ls(cur), ql, qr));
  if (mid < qr)
    res = min(res, query(rs(cur), ql, qr));
  return res;
}

void init() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  return;
}
void solve() {
  build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    // cout<<a[i]<<' '<<b[i]<<endl;
    int k = (a[i] == 1) ? (m + 1) : query(1, a[i] - 1, a[i] - 1) + 1;
    // cout<<k<<endl;
    update(1, a[i], b[i], k);
    if (t[1].min >= m + 1)
      printf("-1\\n");
    else
      printf("%d\\n", t[1].min + i);
  }
  return;
}
int main() {
  init();
  solve();
  return 0;
}