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

#include <bits/stdc++.h>
using namespace std;
typedef long long L;
const int N = 2e5 + 11, M = N << 2, oo = ~0u >> 2;
int n, m, mn[M], z[M], t[M], g[M];
int ql, qr, qx;
void pu(int x) {
  z[x] = z[x * 2];
  t[x] = t[x * 2 + 1];
  mn[x] = min(mn[x * 2], mn[x * 2 + 1]);
}
void build(int x, int l, int r) {
  if (l == r) {
    z[x] = l;
    t[x] = l;
    return;
  }
  int m = l + r >> 1;
  build(x * 2, l, m);
  build(x * 2 + 1, m + 1, r);
  pu(x);
}
void pt(int x, int w, int r) {
  assert(g[x] <= w);
  assert(t[x] <= w);
  g[x] = w;
  t[x] = w;
  z[x] = w;
  if (w <= m)
    mn[x] = w - r;
  else
    mn[x] = oo;
}
/*int ask(int x,int l,int r){
        if(l==r)return l;
        int m=l+r>>1;
        if(g[x]){
                pt(x*2,g[x],m);
                pt(x*2+1,g[x],r);
                g[x]=0;
        }
        if(t[x*2]>=qx) return ask(x*2,l,m);
        else return ask(x*2+1,m+1,r);
}
int qry(int x,int l,int r){
        if(l==r)return l;
        int m=l+r>>1;
        if(g[x]){
                pt(x*2,g[x],m);
                pt(x*2+1,g[x],r);
                g[x]=0;
        }
        if(z[x*2+1]>=qx) return qry(x*2,l,m);
        else return qry(x*2+1,m+1,r);
}*/
void upd(int x, int l, int r) {
  if (z[x] > qr || t[x] < ql)
    return;

  if (/*ql<=l&&r<=qr&&*/ z[x] >= ql && t[x] <= qr) {
    pt(x, qr + 1, r);
    return;
  }
  int m = l + r >> 1;
  if (g[x]) {
    pt(x * 2, g[x], m);
    pt(x * 2 + 1, g[x], r);
    g[x] = 0;
  }
  // if(ql<=m)
  upd(x * 2, l, m);
  // if(qr>m)
  upd(x * 2 + 1, m + 1, r);
  pu(x);
}
int main() {
  cin >> n >> m;
  int i;
  build(1, 1, m);
  int p = 1;
  for (i = 1; i <= n; ++i) {
    cin >> ql >> qr;
    /*qx=ql;
    ql=ask(1,1,m);
    qx=qr+1;
    if(qr!=m) {
            qr=qry(1,1,m);
    }
    //cerr<<ql<<" "<<qr<<endl;
    if(ql<=qr)*/
    upd(1, 1, m);
    if (mn[1] > m)
      puts("-1");
    else
      printf("%d\\n", mn[1] + i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long L;
const int N = 2e5 + 11, M = N << 2, oo = ~0u >> 2;
int n, m, mn[M], z[M], t[M], g[M];
int ql, qr, qx;
void pu(int x) {
  z[x] = z[x * 2];
  t[x] = t[x * 2 + 1];
  mn[x] = min(mn[x * 2], mn[x * 2 + 1]);
}
void build(int x, int l, int r) {
  if (l == r) {
    z[x] = l;
    t[x] = l;
    return;
  }
  int m = l + r >> 1;
  build(x * 2, l, m);
  build(x * 2 + 1, m + 1, r);
  pu(x);
}
void pt(int x, int w, int r) {
  assert(g[x] <= w);
  assert(z[x] <= w);
  g[x] = w;
  t[x] = w;
  z[x] = w;
  if (w <= m)
    mn[x] = w - r;
  else
    mn[x] = oo;
}
int ask(int x, int l, int r) {
  if (l == r)
    return l;
  int m = l + r >> 1;
  if (g[x]) {
    pt(x * 2, g[x], m);
    pt(x * 2 + 1, g[x], r);
    g[x] = 0;
  }
  if (t[x * 2] >= qx)
    return ask(x * 2, l, m);
  else
    return ask(x * 2 + 1, m + 1, r);
}
int qry(int x, int l, int r) {
  if (l == r)
    return l;
  int m = l + r >> 1;
  if (g[x]) {
    pt(x * 2, g[x], m);
    pt(x * 2 + 1, g[x], r);
    g[x] = 0;
  }
  if (z[x * 2 + 1] >= qx)
    return qry(x * 2, l, m);
  else
    return qry(x * 2 + 1, m + 1, r);
}
void upd(int x, int l, int r) {
  if (ql <= l && r <= qr) {
    pt(x, qx, r);
    return;
  }
  int m = l + r >> 1;
  if (g[x]) {
    pt(x * 2, g[x], m);
    pt(x * 2 + 1, g[x], r);
    g[x] = 0;
  }
  if (ql <= m)
    upd(x * 2, l, m);
  if (qr > m)
    upd(x * 2 + 1, m + 1, r);
  pu(x);
}
int main() {
  cin >> n >> m;
  int i;
  build(1, 1, m);
  int p = 1;
  for (i = 1; i <= n; ++i) {
    cin >> ql >> qr;
    qx = ql;
    ql = ask(1, 1, m);
    qx = qr + 1;
    if (qr != m) {
      qr = qry(1, 1, m);
    }
    // cerr<<ql<<" "<<qr<<endl;
    if (ql <= qr)
      upd(1, 1, m);
    if (mn[1] > m)
      puts("-1");
    else
      printf("%d\\n", mn[1] + i);
  }
}