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

#include <bits/stdc++.h>
#define maxn 200086
using namespace std;

struct SegmentTree {
  struct Node {
    int sum, cov;
  } t[maxn << 2];

#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)

  inline void up(int x) { t[x].sum = min(t[ls(x)].sum, t[rs(x)].sum); }

  inline void cover(int x, int y) { t[x].sum = t[x].cov = y; }

  inline void down(int x) {
    if (t[x].cov != -1) {
      cover(ls(x), t[x].cov), cover(rs(x), t[x].cov);
      t[x].cov = -1;
    }
  }

  void modify(int x, int l, int r, int ll, int rr, int d) {
    if (ll <= l && r <= rr) {
      cover(x, d);
      return;
    }
    down(x);
    int mid = l + r >> 1;
    if (mid >= ll)
      modify(ls(x), l, mid, ll, rr, d);
    if (mid < rr)
      modify(rs(x), mid + 1, r, ll, rr, d);
    up(x);
  }

  int query(int x, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr)
      return t[x].sum;
    down(x);
    int mid = l + r >> 1, sum = 1e9;
    if (mid >= ll)
      sum = min(sum, query(ls(x), l, mid, ll, rr));
    if (mid < rr)
      sum = min(sum, query(rs(x), mid + 1, r, ll, rr));
    return sum;
  }

  int a[maxn];

  void build(int x, int l, int r) {
    t[x].cov = -1;
    if (l == r) {
      t[x].sum = a[l];
      return;
    }
    int mid = l + r >> 1;
    build(ls(x), l, mid), build(rs(x), mid + 1, r);
    up(x);
  }
} t[2];

int n, m;
int l, r;

int main() {
  scanf("%d%d", &n, &m);
  t[0].build(1, 1, m);
  for (int i = 1; i <= m; i++)
    t[1].a[i] = -i;
  t[1].build(1, 1, m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l, &r);
    t[0].modify(1, 1, m, l, r, 1e9);
    if (r < m) {
      int x = t[0].query(1, 1, m, r + 1, r + 1);
      int y = t[1].query(1, 1, m, 1, r) + r + 1;
      if (y < x)
        t[0].modify(1, 1, m, r + 1, r + 1, y),
            t[1].modify(1, 1, m, r + 1, r + 1, y - r - 1);
    }
    t[1].modify(1, 1, m, l, r, 1e9);
    if (t[0].t[1].sum == 1e9)
      puts("-1");
    else
      printf("%d\\n", t[0].t[1].sum + i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define maxn 200086

using namespace std;

int n, m;

struct Node {
  bool cov;
  int l, r;
} t[maxn << 2];

#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)

inline void up(int x) {
  if (t[x].cov)
    t[x].l = n + 1, t[x].r = 0;
  else
    t[x].r = max(t[ls(x)].r, t[rs(x)].r), t[x].l = min(t[ls(x)].l, t[rs(x)].l);
}

void modify(int x, int l, int r, int ll, int rr) {
  if (t[x].cov)
    return;
  if (ll <= l && r <= rr) {
    t[x].cov = true, up(x);
    return;
  }
  int mid = l + r >> 1;
  if (mid >= ll)
    modify(ls(x), l, mid, ll, rr);
  if (mid < rr)
    modify(rs(x), mid + 1, r, ll, rr);
  up(x);
}

void build(int x, int l, int r) {
  t[x].cov = false;
  if (l == r) {
    t[x].l = t[x].r = l;
    return;
  }
  int mid = l + r >> 1;
  build(ls(x), l, mid), build(rs(x), mid + 1, r);
  up(x);
}

int x, y, l, r;
int sum;
bool tag;

int main() {
  scanf("%d%d", &n, &m);
  l = 1, r = m;
  build(1, l, r);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    if (tag) {
      puts("-1");
      continue;
    }
    int j = t[1].r;
    if (y >= l)
      modify(1, l, r, max(x, l), y);
    if (!t[1].r) {
      if (y == m) {
        puts("-1");
        tag = true;
        continue;
      }
      sum += y + 1 - j;
      l = y + 1, r = n;
      build(1, l, r);
    }
    printf("%d\\n", sum + i + (l == 1 ? 0 : (t[1].l - l)));
  }
}