Problem p01942 (C++) — buggy submission, verdict: Memory Limit Exceeded

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

template <typename Monoid> class Dim2SegTree {
  using T = typename Monoid::value_type;

  Monoid m;

  vector<vector<T>> tree; // 1-indexed
  int sizeH = 1, sizeW = 1;

public:
  Dim2SegTree(const int &h = 0, const int &w = 0) {
    while (sizeH < h)
      sizeH *= 2;
    while (sizeW < w)
      sizeW *= 2;
    tree.assign(sizeH * 2, vector<T>(sizeW * 2, m.id()));
  }

  template <typename InputIterator>
  Dim2SegTree(InputIterator first, InputIterator last) {
    int h = distance(first, last), w = first->size();
    while (sizeH < h)
      sizeH *= 2;
    while (sizeW < w)
      sizeW *= 2;
    tree.assign(sizeH * 2, vector<T>(sizeW * 2, m.id()));
    int i = 0;
    for (auto iter = first; iter != last; iter++, i++) {
      int j = 0;
      for (auto jter = iter->begin(); jter != iter->end(); jter++, j++)
        tree[sizeH + i][sizeW + j] = *jter;
    }
    rep(i, sizeH) for (int j = sizeW - 1; j >= 1; j--) tree[sizeH + i][j] =
        m(tree[sizeH + i][j * 2], tree[sizeH + i][j * 2 + 1]);
    for (int i = sizeH - 1; i >= 1; i--)
      loop(j, 1, 2 * sizeW) tree[i][j] = m(tree[i * 2][j], tree[i * 2 + 1][j]);
  }

  T foldW(const int &i, int l, int r) { // [l, r)
    T accl = m.id(), accr = m.id();
    for (l += sizeW, r += sizeW; l < r; l /= 2, r /= 2) {
      if (l & 1)
        accl = m(accl, tree[i][l++]);
      if (r & 1)
        accr = m(tree[i][--r], accr);
    }
    return m(accl, accr);
  }

  T fold(int t, int b, const int &l, const int &r) { // [t, b), [l, r)
    T acct = m.id(), accb = m.id();
    for (t += sizeH, b += sizeH; t < b; t /= 2, b /= 2) {
      if (t & 1)
        acct = m(acct, foldW(t++, l, r));
      if (b & 1)
        accb = m(foldW(--b, l, r), accb);
    }
    return m(acct, accb);
  }

  void update(int i, int j, const T &x) {
    tree[i += sizeH][j += sizeW] = x;
    for (int _j = j / 2; _j; _j /= 2)
      tree[i][_j] = m(tree[i][_j * 2], tree[i][_j * 2 + 1]);
    for (int _i = i / 2; _i; _i /= 2) {
      for (int _j = j; _j; _j /= 2)
        tree[_i][_j] = m(tree[_i * 2][_j], tree[_i * 2 + 1][_j]);
    }
  }

  const T &get(int i, int j) const { return tree[sizeH + i][sizeW + j]; }
};

template <typename T> struct sumMonoid {
  using value_type = T;
  value_type id() { return 0; }
  value_type operator()(const value_type &a, const value_type &b) {
    return a + b;
  }
};

const int Z = 100000;

main {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int H, W, T, Q;
  cin >> H >> W >> T >> Q;
  Dim2SegTree<sumMonoid<int>> st(H, W);
  queue<tuple<int, int, int>> q; // time, h, w
  while (Q--) {
    int t = in();
    while (q.size() && get<0>(q.front()) <= t) {
      int h = get<1>(q.front()), w = get<2>(q.front());
      q.pop();
      st.update(h, w, st.get(h, w) - 1 + Z);
    }
    int c = in();
    if (c == 0) {
      int h, w;
      cin >> h >> w;
      --h, --w;
      st.update(h, w, st.get(h, w) + 1);
      q.emplace(t + T, h, w);
    }
    if (c == 1) {
      int h, w;
      cin >> h >> w;
      --h, --w;
      st.update(h, w, st.get(h, w) % Z);
    }
    if (c == 2) {
      int h1, w1, h2, w2;
      cin >> h1 >> w1 >> h2 >> w2;
      --h1, --w1;
      cout << st.fold(h1, h2, w1, w2) / Z << ' ' << st.fold(h1, h2, w1, w2) % Z
           << endl;
    }
  }
}


Error: MLE

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

template <typename Monoid> class Dim2SegTree {
  using T = typename Monoid::value_type;

  Monoid m;

  vector<vector<T>> tree; // 1-indexed
  int sizeH = 1, sizeW = 1;

public:
  Dim2SegTree(const int &h = 0, const int &w = 0) {
    while (sizeH < h)
      sizeH *= 2;
    while (sizeW < w)
      sizeW *= 2;
    tree.assign(sizeH * 2, vector<T>(sizeW * 2, m.id()));
  }

  template <typename InputIterator>
  Dim2SegTree(InputIterator first, InputIterator last) {
    int h = distance(first, last), w = first->size();
    while (sizeH < h)
      sizeH *= 2;
    while (sizeW < w)
      sizeW *= 2;
    tree.assign(sizeH * 2, vector<T>(sizeW * 2, m.id()));
    int i = 0;
    for (auto iter = first; iter != last; iter++, i++) {
      int j = 0;
      for (auto jter = iter->begin(); jter != iter->end(); jter++, j++)
        tree[sizeH + i][sizeW + j] = *jter;
    }
    rep(i, sizeH) for (int j = sizeW - 1; j >= 1; j--) tree[sizeH + i][j] =
        m(tree[sizeH + i][j * 2], tree[sizeH + i][j * 2 + 1]);
    for (int i = sizeH - 1; i >= 1; i--)
      loop(j, 1, 2 * sizeW) tree[i][j] = m(tree[i * 2][j], tree[i * 2 + 1][j]);
  }

  T foldW(const int &i, int l, int r) { // [l, r)
    T accl = m.id(), accr = m.id();
    for (l += sizeW, r += sizeW; l < r; l /= 2, r /= 2) {
      if (l & 1)
        accl = m(accl, tree[i][l++]);
      if (r & 1)
        accr = m(tree[i][--r], accr);
    }
    return m(accl, accr);
  }

  T fold(int t, int b, const int &l, const int &r) { // [t, b), [l, r)
    T acct = m.id(), accb = m.id();
    for (t += sizeH, b += sizeH; t < b; t /= 2, b /= 2) {
      if (t & 1)
        acct = m(acct, foldW(t++, l, r));
      if (b & 1)
        accb = m(foldW(--b, l, r), accb);
    }
    return m(acct, accb);
  }

  void update(int i, int j, const T &x) {
    tree[i += sizeH][j += sizeW] = x;
    for (int _j = j / 2; _j; _j /= 2)
      tree[i][_j] = m(tree[i][_j * 2], tree[i][_j * 2 + 1]);
    for (int _i = i / 2; _i; _i /= 2) {
      for (int _j = j; _j; _j /= 2)
        tree[_i][_j] = m(tree[_i * 2][_j], tree[_i * 2 + 1][_j]);
    }
  }

  const T &get(int i, int j) const { return tree[sizeH + i][sizeW + j]; }
};

template <typename T> struct sumMonoid {
  using value_type = T;
  value_type id() { return 0; }
  value_type operator()(const value_type &a, const value_type &b) {
    return a + b;
  }
};

main {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int H, W, T, Q;
  cin >> H >> W >> T >> Q;
  Dim2SegTree<sumMonoid<int>> inProgress(H, W), done(H, W);
  queue<tuple<int, int, int>> q; // time, h, w
  while (Q--) {
    int t = in();
    while (q.size() && get<0>(q.front()) <= t) {
      int h = get<1>(q.front()), w = get<2>(q.front());
      q.pop();
      inProgress.update(h, w, 0);
      done.update(h, w, 1);
    }
    int c = in();
    if (c == 0) {
      int h, w;
      cin >> h >> w;
      --h, --w;
      inProgress.update(h, w, 1);
      q.emplace(t + T, h, w);
    }
    if (c == 1) {
      int h, w;
      cin >> h >> w;
      --h, --w;
      done.update(h, w, 0);
    }
    if (c == 2) {
      int h1, w1, h2, w2;
      cin >> h1 >> w1 >> h2 >> w2;
      --h1, --w1;
      cout << done.fold(h1, h2, w1, w2) << ' '
           << inProgress.fold(h1, h2, w1, w2) << endl;
    }
  }
}