Problem p02551 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define cauto const auto &
#define ALL(v) begin(v), end(v)
#else
#define ALL(v) (v).begin(), (v).end()
#endif

namespace {
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y) { return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y) { return x = max(x, y); }
template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}
template <class Con> string concat(const Con &c, const string &spr) {
  stringstream ss;
  typename Con::const_iterator it = c.begin(), en = c.end();
  bool fst = true;
  for (; it != en; ++it) {
    if (!fst) {
      ss << spr;
    }
    fst = false;
    ss << *it;
  }
  return ss.str();
}

template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
  vector<typename Con::value_type> ret;
  typename Con::const_iterator it = c.begin(), en = c.end();
  for (; it != en; ++it) {
    if (f(*it)) {
      ret.emplace_back(*it);
    }
  }
  return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
  vector<decltype(f(*c.begin()))> ret;
  ret.reserve(c.size());
  for (const auto &x : c) {
    ret.emplace_back(f(x));
  }
  return ret;
}
#endif

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RALL(v) (v).rbegin(), (v).rend()
#define tget(t, i) get<i>(t)

#define MOD 1000000007LL
#define EPS 1e-8

const int NONE = -1010101010;

struct segtree {
  int m;
  vector<int> dat;
  segtree(int n, int init) {
    for (m = 1; m < n; ++m)
      ;
    dat.assign(m * 2 - 1, init);
  }
  void update(int a, int b, int v) { update(a, b, v, 0, 0, m); }
  void update(int a, int b, int v, int k, int l, int r) {
    if (r <= a || b <= l) {
      return;
    }
    if (a <= l && r <= b) {
      chmin(dat[k], v);
    } else {
      update(a, b, v, k * 2 + 1, l, (l + r) >> 1);
      update(a, b, v, k * 2 + 2, (l + r) >> 1, r);
    }
  }
  int query(int p) {
    int k = 0, l = 0, r = m, ret = dat[0];
    while (k < m - 1) {
      int x = (l + r) >> 1;
      if (p < x) {
        k = k * 2 + 1;
        r = x;
      } else {
        k = k * 2 + 2;
        l = x;
      }
      chmin(ret, dat[k]);
    }
    return ret;
  }
};

void mainmain() {
  int n, q;
  scanf("%d%d", &n, &q);
  segtree sts[2] = {segtree(n, n - 1), segtree(n, n - 1)};
  LL ans = (n - 2LL) * (n - 2LL);
  for (int i = 0; i < q; ++i) {
    int tp, x;
    scanf("%d%d", &tp, &x);
    --x;
    --tp;
    int y = sts[tp].query(x);
    ans -= y - 1;
    sts[!tp].update(0, y, x);
  }
  printf("%lld\n", ans);
}

} // namespace
int main() try {
  //	ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(4);
  mainmain();
} catch (const exception &e) {
  cerr << "exception: " << e.what() << endl;
  abort();
} catch (...) {
  cerr << "exception" << endl;
  abort();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define cauto const auto &
#define ALL(v) begin(v), end(v)
#else
#define ALL(v) (v).begin(), (v).end()
#endif

namespace {
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y) { return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y) { return x = max(x, y); }
template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}
template <class Con> string concat(const Con &c, const string &spr) {
  stringstream ss;
  typename Con::const_iterator it = c.begin(), en = c.end();
  bool fst = true;
  for (; it != en; ++it) {
    if (!fst) {
      ss << spr;
    }
    fst = false;
    ss << *it;
  }
  return ss.str();
}

template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
  vector<typename Con::value_type> ret;
  typename Con::const_iterator it = c.begin(), en = c.end();
  for (; it != en; ++it) {
    if (f(*it)) {
      ret.emplace_back(*it);
    }
  }
  return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
  vector<decltype(f(*c.begin()))> ret;
  ret.reserve(c.size());
  for (const auto &x : c) {
    ret.emplace_back(f(x));
  }
  return ret;
}
#endif

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RALL(v) (v).rbegin(), (v).rend()
#define tget(t, i) get<i>(t)

#define MOD 1000000007LL
#define EPS 1e-8

const int NONE = -1010101010;

struct segtree {
  int m;
  vector<int> dat;
  segtree(int n, int init) {
    for (m = 1; m < n; m *= 2)
      ;
    dat.assign(m * 2 - 1, init);
  }
  void update(int a, int b, int v) { update(a, b, v, 0, 0, m); }
  void update(int a, int b, int v, int k, int l, int r) {
    if (r <= a || b <= l) {
      return;
    }
    if (a <= l && r <= b) {
      chmin(dat[k], v);
    } else {
      update(a, b, v, k * 2 + 1, l, (l + r) >> 1);
      update(a, b, v, k * 2 + 2, (l + r) >> 1, r);
    }
  }
  int query(int p) {
    int k = 0, l = 0, r = m, ret = dat[0];
    while (k < m - 1) {
      int x = (l + r) >> 1;
      if (p < x) {
        k = k * 2 + 1;
        r = x;
      } else {
        k = k * 2 + 2;
        l = x;
      }
      chmin(ret, dat[k]);
    }
    return ret;
  }
};

void mainmain() {
  int n, q;
  scanf("%d%d", &n, &q);
  segtree sts[2] = {segtree(n, n - 1), segtree(n, n - 1)};
  LL ans = (n - 2LL) * (n - 2LL);
  for (int i = 0; i < q; ++i) {
    int tp, x;
    scanf("%d%d", &tp, &x);
    --x;
    --tp;
    int y = sts[tp].query(x);
    ans -= y - 1;
    sts[!tp].update(0, y, x);
  }
  printf("%lld\n", ans);
}

} // namespace
int main() try {
  //	ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(4);
  mainmain();
} catch (const exception &e) {
  cerr << "exception: " << e.what() << endl;
  abort();
} catch (...) {
  cerr << "exception" << endl;
  abort();
}