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

#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    for (int i = 0; i < v.size(); ++i)
      os << v[i] << " ";
    os << '\\n';
  }
  return os;
}
#define N 200005
int seg_row[3 * N];
int seg_col[3 * N];
int mar_row[3 * N];
int mar_col[3 * N];
void push_row(int v) {
  if (mar_row[v]) {
    seg_row[v * 2] = seg_row[v * 2 + 1] = seg_row[v];
    mar_row[v * 2] = mar_row[v * 2 + 1] = true;
    mar_row[v] = false;
  }
}
void push_col(int v) {
  if (mar_col[v]) {
    seg_col[v * 2] = seg_col[v * 2 + 1] = seg_col[v];
    mar_col[v * 2] = mar_col[v * 2 + 1] = true;
    mar_col[v] = false;
  }
}

void update_row(int v, int tl, int tr, int l, int r, int new_val) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    seg_row[v] = new_val;
    mar_row[v] = true;
  } else {
    push_row(v);
    int tm = (tl + tr) / 2;
    update_row(v * 2, tl, tm, l, min(r, tm), new_val);
    update_row(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
  }
}
void update_col(int v, int tl, int tr, int l, int r, int new_val) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    seg_col[v] = new_val;
    mar_col[v] = true;
  } else {
    push_col(v);
    int tm = (tl + tr) / 2;
    update_col(v * 2, tl, tm, l, min(r, tm), new_val);
    update_col(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
  }
}

int get_row(int v, int tl, int tr, int pos) {
  if (tl == tr) {
    return seg_row[v];
  }
  push_row(v);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get_row(v * 2, tl, tm, pos);
  else
    return get_row(v * 2 + 1, tm + 1, tr, pos);
}

int get_col(int v, int tl, int tr, int pos) {
  if (tl == tr) {
    return seg_col[v];
  }
  push_col(v);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get_col(v * 2, tl, tm, pos);
  else
    return get_col(v * 2 + 1, tm + 1, tr, pos);
}

signed main() {
  int n, q;
  cin >> n >> q;
  update_row(1, 1, n, 1, n, n);
  update_col(1, 1, n, 1, n, n);
  int ans = (n - 2) * (n - 2);
  int i;
  int rm = n, cm = n;
  // cout << ans << endl;
  for (i = 1; i <= q; i++) {
    int a, b;
    cin >> a >> b;
    if (a == 1) {
      // (1, b)
      int low = get_col(1, 1, n, b);

      if (low == 2)
        continue;
      ans -= (low - 2);
      // rm = min(rm, b);
      if (rm > b)
        rm = b, update_row(1, 1, n, 2, low - 1, rm);
    } else {
      int low = get_row(1, 1, n, b);
      // cout << a << " " << low << '\\n';
      if (low == 2)
        continue;
      ans -= (low - 2);
      // cm = min(cm, b);
      if (cm > b)
        cm = b, update_col(1, 1, n, 2, low - 1, cm);
    }
    // cout << ans << '\\n';
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    for (int i = 0; i < v.size(); ++i)
      os << v[i] << " ";
    os << '\\n';
  }
  return os;
}
#define N 200005
int seg_row[2 * N];
int seg_col[2 * N];
int row[N];
int col[N];
int mar_row[N];
int mar_col[N];
void push_row(int v) {
  if (mar_row[v]) {
    seg_row[v * 2] = seg_row[v * 2 + 1] = seg_row[v];
    mar_row[v * 2] = mar_row[v * 2 + 1] = true;
    mar_row[v] = false;
  }
}
void push_col(int v) {
  if (mar_col[v]) {
    seg_col[v * 2] = seg_col[v * 2 + 1] = seg_col[v];
    mar_col[v * 2] = mar_col[v * 2 + 1] = true;
    mar_col[v] = false;
  }
}

void update_row(int v, int tl, int tr, int l, int r, int new_val) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    seg_row[v] = new_val;
    mar_row[v] = true;
  } else {
    push_row(v);
    int tm = (tl + tr) / 2;
    update_row(v * 2, tl, tm, l, min(r, tm), new_val);
    update_row(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
  }
}
void update_col(int v, int tl, int tr, int l, int r, int new_val) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    seg_col[v] = new_val;
    mar_col[v] = true;
  } else {
    push_col(v);
    int tm = (tl + tr) / 2;
    update_col(v * 2, tl, tm, l, min(r, tm), new_val);
    update_col(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
  }
}

int get_row(int v, int tl, int tr, int pos) {
  if (tl == tr) {
    return seg_row[v];
  }
  push_row(v);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get_row(v * 2, tl, tm, pos);
  else
    return get_row(v * 2 + 1, tm + 1, tr, pos);
}

int get_col(int v, int tl, int tr, int pos) {
  if (tl == tr) {
    return seg_col[v];
  }
  push_col(v);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get_col(v * 2, tl, tm, pos);
  else
    return get_col(v * 2 + 1, tm + 1, tr, pos);
}

signed main() {
  int n, q;
  cin >> n >> q;
  update_row(1, 1, n, 1, n, n);
  update_col(1, 1, n, 1, n, n);
  int ans = (n - 2) * (n - 2);
  int i;
  for (i = 1; i <= q; i++) {
    int a, b;
    cin >> a >> b;
    if (a == 1) {
      // (1, b)
      int low = get_col(1, 1, n, b);
      if (low == 2)
        continue;
      ans -= (low - 2);
      update_row(1, 1, n, 2, low - 1, b);
    } else {
      int low = get_row(1, 1, n, b);
      if (low == 2)
        continue;
      ans -= (low - 2);
      update_col(1, 1, n, 2, low - 1, b);
    }
  }
  cout << ans;
}