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

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

template <typename T> class BinaryIndexedTree2D {
  vector<vector<ll>> vec;
  const ll m, n;

public:
  BinaryIndexedTree2D(ll _m, ll _n)
      : vec(_m + 1, vector<T>(_n + 1)), m(_m), n(_n) {}

  T query(ll y, ll x) { /* query for [(1, 1), (y, x)] */
    T ret = 0;
    for (ll i = y; i > 0; i -= i & (-i))
      for (ll j = x; j > 0; j -= j & (-j))
        ret += vec[i][j];
    return ret;
  }

  void update(ll y, ll x, T k) { /* update for [(1, 1,), (y, x)] */
    for (ll i = y; i <= m; i += i & (-i))
      for (ll j = x; j <= n; j += j & (-j))
        vec[i][j] += k;
  }
};

struct event {
  ll t, c, h1, w1, h2, w2;

public:
  bool operator<(const event &e) const { return t > e.t; }
};

ll H, W, T, Q;

int main(void) {
  priority_queue<event> q;

  cin >> H >> W >> T >> Q;
  REP(i, 0, Q) {
    ll t, c, h1, w1, h2, w2;
    cin >> t >> c >> h1 >> w1;
    if (c == 2)
      cin >> h2 >> w2;
    else
      h2 = 0, w2 = 0;
    q.push((event){t * 2, c, h1, w1, h2, w2});
  }

  BinaryIndexedTree2D<ll> bit1(H, W);
  BinaryIndexedTree2D<ll> bit2(H, W);
  ll f[H + 1][W + 1];
  REP(i, 1, H + 1) REP(j, 1, W + 1) f[i][j] = 0;

  while (!q.empty()) {
    event e = q.top();
    q.pop();

    // cout << e.t << " " << e.c << " " << e.h1 << " " << e.w1 << " " << e.h2 <<
    // " " << e.w2 << endl;

    if (e.c == 0) { // set
      bit1.update(e.h1, e.w1, 1);
      q.push((event){e.t + T * 2 - 1, 3, e.h1, e.w1, 0, 0});
      f[e.h1][e.w1] = 1;
    }
    if (e.c == 1) { // remove
      if (f[e.h1][e.w1] == 2) {
        bit2.update(e.h1, e.w1, -1);
        f[e.h1][e.w1] = 0;
      }
    }
    if (e.c == 2) { // query
      ll ans1 = bit1.query(e.h2, e.w2) - bit1.query(e.h1 - 1, e.w2) -
                bit1.query(e.h2, e.w1 - 1) + bit1.query(e.h1 - 1, e.w1 - 1);
      ll ans2 = bit2.query(e.h2, e.w2) - bit2.query(e.h1 - 1, e.w2) -
                bit2.query(e.h2, e.w1 - 1) + bit2.query(e.h1 - 1, e.w1 - 1);
      cout << ans2 << " " << ans1 << endl;
    }
    if (e.c == 3) { // complete
      if (f[e.h1][e.w1] == 1) {
        bit1.update(e.h1, e.w1, -1);
        bit2.update(e.h1, e.w1, 1);
        f[e.h1][e.w1] = 2;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

template <typename T> class BinaryIndexedTree2D {
  vector<vector<ll>> vec;
  const ll m, n;

public:
  BinaryIndexedTree2D(ll _m, ll _n)
      : vec(_m + 1, vector<T>(_n + 1)), m(_m), n(_n) {}

  T query(ll y, ll x) { /* query for [(1, 1), (y, x)] */
    T ret = 0;
    for (ll i = y; i > 0; i -= i & (-i))
      for (ll j = x; j > 0; j -= j & (-j))
        ret += vec[i][j];
    return ret;
  }

  void update(ll y, ll x, T k) { /* update for [(1, 1,), (y, x)] */
    for (ll i = y; i <= n; i += i & (-i))
      for (ll j = x; j <= m; j += j & (-j))
        vec[i][j] += k;
  }
};

struct event {
  ll t, c, h1, w1, h2, w2;

public:
  bool operator<(const event &e) const { return t > e.t; }
};

ll H, W, T, Q;

int main(void) {
  priority_queue<event> q;

  cin >> H >> W >> T >> Q;
  REP(i, 0, Q) {
    ll t, c, h1, w1, h2, w2;
    cin >> t >> c >> h1 >> w1;
    if (c == 2)
      cin >> h2 >> w2;
    else
      h2 = 0, w2 = 0;
    q.push((event){t * 2, c, h1, w1, h2, w2});
  }

  BinaryIndexedTree2D<ll> bit1(H, W);
  BinaryIndexedTree2D<ll> bit2(H, W);
  ll f[H + 1][W + 1];
  REP(i, 1, H + 1) REP(j, 1, W + 1) f[i][j] = 0;

  while (!q.empty()) {
    event e = q.top();
    q.pop();

    // cout << e.t << " " << e.c << " " << e.h1 << " " << e.w1 << " " << e.h2 <<
    // " " << e.w2 << endl;

    if (e.c == 0) { // set
      bit1.update(e.h1, e.w1, 1);
      q.push((event){e.t + T * 2 - 1, 3, e.h1, e.w1, 0, 0});
      f[e.h1][e.w1] = 1;
    }
    if (e.c == 1) { // remove
      if (f[e.h1][e.w1] == 2) {
        bit2.update(e.h1, e.w1, -1);
        f[e.h1][e.w1] = 0;
      }
    }
    if (e.c == 2) { // query
      ll ans1 = bit1.query(e.h2, e.w2) - bit1.query(e.h1 - 1, e.w2) -
                bit1.query(e.h2, e.w1 - 1) + bit1.query(e.h1 - 1, e.w1 - 1);
      ll ans2 = bit2.query(e.h2, e.w2) - bit2.query(e.h1 - 1, e.w2) -
                bit2.query(e.h2, e.w1 - 1) + bit2.query(e.h1 - 1, e.w1 - 1);
      cout << ans2 << " " << ans1 << endl;
    }
    if (e.c == 3) { // complete
      if (f[e.h1][e.w1] == 1) {
        bit1.update(e.h1, e.w1, -1);
        bit2.update(e.h1, e.w1, 1);
        f[e.h1][e.w1] = 2;
      }
    }
  }
}