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

/*************************************************************************
        > File Name: 4.cpp
        > Author: Knowledge_llz
        > Mail: 925538513@qq.com
        > Blog: https://blog.csdn.net/Pig_cfbsl
        > Created Time: 2020/9/19 21:08:49
 ************************************************************************/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
#define ls (node << 1)
#define rs (node << 1 | 1)
using namespace std;
int read() {
  char x = getchar();
  int u = 0, fg = 0;
  while (!isdigit(x)) {
    if (x == '-')
      fg = 1;
    x = getchar();
  }
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return fg ? -u : u;
}
const int maxx = 2e6 + 10;
LL ans;
int n, q, tr[2][maxx << 2], lazy[2][maxx << 2], o, oo = 1e9;
void build(int node, int l, int r) {
  lazy[0][node] = lazy[1][node] = oo;
  if (l >= r) {
    tr[0][node] = tr[1][node] = n - 2;
    return;
  }
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}
void pushdown(int node, int x) {
  if (tr[o][node] == oo)
    return;
  tr[o][ls] = min(tr[o][ls], x);
  tr[o][rs] = min(tr[o][rs], x);
  lazy[o][ls] = min(lazy[o][ls], x);
  lazy[o][rs] = min(lazy[o][rs], x);
  lazy[o][node] = oo;
}

int query(int node, int l, int r, int pos) {
  if (l >= r) {
    int tmp = tr[o][node];
    tr[o][node] = 0;
    return tmp;
  }
  pushdown(node, lazy[o][node]);
  int mid = (l + r) >> 1;
  if (pos <= mid)
    return query(ls, l, mid, pos);
  else
    return query(rs, mid + 1, r, pos);
}
void update(int node, int l, int r, int ul, int ur, int x) {
  if (l >= ul && r <= ur) {
    tr[o][node] = min(tr[o][node], x);
    lazy[o][node] = min(lazy[o][node], x);
    return;
  }
  pushdown(node, lazy[o][node]);
  int mid = (l + r) >> 1;
  if (ur <= mid)
    update(ls, l, mid, ul, ur, x);
  else if (ul > mid)
    update(rs, mid + 1, r, ul, ur, x);
  else {
    update(ls, l, mid, ul, mid, x);
    update(rs, mid + 1, r, mid + 1, ur, x);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  int u;
  n = read();
  q = read();
  ans = 1ll * (n - 2) * (n - 2);
  build(1, 1, n - 2);
  while (q--) {
    o = read() - 1;
    u = read() - 1;
    int x = query(1, 1, n - 2, u);
    ans -= x;
    o ^= 1;
    if (x)
      update(1, 1, n - 2, 1, x, u - 1);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

/*************************************************************************
        > File Name: 4.cpp
        > Author: Knowledge_llz
        > Mail: 925538513@qq.com
        > Blog: https://blog.csdn.net/Pig_cfbsl
        > Created Time: 2020/9/19 21:08:49
 ************************************************************************/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define For(i, a, b) for (register int i = (a); i <= (b); ++i)
#define LL long long
#define ls (node << 1)
#define rs (node << 1 | 1)
using namespace std;
int read() {
  char x = getchar();
  int u = 0, fg = 0;
  while (!isdigit(x)) {
    if (x == '-')
      fg = 1;
    x = getchar();
  }
  while (isdigit(x)) {
    u = (u << 3) + (u << 1) + (x ^ 48);
    x = getchar();
  }
  return fg ? -u : u;
}
const int maxx = 2e6 + 10;
LL n, q, ans;
int tr[2][maxx << 2], lazy[2][maxx << 2], o, oo = 1e9;
void build(int node, int l, int r) {
  lazy[0][node] = lazy[1][node] = oo;
  if (l >= r) {
    tr[0][node] = tr[1][node] = n - 2;
    return;
  }
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}
void pushdown(int node, int x) {
  if (tr[o][node] == oo)
    return;
  tr[o][ls] = min(tr[o][ls], x);
  tr[o][rs] = min(tr[o][rs], x);
  lazy[o][ls] = min(lazy[o][ls], x);
  lazy[o][rs] = min(lazy[o][rs], x);
  lazy[o][node] = oo;
}

int query(int node, int l, int r, int pos) {
  if (l >= r) {
    int tmp = tr[o][node];
    tr[o][node] = 0;
    return tmp;
  }
  pushdown(node, lazy[o][node]);
  int mid = (l + r) >> 1;
  if (pos <= mid)
    query(ls, l, mid, pos);
  else
    query(rs, mid + 1, r, pos);
}
void update(int node, int l, int r, int ul, int ur, int x) {
  if (l >= ul && r <= ur) {
    tr[o][node] = min(tr[o][node], x);
    lazy[o][node] = min(lazy[o][node], x);
    return;
  }
  pushdown(node, lazy[o][node]);
  int mid = (l + r) >> 1;
  if (ur <= mid)
    update(ls, l, mid, ul, ur, x);
  else if (ul > mid)
    update(rs, mid + 1, r, ul, ur, x);
  else {
    update(ls, l, mid, ul, mid, x);
    update(rs, mid + 1, r, mid + 1, ur, x);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  int u;
  n = read();
  q = read();
  ans = (n - 2) * (n - 2);
  build(1, 1, n - 2);
  while (q--) {
    o = read() - 1;
    u = read() - 1;
    int x = query(1, 1, n - 2, u);
    ans -= x;
    o ^= 1;
    if (x)
      update(1, 1, n - 2, 1, x, u - 1);
  }
  printf("%lld\\n", ans);
  return 0;
}