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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
typedef int V;
#define NV 3010101
#define def 0
struct PersistentSegmentTree { //[L,R)
  V comp(V a, V b) { return a + b; }

  // -- template ---------------------
  struct S {
    int l, r;
    V x;
  } v[NV];
  int cnt = 1, n = -1;
  void init(int _n) {
    n = _n;
    rep(i, 0, NV) v[i].l = v[i].r = 0, v[i].x = def;
  }
  void add(int &root, int l, int r, int i, int val) {
    v[cnt] = v[root];
    root = cnt++;

    if (l + 1 == r) {
      v[root].x += val;
      return;
    }

    int mi = (l + r) / 2;
    if (i < mi)
      add(v[root].l, l, mi, i, val);
    else
      add(v[root].r, mi, r, i, val);

    v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
  }
  void add(int &root, int i, int val) {
    assert(0 < n);
    add(root, 0, n, i, val);
  }
  void update(int &root, int l, int r, int i, int val) {
    v[cnt] = v[root];
    root = cnt++;

    if (l + 1 == r) {
      v[root].x = val;
      return;
    }

    int mi = (l + r) / 2;
    if (i < mi)
      update(v[root].l, l, mi, i, val);
    else
      update(v[root].r, mi, r, i, val);

    v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
  }
  void update(int &root, int i, int val) {
    assert(0 < n);
    update(root, 0, n, i, val);
  }
  int query(int a, int b, int l, int r, int k) {
    assert(0 < n);
    if (v[b].x - v[a].x < k)
      return -2;
    if (l + 1 == r)
      return l;
    int mi = (l + r) / 2;
    int re = query(v[a].l, v[b].l, l, mi, k); // v[0] always 0
    if (re < 0)
      re = query(v[a].r, v[b].r, mi, r, k);
    return re;
  }

  int allget(int root) { return v[root].x; }

  // todo
  int get(int root, int l, int r, int L, int R) {
    if (l >= r)
      return def;
    if (l == L && r == R)
      return v[root].x;
    int mi = (L + R) / 2;
    V le = get(v[root].l, max(l, L), min(r, mi), L, mi);
    V ri = get(v[root].r, max(l, mi), min(r, R), mi, R);
    return comp(le, ri);
  }
  int get(int root, int l, int r) { return get(root, l, r, 0, n); }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

int N, Q;
PersistentSegmentTree pst;
int root = 0;
//---------------------------------------------------------------------------------------------------
void _main() {
  pst.init(101010);

  cin >> N >> Q;
  rep(_, 0, Q) {
    int c, x, y;
    cin >> c >> x >> y;
    x--;

    if (c == 0)
      pst.add(root, x, y);
    else
      printf("%d\\n", pst.get(root, x, y));
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
typedef int V;
#define NV 6010101
#define def 0
struct PersistentSegmentTree { //[L,R)
  V comp(V a, V b) { return a + b; }

  // -- template ---------------------
  struct S {
    int l, r;
    V x;
  } v[NV];
  int cnt = 1, n = -1;
  void init(int _n) {
    n = _n;
    rep(i, 0, NV) v[i].l = v[i].r = 0, v[i].x = def;
  }
  void add(int &root, int l, int r, int i, int val) {
    v[cnt] = v[root];
    root = cnt++;

    if (l + 1 == r) {
      v[root].x += val;
      return;
    }

    int mi = (l + r) / 2;
    if (i < mi)
      add(v[root].l, l, mi, i, val);
    else
      add(v[root].r, mi, r, i, val);

    v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
  }
  void add(int &root, int i, int val) {
    assert(0 < n);
    add(root, 0, n, i, val);
  }
  void update(int &root, int l, int r, int i, int val) {
    v[cnt] = v[root];
    root = cnt++;

    if (l + 1 == r) {
      v[root].x = val;
      return;
    }

    int mi = (l + r) / 2;
    if (i < mi)
      update(v[root].l, l, mi, i, val);
    else
      update(v[root].r, mi, r, i, val);

    v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
  }
  void update(int &root, int i, int val) {
    assert(0 < n);
    update(root, 0, n, i, val);
  }
  int query(int a, int b, int l, int r, int k) {
    assert(0 < n);
    if (v[b].x - v[a].x < k)
      return -2;
    if (l + 1 == r)
      return l;
    int mi = (l + r) / 2;
    int re = query(v[a].l, v[b].l, l, mi, k); // v[0] always 0
    if (re < 0)
      re = query(v[a].r, v[b].r, mi, r, k);
    return re;
  }

  int allget(int root) { return v[root].x; }

  // todo
  int get(int root, int l, int r, int L, int R) {
    if (l >= r)
      return def;
    if (l == L && r == R)
      return v[root].x;
    int mi = (L + R) / 2;
    V le = get(v[root].l, max(l, L), min(r, mi), L, mi);
    V ri = get(v[root].r, max(l, mi), min(r, R), mi, R);
    return comp(le, ri);
  }
  int get(int root, int l, int r) { return get(root, l, r, 0, n); }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

int N, Q;
PersistentSegmentTree pst;
int root = 0;
//---------------------------------------------------------------------------------------------------
void _main() {
  pst.init(101010);

  cin >> N >> Q;
  rep(_, 0, Q) {
    int c, x, y;
    cin >> c >> x >> y;
    x--;

    if (c == 0)
      pst.add(root, x, y);
    else
      printf("%d\\n", pst.get(root, x, y));
  }
}