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

#include <bits/stdc++.h>
#define rei register int
#define lson (id << 1)
#define rson ((id << 1) + 1)
using namespace std;
const int maxn = 200010;
int h, w;
int tree[maxn << 3], tag[maxn << 3], p[maxn << 3];
void pushdown(int id, int l, int r) {
  if (tag[id] != -1) {
    int mid(l + r >> 1);
    //		tag[lson] = max(tag[id],tag[lson]);
    //		tag[rson] = max(tag[id],tag[rson]);
    tag[lson] = tag[lson] == -1 ? tag[id] : min(tag[lson], tag[id]);
    tag[rson] = tag[rson] == -1 ? tag[id] : min(tag[rson], tag[id]);
    p[lson] = min(p[lson], tag[id]), p[rson] = min(p[rson], tag[id]);
    tree[lson] = max(tree[lson], l - p[lson]);
    tree[rson] = max(tree[rson], mid + 1 - p[rson]);
    tag[id] = -1;
  }
}
void build(int id, int l, int r) {
  if (l == r) {
    tree[id] = 0, p[id] = l, tag[id] = -1;
    return;
  }
  int mid(l + r >> 1);
  tag[id] = -1;
  build(lson, l, mid), build(rson, mid + 1, r);
  tree[id] = min(tree[lson], tree[rson]), p[id] = max(p[lson], p[rson]);
}
void cg(int id, int l, int r, int cl, int cr, int k) {
  if (tag[id] != -1 && l != r)
    pushdown(id, l, r);
  if (cl <= l && cr >= r) {
    p[id] = k, tree[id] = max(tree[id], l - k),
    tag[id] = tag[id] == -1 ? k : min(tag[id], k);
    return;
  }
  int mid(l + r >> 1);
  if (cl <= mid)
    cg(lson, l, mid, cl, cr, k);
  if (cr > mid)
    cg(rson, mid + 1, r, cl, cr, k);
  tree[id] = min(tree[lson], tree[rson]), p[id] = max(p[lson], p[rson]);
}
int qry(int id, int l, int r, int ql, int qr) {
  //	cerr << id << " " << l << " "  << r << " " << ql << " " << qr << " " <<
  //p[id] << endl;
  if (tag[id] != -1 && l != r)
    pushdown(id, l, r);
  if (ql <= l && qr >= r)
    return p[id];
  int ans(0);
  int mid(l + r >> 1);
  if (ql <= mid)
    ans = qry(lson, l, mid, ql, qr);
  if (qr > mid)
    ans = max(ans, qry(rson, mid + 1, r, ql, qr));
  return ans;
}
int main() {
  cin >> h >> w;
  build(1, 1, w);
  //	cerr << p[1] << endl;
  for (rei i = 1; i <= h; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    int x;
    if (l == 1)
      x = -9999999;
    else
      x = qry(1, 1, w, 1, l - 1);
    //		cerr << x << endl;
    cg(1, 1, w, l, r, x);
    if (p[1] > 0)
      printf("%d\\n", i + tree[1]);
    else
      puts("-1");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rei register int
#define lson (id << 1)
#define rson ((id << 1) + 1)
using namespace std;
const int maxn = 200010;
int h, w;
int tree[maxn << 2], tag[maxn << 2], p[maxn << 2];
void pushdown(int id, int l, int r) {
  if (tag[id] != -1) {
    int mid(l + r >> 1);
    //		tag[lson] = max(tag[id],tag[lson]);
    //		tag[rson] = max(tag[id],tag[rson]);
    tag[lson] = tag[lson] == -1 ? tag[id] : min(tag[lson], tag[id]);
    tag[rson] = tag[rson] == -1 ? tag[id] : min(tag[rson], tag[id]);
    p[lson] = min(p[lson], tag[id]), p[rson] = min(p[rson], tag[id]);
    tree[lson] = max(tree[lson], l - p[lson]);
    tree[rson] = max(tree[rson], mid + 1 - p[rson]);
    tag[id] = -1;
  }
}
void build(int id, int l, int r) {
  if (l == r) {
    tree[id] = 0, p[id] = l, tag[id] = -1;
    return;
  }
  int mid(l + r >> 1);
  tag[id] = -1;
  build(lson, l, mid), build(rson, mid + 1, r);
  tree[id] = min(tree[lson], tree[rson]), p[id] = max(p[lson], p[rson]);
}
void cg(int id, int l, int r, int cl, int cr, int k) {
  if (tag[id] != -1)
    pushdown(id, l, r);
  if (cl <= l && cr >= r) {
    p[id] = k, tree[id] = max(tree[id], l - k),
    tag[id] = tag[id] == -1 ? k : min(tag[id], k);
    return;
  }
  int mid(l + r >> 1);
  if (cl <= mid)
    cg(lson, l, mid, cl, cr, k);
  if (cr > mid)
    cg(rson, mid + 1, r, cl, cr, k);
  tree[id] = min(tree[lson], tree[rson]), p[id] = max(p[lson], p[rson]);
}
int qry(int id, int l, int r, int ql, int qr) {
  //	cerr << id << " " << l << " "  << r << " " << ql << " " << qr << " " <<
  //p[id] << endl;
  if (tag[id] != -1)
    pushdown(id, l, r);
  if (ql <= l && qr >= r)
    return p[id];
  int ans(0);
  int mid(l + r >> 1);
  if (ql <= mid)
    ans = qry(lson, l, mid, ql, qr);
  if (qr > mid)
    ans = max(ans, qry(rson, mid + 1, r, ql, qr));
  return ans;
}
int main() {
  cin >> h >> w;
  build(1, 1, w);
  //	cerr << p[1] << endl;
  for (rei i = 1; i <= h; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    int x;
    if (l == 1)
      x = -9999999;
    else
      x = qry(1, 1, w, 1, l - 1);
    //		cerr << x << endl;
    cg(1, 1, w, l, r, x);
    if (p[1] > 0)
      printf("%d\\n", i + tree[1]);
    else
      puts("-1");
  }
  return 0;
}