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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

template <typename Monoid> class SegmentTree {
private:
  using Func = function<Monoid(Monoid, Monoid)>;
  Func func;
  Monoid unity;
  int siz;
  vector<Monoid> data;
  int n;
  int height;
  inline void build() {
    siz = 1;
    while (siz < n)
      siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++)
      set(i, unity);
    for (int idx = siz - 1; idx > 0; idx--)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  template <typename T> inline void build(const T &arr) {
    if (!~n)
      n = (int)arr.size();
    siz = 1;
    while (siz < n)
      siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++)
      set(i, arr[i]);
    for (int idx = siz - 1; idx > 0; idx--)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  inline void set(int idx, const Monoid &val) {
    assert(idx + siz < (int)data.size());
    data[idx + siz] = val;
  }

public:
  SegmentTree() {}
  SegmentTree(const Func f, const Monoid &u, const int &n_)
      : func(f), unity(u), n(n_) {
    build();
  }
  template <typename T>
  SegmentTree(const T &arr, const Func f, const Monoid &u, int n_ = -1)
      : func(f), unity(u), n(n_) {
    build(arr);
  }

  void initialize(const Func f, const Monoid &u, const int &n_) {
    func = f;
    unity = u;
    n = n_;
    build();
  }
  template <typename T>
  void initialize(const T &arr, const Func f, const Monoid &u, int n_ = -1) {
    func = f;
    unity = u;
    n = n_;
    buld(arr);
  }

  void modify(int idx, const Monoid &val) {
    idx += siz;
    data[idx] = val;
    while (idx >>= 1)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  //[left, right)
  Monoid get(int left, int right) {
    if (left > right)
      swap(left, right);
    Monoid val_left = unity, val_right = unity;
    for (int l = left + siz, r = right + siz; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        val_left = func(val_left, data[l++]);
      if (r & 1)
        val_right = func(data[--r], val_right);
    }
    return func(val_left, val_right);
  }

  template <typename U>
  int find(int start, int left, int right, int cur, Monoid &val, const U &f) {
    if (left + 1 == right) {
      val = func(val, data[cur]);
      return f(val) ? cur - siz : -1;
    }
    int mid = (left + right) >> 1;
    if (mid <= start)
      return find(start, mid, right, (cur << 1) | 1, val, f);
    if (start <= left && !f(func(val, data[cur]))) {
      val = func(val, data[cur]);
      return -1;
    }
    int val_left = find(start, left, mid, (cur << 1) | 0, val, f);
    if (~val_left)
      return val_left;
    return find(start, mid, right, (cur << 1) | 1, val, f);
  }
  template <typename U> int find(int start, const U &f) {
    Monoid val = unity;
    return find(start, 0, siz, 1, val, f);
  }

  inline Monoid operator[](int idx) { return data[idx + siz]; }
  void print() {
    for (int i = 0; i < siz; i++) {
      cout << (*this)[i];
      if (i != siz - 1)
        cout << ", ";
    }
    cout << '\\n';
  }
};

// long long max
function<long long(long long, long long)> LLMAX =
    [](long long a, long long b) -> long long { return max(a, b); };
const long long LLMAX_UNITY = numeric_limits<long long>::min();
// int max
function<int(int, int)> INTMAX = [](int a, int b) -> int { return max(a, b); };
const int INTMAX_UNITY = numeric_limits<int>::min();
// long long min
function<long long(long long, long long)> LLMIN =
    [](long long a, long long b) -> long long { return min(a, b); };
const long long LLMIN_UNITY = numeric_limits<long long>::max();
// int min
function<int(int, int)> INTMIN = [](int a, int b) -> int { return min(a, b); };
const int INTMIN_UNITY = numeric_limits<int>::max();
// long long sum
function<long long(long long, long long)> LLSUM =
    [](long long a, long long b) -> long long { return a + b; };
const long long LLSUM_UNITY = 0LL;
// int sum
function<int(int, int)> INTSUM = [](int a, int b) -> int { return a + b; };
const int INTSUM_UNITY = 0;

const int N = 202020;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // f
  int n, q;
  cin >> n >> q;
  SegmentTree<int> sgt_ht(INTMIN, INTMIN_UNITY, N + 10);
  SegmentTree<int> sgt_hp(INTMIN, INTMIN_UNITY, N + 10);
  SegmentTree<int> sgt_vt(INTMIN, INTMIN_UNITY, N + 10);
  SegmentTree<int> sgt_vp(INTMIN, INTMIN_UNITY, N + 10);
  long long ans = 0;
  for (int i = 0; i < q; i++) {
    int t, c;
    cin >> t >> c;
    if (t == 1) {
      int time = sgt_ht.get(0, c);
      if (time == INTMIN_UNITY) {
        time = N + 5;
      }
      int pos = sgt_vp.get(0, time);
      if (pos == INTMIN_UNITY) {
        pos = n;
      }
      ans += pos - 2;
      sgt_ht.modify(c, i);
      sgt_hp.modify(i, c);
    } else {
      int time = sgt_vt.get(0, c);
      if (time == INTMIN_UNITY) {
        time = N + 5;
      }
      int pos = sgt_hp.get(0, time);
      if (pos == INTMIN_UNITY) {
        pos = n;
      }
      ans += pos - 2;
      sgt_vt.modify(c, i);
      sgt_vp.modify(i, c);
    }
  }
  long long tot = 1LL * (n - 2) * (n - 2);
  cout << tot - ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

template <typename Monoid> class SegmentTree {
private:
  using Func = function<Monoid(Monoid, Monoid)>;
  Func func;
  Monoid unity;
  int siz;
  vector<Monoid> data;
  int n;
  int height;
  inline void build() {
    siz = 1;
    while (siz < n)
      siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++)
      set(i, unity);
    for (int idx = siz - 1; idx > 0; idx--)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  template <typename T> inline void build(const T &arr) {
    if (!~n)
      n = (int)arr.size();
    siz = 1;
    while (siz < n)
      siz <<= 1, ++height;
    data.assign(siz << 1, unity);
    for (int i = 0; i < n; i++)
      set(i, arr[i]);
    for (int idx = siz - 1; idx > 0; idx--)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  inline void set(int idx, const Monoid &val) {
    assert(idx + siz < (int)data.size());
    data[idx + siz] = val;
  }

public:
  SegmentTree() {}
  SegmentTree(const Func f, const Monoid &u, const int &n_)
      : func(f), unity(u), n(n_) {
    build();
  }
  template <typename T>
  SegmentTree(const T &arr, const Func f, const Monoid &u, int n_ = -1)
      : func(f), unity(u), n(n_) {
    build(arr);
  }

  void initialize(const Func f, const Monoid &u, const int &n_) {
    func = f;
    unity = u;
    n = n_;
    build();
  }
  template <typename T>
  void initialize(const T &arr, const Func f, const Monoid &u, int n_ = -1) {
    func = f;
    unity = u;
    n = n_;
    buld(arr);
  }

  void modify(int idx, const Monoid &val) {
    idx += siz;
    data[idx] = val;
    while (idx >>= 1)
      data[idx] = func(data[(idx << 1) | 0], data[(idx << 1) | 1]);
  }
  //[left, right)
  Monoid get(int left, int right) {
    if (left > right)
      swap(left, right);
    Monoid val_left = unity, val_right = unity;
    for (int l = left + siz, r = right + siz; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        val_left = func(val_left, data[l++]);
      if (r & 1)
        val_right = func(data[--r], val_right);
    }
    return func(val_left, val_right);
  }

  template <typename U>
  int find(int start, int left, int right, int cur, Monoid &val, const U &f) {
    if (left + 1 == right) {
      val = func(val, data[cur]);
      return f(val) ? cur - siz : -1;
    }
    int mid = (left + right) >> 1;
    if (mid <= start)
      return find(start, mid, right, (cur << 1) | 1, val, f);
    if (start <= left && !f(func(val, data[cur]))) {
      val = func(val, data[cur]);
      return -1;
    }
    int val_left = find(start, left, mid, (cur << 1) | 0, val, f);
    if (~val_left)
      return val_left;
    return find(start, mid, right, (cur << 1) | 1, val, f);
  }
  template <typename U> int find(int start, const U &f) {
    Monoid val = unity;
    return find(start, 0, siz, 1, val, f);
  }

  inline Monoid operator[](int idx) { return data[idx + siz]; }
  void print() {
    for (int i = 0; i < siz; i++) {
      cout << (*this)[i];
      if (i != siz - 1)
        cout << ", ";
    }
    cout << '\\n';
  }
};

// long long max
function<long long(long long, long long)> LLMAX =
    [](long long a, long long b) -> long long { return max(a, b); };
const long long LLMAX_UNITY = numeric_limits<long long>::min();
// int max
function<int(int, int)> INTMAX = [](int a, int b) -> int { return max(a, b); };
const int INTMAX_UNITY = numeric_limits<int>::min();
// long long min
function<long long(long long, long long)> LLMIN =
    [](long long a, long long b) -> long long { return min(a, b); };
const long long LLMIN_UNITY = numeric_limits<long long>::max();
// int min
function<int(int, int)> INTMIN = [](int a, int b) -> int { return min(a, b); };
const int INTMIN_UNITY = numeric_limits<int>::max();
// long long sum
function<long long(long long, long long)> LLSUM =
    [](long long a, long long b) -> long long { return a + b; };
const long long LLSUM_UNITY = 0LL;
// int sum
function<int(int, int)> INTSUM = [](int a, int b) -> int { return a + b; };
const int INTSUM_UNITY = 0;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // f
  int n, q;
  cin >> n >> q;
  SegmentTree<int> sgt_ht(INTMIN, INTMIN_UNITY, n + 10);
  SegmentTree<int> sgt_hp(INTMIN, INTMIN_UNITY, n + 10);
  SegmentTree<int> sgt_vt(INTMIN, INTMIN_UNITY, n + 10);
  SegmentTree<int> sgt_vp(INTMIN, INTMIN_UNITY, n + 10);
  long long ans = 0;
  for (int i = 0; i < q; i++) {
    int t, c;
    cin >> t >> c;
    if (t == 1) {
      int time = sgt_ht.get(0, c);
      if (time == INTMIN_UNITY) {
        time = n + 5;
      }
      int pos = sgt_vp.get(0, time);
      if (pos == INTMIN_UNITY) {
        pos = n;
      }
      ans += pos - 2;
      sgt_ht.modify(c, i);
      sgt_hp.modify(i, c);
    } else {
      int time = sgt_vt.get(0, c);
      if (time == INTMIN_UNITY) {
        time = n + 5;
      }
      int pos = sgt_hp.get(0, time);
      if (pos == INTMIN_UNITY) {
        pos = n;
      }
      ans += pos - 2;
      sgt_vt.modify(c, i);
      sgt_vp.modify(i, c);
    }
  }
  long long tot = 1LL * (n - 2) * (n - 2);
  cout << tot - ans << endl;
  return 0;
}