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

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

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <class Z> Z getint() {
  char c = getchar();
  bool neg = c == '-';
  Z res = neg ? 0 : c - '0';
  while (isdigit(c = getchar()))
    res = res * 10 + (c - '0');
  return neg ? -res : res;
}
template <class Z> void putint(Z a, char c = '\\n') {
  if (a < 0)
    putchar('-'), a = -a;
  int d[40], i = 0;
  do
    d[i++] = a % 10;
  while (a /= 10);
  while (i--)
    putchar('0' + d[i]);
  putchar(c);
}

template <class T> struct FenwickTree {
  const int n;
  vector<T> t;
  FenwickTree(int _n) : n(_n), t(n + 1) {}
  void add(int i, T a) {
    for (++i; i <= n; i += i & -i)
      t[i] += a;
  }
  T sum(int i) const {
    T s = 0;
    for (; i; i -= i & -i)
      s += t[i];
    return s;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  int kth(T k) const {
    int i = 0;
    for (int w = 1 << __lg(n); w; w >>= 1)
      if (i + w <= n and t[i + w] <= k)
        k -= t[i += w];
    return i;
  }
};
template <class T> struct AddSumTree {
  const int n;
  vector<FenwickTree<T>> ft;
  AddSumTree(int _n) : n(_n), ft(2, n + 1) {}
  void add(int l, int r, T a) {
    ft[0].add(l, a), ft[0].add(r, -a);
    ft[1].add(l, l * a), ft[1].add(r, -(r * a));
  }
  T sum(int i) const { return i * ft[0].sum(i) - ft[1].sum(i); }
  T sum(int l, int r) const { return sum(r) - sum(l); }
};

int main() {
  AddSumTree<long long> ast(getint<int>());
  for (int _ = getint<int>(); _--;) {
    if (getint<int>() == 0) {
      int l = getint<int>() - 1, r = getint<int>();
      long long a = getint<long long>();
      ast.add(l, r, a);
    } else {
      int l = getint<int>() - 1, r = getint<int>();
      putint(ast.sum(l, r));
    }
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T, class U> void assign(V<T> &v, int n, const U &a) {
  v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
  v.resize(n);
  for (auto &&e : v)
    assign(e, args...);
}

template <class Int> Int rng(Int a, Int b) {
  static mt19937 mt(chrono::steady_clock().now().time_since_epoch().count());
  assert(a < b);
  return uniform_int_distribution<Int>(a, b - 1)(mt);
}

namespace RandomizedBinarySearchTree {
struct Node;
using Tree = Node *;
struct Node {
  lint val, sum, laz;
  Tree cl, cr;
  int n;
  Node() {}
  Node(lint v);
  Node(lint v, Tree cl, Tree cr)
      : val(v), sum(cl->sum + v + cr->sum), laz(0), cl(cl), cr(cr),
        n(cl->n + 1 + cr->n) {}
};
Tree nil = new Node();
Node::Node(lint v) : val(v), sum(v), laz(0), cl(nil), cr(nil), n(1) {}

Tree update(Tree t) {
  t->sum = t->cl->sum + t->val + t->cr->sum;
  t->n = t->cl->n + 1 + t->cr->n;
  return t;
}

void push(Tree t) {
  t->cl->val += t->laz;
  t->cl->sum += t->laz * t->cl->n;
  t->cl->laz += t->laz;
  t->cr->val += t->laz;
  t->cr->sum += t->laz * t->cr->n;
  t->cr->laz += t->laz;
  t->laz = 0;
}

Tree merge(Tree tl, Tree tr) {
  if (tr == nil)
    return tl;
  if (tl == nil)
    return tr;
  if (rng(0, tl->n + tr->n) < tl->n) {
    push(tl);
    return new Node(tl->val, tl->cl, merge(tl->cr, tr));
  } else {
    push(tr);
    return new Node(tr->val, merge(tl, tr->cl), tr->cr);
  }
}

pair<Tree, Tree> split(Tree t, int i) {
  if (t == nil)
    return {nil, nil};
  push(t);
  if (i <= t->cl->n) {
    Tree resl, resr;
    tie(resl, resr) = split(t->cl, i);
    resr = new Node(t->val, resr, t->cr);
    return {resl, resr};
  } else {
    Tree resl, resr;
    tie(resl, resr) = split(t->cr, i - t->cl->n - 1);
    resl = new Node(t->val, t->cl, resl);
    return {resl, resr};
  }
}

Tree insert(Tree t, int i, lint v) {
  Tree tl, tr;
  tie(tl, tr) = split(t, i);
  return merge(merge(tl, new Node(v)), tr);
}

Tree erase(Tree t, int i) {
  Tree tl, tr;
  tie(tl, tr) = split(t, i);
  tr = split(tr, 1).second;
  return merge(tl, tr);
}
} // namespace RandomizedBinarySearchTree

using namespace RandomizedBinarySearchTree;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  Tree t = nil;
  for (int i = 0; i < n; ++i) {
    t = insert(t, i, 0);
  }
  while (q--) {
    int type;
    cin >> type;
    if (type == 0) {
      int l, r, v;
      cin >> l >> r >> v, --l;
      Tree tl, tm, tr;
      tie(tl, tr) = split(t, r);
      tie(tl, tm) = split(tl, l);
      tm->val += v;
      tm->sum += v * tm->n;
      tm->laz += v;
      t = merge(merge(tl, tm), tr);
    } else {
      int l, r;
      cin >> l >> r, --l;
      Tree tl, tm, tr;
      tie(tl, tr) = split(t, r);
      tie(tl, tm) = split(tl, l);
      cout << tm->sum << '\\n';
      t = merge(merge(tl, tm), tr);
    }
  }
}