Problem p02351 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

template <typename T> class node {
public:
  node<T> *l, *r;
  T val, add;
  node(T val_ = 0, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), add(0) {}
};

template <typename T> class DynamicLazySegmentTree {
  const ll n;
  node<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return 0;
    if (l <= lb && ub <= r)
      return n->val + n->add * (ub - lb);
    return n->add * (min(r, ub) - max(l, lb)) +
           (n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : 0) +
           (n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : 0);
  }
  void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return;
    if (l <= lb && ub <= r) {
      n->add += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new node<T>();
    }
    if (n->r == nullptr) {
      n->r = new node<T>();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(ll n_) : n(size(n_)) { root = new node<T>(); }
  void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
  T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};

template <typename T> class node2 {
public:
  node2<T> *l, *r;
  DynamicLazySegmentTree<T> val, add;
  node2(ll w, node2<T> *l_ = nullptr, node2<T> *r_ = nullptr)
      : l(l_), r(r_), val(w), add(w) {}
};

template <typename T> class DynamicLazySegmentTree2 {
  const ll h, w;
  node2<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub) {
    if (ub <= li || ri <= lb)
      return 0;
    if (li <= lb && ub <= ri)
      return n->val.getSum(lj, rj - 1) + n->add.getSum(lj, rj - 1) * (ub - lb);
    return n->add.getSum(lj, rj - 1) * (min(ri, ub) - max(li, lb)) +
           (n->l == nullptr ? 0
                            : sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2)) +
           (n->r == nullptr ? 0 : sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub));
  }
  void suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
    if (ub <= li || ri <= lb)
      return;
    if (li <= lb && ub <= ri) {
      n->add.add(lj, rj - 1, val);
      return;
    }
    n->val.add(lj, rj - 1, val * (min(ri, ub) - max(li, lb)));
    if (n->l == nullptr) {
      n->l = new node2<T>(w);
    }
    if (n->r == nullptr) {
      n->r = new node2<T>(w);
    }
    suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
    suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree2(ll h_, ll w_) : h(size(h_)), w(size(w_)) {
    root = new node2<T>(w);
  }
  void add(ll li, ll lj, ll ri, ll rj, T val) {
    suc(li, lj, ri + 1, rj + 1, root, 0, h, val);
  }
  T getSum(ll li, ll lj, ll ri, ll rj) {
    return sub(li, lj, ri + 1, rj + 1, root, 0, h);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  DynamicLazySegmentTree2<ll> dlst(10000000000ll, 10000000000ll);
  for (int i = 0, type, s, t, x; i < q; i++) {
    cin >> type >> s >> t;
    s--;
    t--;
    if (type) {
      printf("%lld\n", dlst.getSum(0, s, 0, t));
    } else {
      cin >> x;
      dlst.add(0, s, 0, t, x);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

template <typename T> class node {
public:
  node<T> *l, *r;
  T val, add;
  node(T val_ = 0, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), add(0) {}
};

template <typename T> class DynamicLazySegmentTree {
  const ll n;
  node<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return 0;
    if (l <= lb && ub <= r)
      return n->val + n->add * (ub - lb);
    return n->add * (min(r, ub) - max(l, lb)) +
           (n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : 0) +
           (n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : 0);
  }
  void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return;
    if (l <= lb && ub <= r) {
      n->add += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new node<T>();
    }
    if (n->r == nullptr) {
      n->r = new node<T>();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(ll n_) : n(size(n_)) { root = new node<T>(); }
  void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
  T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};

template <typename T> class node2 {
public:
  node2<T> *l, *r;
  DynamicLazySegmentTree<T> val, add;
  node2(ll w, node2<T> *l_ = nullptr, node2<T> *r_ = nullptr)
      : l(l_), r(r_), val(w), add(w) {}
};

template <typename T> class DynamicLazySegmentTree2 {
  const ll h, w;
  node2<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1ll;
    return res;
  }
  T sub(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub) {
    if (ub <= li || ri <= lb)
      return 0;
    if (li <= lb && ub <= ri)
      return n->val.getSum(lj, rj - 1) + n->add.getSum(lj, rj - 1) * (ub - lb);
    return n->add.getSum(lj, rj - 1) * (min(ri, ub) - max(li, lb)) +
           (n->l == nullptr ? 0
                            : sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2)) +
           (n->r == nullptr ? 0 : sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub));
  }
  void suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
    if (ub <= li || ri <= lb)
      return;
    if (li <= lb && ub <= ri) {
      n->add.add(lj, rj - 1, val);
      return;
    }
    n->val.add(lj, rj - 1, val * (min(ri, ub) - max(li, lb)));
    if (n->l == nullptr) {
      n->l = new node2<T>(w);
    }
    if (n->r == nullptr) {
      n->r = new node2<T>(w);
    }
    suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
    suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree2(ll h_, ll w_) : h(size(h_)), w(size(w_)) {
    root = new node2<T>(w);
  }
  void add(ll li, ll lj, ll ri, ll rj, T val) {
    suc(li, lj, ri + 1, rj + 1, root, 0, h, val);
  }
  T getSum(ll li, ll lj, ll ri, ll rj) {
    return sub(li, lj, ri + 1, rj + 1, root, 0, h);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  DynamicLazySegmentTree2<ll> dlst(1000000ll, 1000000ll);
  for (int i = 0, type, s, t, x; i < q; i++) {
    cin >> type >> s >> t;
    s--;
    t--;
    if (type) {
      printf("%lld\n", dlst.getSum(0, s, 0, t));
    } else {
      cin >> x;
      dlst.add(0, s, 0, t, x);
    }
  }
  return 0;
}