Problem p02351 (C++) — buggy submission, verdict: Time 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 RARS {
  const ll n;
  node<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (!n || ub <= l || r <= lb)
      return 0;
    if (l <= lb && ub <= r)
      return n->val + n->add * (ub - lb);
    return sub(l, r, n->l, lb, (lb + ub) / 2) +
           sub(l, r, n->r, (lb + ub) / 2, ub) +
           n->add * (min(r, ub) - max(l, lb));
  }
  node<T> *suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return n;
    if (!n)
      n = new node<T>;
    if (l <= lb && ub <= r) {
      n->add += val;
      return n;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    n->l = suc(l, r, n->l, lb, (lb + ub) / 2, val);
    n->r = suc(l, r, n->r, (lb + ub) / 2, ub, val);
    return n;
  }

public:
  RARS(ll n_) : n(size(n_)), root(nullptr) {}
  void add(ll l, ll r, T val) { root = 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:
  RARS<T> val, add;
  node2<T> *l, *r;
  node2(ll size) : val(size), add(size), l(nullptr), r(nullptr) {}
};

template <typename T> class RARS2 {
  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 (!n || ub <= li || ri <= lb)
      return 0;
    if (li <= lb && ub <= rj)
      return n->val.getSum(lj, rj) + n->add.getSum(lj, rj) * (ub - lb);
    return sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2) +
           sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub) +
           n->add.getSum(lj, rj) * (min(ri, ub) - max(li, lb));
  }
  node2<T> *suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
    if (ub <= li || ri <= lb)
      return n;
    if (!n)
      n = new node2<T>(w);
    if (li <= lb && ub <= ri) {
      n->add.add(lj, rj, val);
      return n;
    }
    n->val.add(lj, rj, val * (min(ri, ub) - max(li, lb)));
    n->l = suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
    n->r = suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
    return n;
  }

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

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

Error: TLE

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 RARS {
  const ll n;
  node<T> *root;
  ll size(ll n) {
    ll res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
    if (!n || ub <= l || r <= lb)
      return 0;
    if (l <= lb && ub <= r)
      return n->val + n->add * (ub - lb);
    return sub(l, r, n->l, lb, (lb + ub) / 2) +
           sub(l, r, n->r, (lb + ub) / 2, ub) +
           n->add * (min(r, ub) - max(l, lb));
  }
  node<T> *suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
    if (ub <= l || r <= lb)
      return n;
    if (!n)
      n = new node<T>;
    if (l <= lb && ub <= r) {
      n->add += val;
      return n;
    }
    n->val += val * (min(r, ub) - max(l, lb));
    n->l = suc(l, r, n->l, lb, (lb + ub) / 2, val);
    n->r = suc(l, r, n->r, (lb + ub) / 2, ub, val);
    return n;
  }

public:
  RARS(ll n_) : n(size(n_)), root(nullptr) {}
  void add(ll l, ll r, T val) { root = 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:
  RARS<T> val, add;
  node2<T> *l, *r;
  node2(ll size) : val(size), add(size), l(nullptr), r(nullptr) {}
};

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

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

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