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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template <class T> using pque = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
void fail() {
  cout << "-1\\n";
  exit(0);
}
void no() {
  cout << "No\\n";
  exit(0);
}
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (auto &e : v)
    s >> e;
  return s;
}
template <class T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (auto &e : v)
    s << e << ' ';
  return s;
}

struct fastio {
  fastio() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  }
} fastio_;

template <class T> class segtree {
  int n;
  vector<T> data;
  T id = 0;
  T operation(T a, T b) { return max(a, b); };

public:
  segtree(int _n) {
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
  }
  segtree(vector<T> vec) {
    int _n = vec.size();
    n = 1;
    while (n < _n + 2)
      n <<= 1;
    data = vector<T>(2 * n, id);
    for (int i = 0; i < _n; i++)
      data[i + n] = vec[i];
    for (int i = n - 1; i >= 1; i--)
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
  }
  void change(int i, T x) {
    i += n;
    data[i] = x;
    while (i > 1) {
      i >>= 1;
      data[i] = operation(data[i << 1], data[i << 1 | 1]);
    }
  }
  void add(int i, T x) { change(i, data[i + n] + x); }
  T get(int a, int b) {
    T left = id;
    T right = id;
    a += n;
    b += n;
    while (a < b) {
      if (a & 1)
        left = operation(left, data[a++]);
      if (b & 1)
        right = operation(data[--b], right);
      a >>= 1;
      b >>= 1;
    }
    return operation(left, right);
  }
  T get_all() { return data[1]; }
  T operator[](int i) { return data[i + n]; }
};

int main() {
  ll n, q;
  cin >> n >> q;
  segtree<int> a(n), b(n);
  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int idx;
      cin >> idx;
      idx--;
      int ok = 0, ng = n - 1;
      while (ng - ok > 1) {
        int mid = (ng + ok) / 2;
        if (b.get(0, mid + 1) >= idx)
          ng = mid;
        else
          ok = mid;
      }
      a.change(idx, ok);
    } else {
      int idx;
      cin >> idx;
      idx--;
      int ok = 0, ng = n - 1;
      while (ng - ok > 1) {
        int mid = (ng + ok) / 2;
        if (a.get(0, mid + 1) >= idx)
          ng = mid;
        else
          ok = mid;
      }
      b.change(idx, ok);
    }
  }
  ll ans = (n - 2) * (n - 2);
  rep(i, n) ans -= a[i] + b[i];
  cout << ans << '\\n';
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template <class T> using pque = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
void fail() {
  cout << "-1\\n";
  exit(0);
}
void no() {
  cout << "No\\n";
  exit(0);
}
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (auto &e : v)
    s >> e;
  return s;
}
template <class T> ostream &operator<<(ostream &s, const vector<T> &v) {
  for (auto &e : v)
    s << e << ' ';
  return s;
}

struct fastio {
  fastio() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  }
} fastio_;

int main() {
  int n, q;
  cin >> n >> q;
  vector<P> query(q);
  vector<int> a, b;
  rep(i, q) {
    cin >> query[i].first >> query[i].second;
    if (query[i].first == 1)
      a.pb(query[i].second);
    else
      b.pb(query[i].second);
  }
  sort(all(a));
  sort(all(b));
  int as = a.size();
  int bs = b.size();
  int x = 2 * as + 1;
  int y = 2 * bs + 1;
  vector<vector<bool>> c(x, vector<bool>(y));
  rep(i, q) {
    if (query[i].first == 1) {
      int idx = lower_bound(all(a), query[i].second) - a.begin();
      idx *= 2;
      idx++;
      rep(i, y) {
        if (!c[idx][i])
          c[idx][i] = true;
        else
          break;
      }
    } else {
      int idx = lower_bound(all(b), query[i].second) - b.begin();
      idx *= 2;
      idx++;
      rep(j, x) {
        if (!c[j][idx])
          c[j][idx] = true;
        else
          break;
      }
    }
  }
  ll ans = 0;
  rep(i, x) {
    rep(j, y) {
      if (!c[i][j]) {
        ll xx, yy;
        if (i & 1)
          xx = 1;
        else
          xx = (i / 2 == as ? n : a[i / 2]) - (i == 0 ? 1 : a[i / 2 - 1]) - 1;
        if (j & 1)
          yy = 1;
        else
          yy = (j / 2 == bs ? n : b[j / 2]) - (j == 0 ? 1 : b[j / 2 - 1]) - 1;
        ans += xx * yy;
      }
    }
  }
  cout << ans << '\\n';
}