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

/*input
176527 15
1 81279
2 22308
2 133061
1 80744
2 44603
1 170938
2 139754
2 15220
1 172794
1 159290
2 156968
1 56426
2 77429
1 97459
2 71282
*/

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// AntBook p155
// Segment Tree
const int INF = 1001001001;
struct SegmentTree {
  int n;
  vector<int> dat;
  // n_: num of elements
  SegmentTree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.resize(2 * n - 1, INF);
  }
  // Sets k-th element at a
  void set(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // Returns k-th element
  int get(int k) { return dat[k + n - 1]; }
  // Returns min[a, b)
  // Use like query(a, b)
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // When intersection of [a, b) and [l, r) is empty
    if (r <= a || b <= l)
      return INF;
    // When [a, b) includes [l, r)
    if (a <= l && r <= b)
      return dat[k];
    // Return min of 2 children
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int main() {
  int n, q;
  cin >> n >> q;

  SegmentTree tx(n + 1), ty(n + 1);
  tx.set(n, n);
  ty.set(n, n);

  ll ans = ll(n - 2) * (n - 2);
  rep(qi, q) {
    int typ, i;
    cin >> typ >> i;
    if (typ == 1) {
      int r = tx.query(i, n + 1);
      ans -= r - 2;
      ty.set(r, min(ty.get(r), i));
    } else {
      int r = ty.query(i, n + 1);
      ans -= r - 2;
      tx.set(r, min(tx.get(r), i));
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

/*input
176527 15
1 81279
2 22308
2 133061
1 80744
2 44603
1 170938
2 139754
2 15220
1 172794
1 159290
2 156968
1 56426
2 77429
1 97459
2 71282
*/

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// AntBook p155
// Segment Tree
const int INF = 1001001001;
struct SegmentTree {
  int n;
  vector<int> dat;
  // n_: num of elements
  SegmentTree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.resize(2 * n - 1, INF);
  }
  // Sets k-th element at a
  void set(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // Returns k-th element
  int get(int k) { return dat[k + n - 1]; }
  // Returns min[a, b)
  // Use like query(a, b)
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // When intersection of [a, b) and [l, r) is empty
    if (r <= a || b <= l)
      return INF;
    // When [a, b) includes [l, r)
    if (a <= l && r <= b)
      return dat[k];
    // Return min of 2 children
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int main() {
  int n, q;
  cin >> n >> q;

  SegmentTree tx(n + 1), ty(n + 1);
  tx.set(n, n);
  tx.set(n, n);

  ll ans = ll(n - 2) * (n - 2);
  rep(qi, q) {
    int typ, i;
    cin >> typ >> i;
    if (typ == 1) {
      int r = tx.query(i, n + 1);
      ans -= r - 2;
      ty.set(r, min(ty.get(r), i));
    } else {
      int r = ty.query(i, n + 1);
      ans -= r - 2;
      tx.set(r, min(tx.get(r), i));
    }
  }
  cout << ans << endl;
}