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

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define fir first
#define sec second
#define mset(s, t) memset(s, t, sizeof(s))
template <typename T1, typename T2> void ckmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void ckmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
#define int long long
const int N = 3000005;
int n, q;
struct Seg {
#define ls (u << 1)
#define rs (u << 1 | 1)
  int minn[N], tag[N];
  void pushup(int u) { minn[u] = min(minn[ls], minn[rs]); }
  void pushdown(int u) {
    if (tag[u] != 1e9) {
      tag[ls] = min(tag[ls], tag[u]);
      tag[rs] = min(tag[rs], tag[u]);
      minn[ls] = min(minn[ls], tag[u]);
      minn[rs] = min(minn[rs], tag[u]);
      tag[u] = 1e9;
    }
  }
  void build(int u, int l, int r) {
    tag[u] = 1e9, minn[u] = n;
    if (l == r)
      return;
    int mid = l + r >> 1;
    build(ls, l, mid), build(rs, mid + 1, r);
  }
  void update(int u, int l, int r, int ql, int qr, int dlt) {
    pushdown(u);
    if (ql <= l && r <= qr) {
      minn[u] = min(minn[u], dlt);
      tag[u] = min(tag[u], dlt);
      return;
    }
    pushdown(u);
    int mid = l + r >> 1;
    if (ql <= mid)
      update(ls, l, mid, ql, qr, dlt);
    if (qr > mid)
      update(rs, mid + 1, r, ql, qr, dlt);
    pushup(u);
  }
  int query(int u, int l, int r, int pos) {
    pushdown(u);
    if (l == r)
      return minn[u];
    int mid = l + r >> 1;
    if (pos <= mid)
      return query(ls, l, mid, pos);
    else
      return query(rs, mid + 1, r, pos);
  }
} row, col;
// row:行，col:列
signed main() {
  n = read(), q = read();
  ll ans = 1ll * (n - 2) * (n - 2);
  row.build(1, 1, n);
  col.build(1, 1, n);
  while (q--) {
    int opt = read(), x = read();
    if (opt == 1) {
      int t = col.query(1, 1, n, x);
      // printf("opt = 1, t=%d\\n",t);
      ans -= max(0ll, t - 2);
      if (t - 1 >= 2)
        row.update(1, 1, n, 2, t - 1, x);
    } else {
      int t = row.query(1, 1, n, x);
      ans -= max(0ll, t - 2);
      if (t - 1 >= 2)
        col.update(1, 1, n, 2, t - 1, x);
    }
    // printf("%lld\\n",ans);
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define rep(i, l, r) for (rint i = l; i <= r; i++)
#define per(i, l, r) for (rint i = l; i >= r; i--)
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define fir first
#define sec second
#define mset(s, t) memset(s, t, sizeof(s))
template <typename T1, typename T2> void ckmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> void ckmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
#define int long long
const int N = 600005;
int n, q;
struct Seg {
#define ls (u << 1)
#define rs (u << 1 | 1)
  int minn[N], tag[N];
  void pushup(int u) { minn[u] = min(minn[ls], minn[rs]); }
  void pushdown(int u) {
    if (tag[u] != 1e9) {
      tag[ls] = min(tag[ls], tag[u]);
      tag[rs] = min(tag[rs], tag[u]);
      minn[ls] = min(minn[ls], tag[u]);
      minn[rs] = min(minn[rs], tag[u]);
      tag[u] = 1e9;
    }
  }
  void build(int u, int l, int r) {
    tag[u] = 1e9, minn[u] = n;
    if (l == r)
      return;
    int mid = l + r >> 1;
    build(ls, l, mid), build(rs, mid + 1, r);
  }
  void update(int u, int l, int r, int ql, int qr, int dlt) {
    pushdown(u);
    if (ql <= l && r <= qr) {
      minn[u] = min(minn[u], dlt);
      tag[u] = min(tag[u], dlt);
      return;
    }
    pushdown(u);
    int mid = l + r >> 1;
    if (ql <= mid)
      update(ls, l, mid, ql, qr, dlt);
    if (qr > mid)
      update(rs, mid + 1, r, ql, qr, dlt);
    pushup(u);
  }
  int query(int u, int l, int r, int pos) {
    pushdown(u);
    if (l == r)
      return minn[u];
    int mid = l + r >> 1;
    if (pos <= mid)
      return query(ls, l, mid, pos);
    else
      return query(rs, mid + 1, r, pos);
  }
} row, col;
// row:行，col:列
signed main() {
  n = read(), q = read();
  ll ans = 1ll * (n - 2) * (n - 2);
  row.build(1, 1, n);
  col.build(1, 1, n);
  while (q--) {
    int opt = read(), x = read();
    if (opt == 1) {
      int t = col.query(1, 1, n, x);
      // printf("opt = 1, t=%d\\n",t);
      ans -= max(0ll, t - 2);
      if (t - 1 >= 2)
        row.update(1, 1, n, 2, t - 1, x);
    } else {
      int t = row.query(1, 1, n, x);
      ans -= max(0ll, t - 2);
      if (t - 1 >= 2)
        col.update(1, 1, n, 2, t - 1, x);
    }
    // printf("%lld\\n",ans);
  }
  printf("%lld\\n", ans);
  return 0;
}