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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(kbrni, n) cout << " " << a[kbrni];                                       \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

template <typename T> class dynamic_segtree {
private:
  struct node {
    T val, lazy;
    int st_size;
    node *par, *left, *right;
    node()
        : val(identity), lazy(identity), st_size(0), par(nullptr),
          left(nullptr), right(nullptr) {}
  };
  void opr1(T &arg1, T arg2) { arg1 += arg2; }
  T opr2(T arg1, T arg2) { return arg1 + arg2; }
  static const int POOL_SIZE = 4000000;
  static node *root;
  static node pool[POOL_SIZE];
  static const T identity = 0;
  int nw_size;
  node *alloc() {
    assert(nw_size < POOL_SIZE);
    return (&pool[nw_size++]);
  }
  void eval(node *k, int l, int r) {
    if (k->lazy != 0) {
      opr1(k->val, k->lazy);
      if (r - l > 1) {
        if (k->left) {
          opr1(k->left->lazy, k->lazy / k->st_size * k->left->st_size);
        }
        if (k->right) {
          opr1(k->right->lazy, k->lazy / k->st_size * k->right->st_size);
        }
      }
      k->lazy = 0;
    }
  }

public:
  static const int MAX_SIZE = 100001;
  dynamic_segtree() {
    nw_size = 0;
    root = alloc();
  }
  void insert(int a, T x, node *k = root, int l = 0, int r = MAX_SIZE) {
    eval(k, l, r);
    k->val += x;
    k->st_size++;
    if (r - l == 1) {
      return;
    }
    if (a < (l + r) / 2) {
      if (!(k->left)) {
        k->left = alloc();
        k->left->par = k;
      }
      insert(a, x, k->left, l, (l + r) / 2);
    } else {
      if (!(k->right)) {
        k->right = alloc();
        k->right->par = k;
      }
      insert(a, x, k->right, (l + r) / 2, r);
    }
  }
  void range(int a, int b, const T x, node *k = root, int l = 0,
             int r = MAX_SIZE) {
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      k->lazy += k->st_size * x;
      eval(k, l, r);
      return;
    }
    k->val = 0;
    if (k->left) {
      range(a, b, x, k->left, l, (l + r) / 2);
      opr1(k->val, k->left->val);
    }
    if (k->right) {
      range(a, b, x, k->right, (l + r) / 2, r);
      opr1(k->val, k->right->val);
    }
  }
  T query(int a, int b, node *k = root, int l = 0, int r = MAX_SIZE) {
    eval(k, l, r);
    if (b <= l || r <= a) {
      return identity;
    }
    if (a <= l && r <= b) {
      return k->val;
    }
    T vl = identity, vr = identity;
    if (k->left) {
      vl = query(a, b, k->left, l, (l + r) / 2);
    }
    if (k->right) {
      vr = query(a, b, k->right, (l + r) / 2, r);
    }
    return opr2(vl, vr);
  }
};
template <typename T> class dynamic_segtree<T>::node *dynamic_segtree<T>::root;
template <typename T>
class dynamic_segtree<T>::node dynamic_segtree<T>::pool[POOL_SIZE];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  dynamic_segtree<ll> sg;
  rep(i, n) { sg.insert(i, 0); }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      sg.range(s - 1, t, x);
    } else {
      int s, t;
      cin >> s >> t;
      cout << sg.query(s - 1, t) << "\\n";
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(kbrni, n) cout << " " << a[kbrni];                                       \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(kbrni, v) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

template <typename T> class dynamic_segtree {
private:
  struct node {
    T val, lazy;
    int st_size;
    node *par, *left, *right;
    node()
        : val(identity), lazy(identity), st_size(1), par(nullptr),
          left(nullptr), right(nullptr) {}
  };
  void opr1(T &arg1, T &arg2) { arg1 += arg2; }
  T opr2(T arg1, T arg2) { return arg1 + arg2; }
  static const int POOL_SIZE = 8000000;
  static node *root;
  static node pool[POOL_SIZE];
  static const T identity = 0;
  int nw_size;
  node *alloc() {
    assert(nw_size < POOL_SIZE);
    return (&pool[nw_size++]);
  }
  void eval(node *k, int l, int r) {
    if (k->lazy != 0) {
      opr1(k->val, k->lazy);
      if (r - l > 1) {
        if (k->left) {
          opr1(k->left->lazy, k->lazy);
        }
        if (k->right) {
          opr1(k->right->lazy, k->lazy);
        }
      }
      k->lazy = 0;
    }
  }

public:
  static const int MAX_SIZE = 10;
  dynamic_segtree() {
    nw_size = 0;
    root = alloc();
  }
  void insert(int a, T x, node *k = root, int l = 0, int r = MAX_SIZE) {
    k->val += x;
    if (r - l == 1) {
      return;
    }
    k->st_size++;
    eval(k, l, r);
    if (a < (l + r) / 2) {
      if (!(k->left)) {
        k->left = alloc();
        k->left->par = k;
      }
      insert(a, x, k->left, l, (l + r) / 2);
    } else {
      if (!(k->right)) {
        k->right = alloc();
        k->right->par = k;
      }
      insert(a, x, k->right, (l + r) / 2, r);
    }
  }
  void range(int a, int b, const T x, node *k = root, int l = 0,
             int r = MAX_SIZE) {
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      k->lazy += k->st_size * x;
      eval(k, l, r);
      return;
    }
    if (!(k->left)) {
      k->left = alloc();
      k->left->par = k;
    }
    range(a, b, x, k->left, l, (l + r) / 2);
    if (!(k->right)) {
      k->right = alloc();
      k->right->par = k;
    }
    range(a, b, x, k->right, (l + r) / 2, r);
  }
  T query(int a, int b, node *k = root, int l = 0, int r = MAX_SIZE) {
    eval(k, l, r);
    if (b <= l || r <= a) {
      return identity;
    }
    if (a <= l && r <= b) {
      return k->val;
    }
    T vl = identity, vr = identity;
    if (k->left) {
      vl = query(a, b, k->left, l, (l + r) / 2);
    }
    if (k->right) {
      vr = query(a, b, k->right, (l + r) / 2, r);
    }
    return opr2(vl, vr);
  }
};
template <typename T> class dynamic_segtree<T>::node *dynamic_segtree<T>::root;
template <typename T>
class dynamic_segtree<T>::node dynamic_segtree<T>::pool[POOL_SIZE];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  dynamic_segtree<int> sg;
  rep(i, n) { sg.insert(i, 0); }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      sg.range(s, t + 1, x);
    } else {
      int s, t;
      cin >> s >> t;
      cout << sg.query(s, t + 1) << "\\n";
    }
  }
  return 0;
}