Problem p02351 (C++) — buggy submission, verdict: 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, val2;
  node() : val2(0) {}
  node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), val2(0) {}
  void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
    val = val_;
    l = l_;
    r = r_;
  }
};

template <typename T> class DynamicLazySegmentTree {
  using func_t = function<T(T, T)>;
  const ll n;
  const T id;
  func_t merge;
  node<T> *root;
  vector<node<T>> pool;
  int it;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node() {
    pool[it].init(id);
    return &pool[it++];
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return id;
    if (l <= lb && ub <= r)
      return n->val + n->val2 * (ub - lb);
    return n->val2 * (min(r, ub) - max(l, lb)) +
           merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
                 n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
  }
  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->val2 += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new_node();
    }
    if (n->r == nullptr) {
      n->r = new_node();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(
      ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
      int max_ = 1000000)
      : n(size(n_)), id(id_), merge(merge_), pool(max_) {
    it = 0;
    root = new_node();
  }
  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); }
};

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

Error: MLE

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, val2;
  node() : val2(0) {}
  node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
      : l(l_), r(r_), val(val_), val2(0) {}
  void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
    val = val_;
    l = l_;
    r = r_;
  }
};

template <typename T> class DynamicLazySegmentTree {
  using func_t = function<T(T, T)>;
  const ll n;
  const T id;
  func_t merge;
  node<T> *root;
  vector<node<T>> pool;
  int it;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node() {
    pool[it].init(id);
    return &pool[it++];
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (ub <= l || r <= lb)
      return id;
    if (l <= lb && ub <= r)
      return n->val + n->val2 * (ub - lb);
    return n->val2 * (min(r, ub) - max(l, lb)) +
           merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
                 n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
  }
  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->val2 += val;
      return;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    if (n->l == nullptr) {
      n->l = new_node();
    }
    if (n->r == nullptr) {
      n->r = new_node();
    }
    suc(l, r, n->l, lb, (lb + ub) / 2, val);
    suc(l, r, n->r, (lb + ub) / 2, ub, val);
  }

public:
  DynamicLazySegmentTree(
      ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
      int max_ = 10000000)
      : n(size(n_)), id(id_), merge(merge_), pool(max_) {
    it = 0;
    root = new_node();
  }
  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); }
};

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