Problem p02551 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(i, k, n) for (int i = k; i < n; ++i)
#define fri(i, k, n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(), arr.end()

void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int N = 2e5 + 5;
int t[2][4 * N], n, lazy[2][4 * N];
bool marked[2][4 * N];

void push(int v, int i) {
  if (marked[v]) {
    t[i][v * 2] = max(t[i][v * 2], lazy[i][v]);
    t[i][v * 2 + 1] = max(t[i][v * 2 + 1], lazy[i][v]);
    lazy[i][v * 2] = max(lazy[i][v * 2], lazy[i][v]);
    lazy[i][v * 2 + 1] = max(lazy[i][v * 2 + 1], lazy[i][v]);
    lazy[i][v] = 0;
    marked[i][v * 2] = marked[i][v * 2 + 1] = true;
    marked[i][v] = false;
  }
}

void update(int v, int tl, int tr, int l, int r, int new_val, int i) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    push(v, i);
    lazy[i][v] = max(lazy[i][v], new_val);
    t[i][v] = max(t[i][v], new_val);
    marked[i][v] = true;
  } else {
    push(v, i);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), new_val, i);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val, i);
    t[i][v] = max(t[i][v * 2], t[i][v * 2 + 1]);
  }
}

int get(int v, int tl, int tr, int pos, int i) {
  if (tl == tr) {
    return t[i][v];
  }
  push(v, i);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get(v * 2, tl, tm, pos, i);
  else
    return get(v * 2 + 1, tm + 1, tr, pos, i);
}

void solve() {
  fr(i, 0, 2) {
    fr(j, 0, 4 * N) { t[i][j] = 1; }
  }
  int n, q;
  cin >> n >> q;
  ll ans = 0;
  while (q--) {
    int i, y;
    cin >> i >> y;
    i--;
    int mn = get(1, 1, n, y, i);
    // cout<<mn<<endl;
    ans += (n - 1LL - mn);
    if (i == 0)
      update(1, 1, n, 1, n - mn, n - y + 1, i ^ 1);
    else
      update(1, 1, n, 1, n - mn, n - y + 1, i ^ 1);
  }
  // cout<<ans<<endl;
  ans = ((n - 2) * 1LL * (n - 2) - ans);
  cout << ans << endl;
  return;
}

int main() {
  boost();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(i, k, n) for (int i = k; i < n; ++i)
#define fri(i, k, n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(), arr.end()

void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

const int N = 2e5 + 5;
int t[2][4 * N], n, lazy[2][4 * N];
bool marked[2][4 * N];

void push(int v, int i) {
  if (marked[v]) {
    t[i][v * 2] = max(t[i][v * 2], lazy[i][v]);
    t[i][v * 2 + 1] = max(t[i][v * 2 + 1], lazy[i][v]);
    lazy[i][v * 2] = max(lazy[i][v * 2], lazy[i][v]);
    lazy[i][v * 2 + 1] = max(lazy[i][v * 2 + 1], lazy[i][v]);
    lazy[i][v] = 0;
    marked[i][v * 2] = marked[i][v * 2 + 1] = true;
    marked[i][v] = false;
  }
}

void update(int v, int tl, int tr, int l, int r, int new_val, int i) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    // push(v,i);
    lazy[i][v] = max(lazy[i][v], new_val);
    t[i][v] = max(t[i][v], new_val);
    marked[i][v] = true;
  } else {
    push(v, i);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), new_val, i);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val, i);
    t[i][v] = max(t[i][v * 2], t[i][v * 2 + 1]);
  }
}

int get(int v, int tl, int tr, int pos, int i) {
  if (tl == tr) {
    return t[i][v];
  }
  push(v, i);
  int tm = (tl + tr) / 2;
  if (pos <= tm)
    return get(v * 2, tl, tm, pos, i);
  else
    return get(v * 2 + 1, tm + 1, tr, pos, i);
}

void solve() {
  fr(i, 0, 2) {
    fr(j, 0, 4 * N) { t[i][j] = 1; }
  }
  int n, q;
  cin >> n >> q;
  ll ans = 0;
  while (q--) {
    int i, y;
    cin >> i >> y;
    i--;
    int mn = get(1, 1, n, y, i);
    // cout<<mn<<endl;
    ans += (n - 1LL - mn);
    if (i == 0)
      update(1, 1, n, 1, n - mn, n - y + 1, i ^ 1);
    else
      update(1, 1, n, 1, n - mn, n - y + 1, i ^ 1);
  }
  // cout<<ans<<endl;
  ans = ((n - 2) * 1LL * (n - 2) - ans);
  cout << ans << endl;
  return;
}

int main() {
  boost();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    solve();
  return 0;
}