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

#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000000;

struct node {
  int val;
  node *l, *r;
  node() : l(nullptr), r(nullptr) {}
  node(int val) : val(val), l(nullptr), r(nullptr) {}
  void init(int v, node *left, node *right) {
    val = v;
    l = left;
    r = right;
  }
};

auto merge(int l, int r) { return min(l, r); }

const int id = INT_MAX;
int it = 0, rit = 0, siz;
node pool[MAX];
node *root[100000];

int size(int n) {
  int res = 1;
  while (res < n)
    res <<= 1;
  return res;
}

node *new_node(int val = 0, node *l = nullptr, node *r = nullptr) {
  pool[it].init(val, l, r);
  return &pool[it++];
}

node *fix(node *x, int p, int val, int lb, int ub) {
  if (p < lb || ub <= p)
    return x;
  if (lb + 1 == ub)
    return new_node(val);
  int c = (lb + ub) / 2;
  auto nl = fix(x->l, p, val, lb, c);
  auto nr = fix(x->r, p, val, c, ub);
  return new_node(merge(nl->val, nr->val), nl, nr);
}

int sub(node *x, int l, int r, int lb, int ub) {
  if (ub <= l || r <= lb)
    return id;
  if (l <= lb && ub <= r)
    return x->val;
  int c = (lb + ub) / 2;
  return merge(sub(x->l, l, r, lb, c), sub(x->r, l, r, c, ub));
}

node *update(int p, int val, node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  return root[rit++] = fix(rt, p, val, 0, siz);
}

int find(int l, int r, node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  return sub(rt, l, r + 1, 0, siz);
}

node *init(int n) {
  siz = size(n);
  for (int i = 1; i < siz * 2; i++) {
    if (i < siz) {
      pool[i].init(id, &(pool[i * 2]), &(pool[i * 2 + 1]));
    } else {
      pool[i].init(id, nullptr, nullptr);
    }
  }
  it = siz * 2;
  return root[rit++] = &pool[1];
}

void print(node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  if (rt->l == nullptr && rt->r == nullptr) {
    printf(" %d", rt->val);
    return;
  }
  print(rt->l);
  print(rt->r);
}

int main() {
  cin.sync_with_stdio(false);
  int n, q, com, x, y;
  cin >> n >> q;
  init(n);
  while (q--) {
    cin >> com >> x >> y;
    if (com) {
      printf("%d\\n", find(x, y));
    } else {
      update(x, y);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 3000000;

struct node {
  int val;
  node *l, *r;
  node() : l(nullptr), r(nullptr) {}
  node(int val) : val(val), l(nullptr), r(nullptr) {}
  void init(int v, node *left, node *right) {
    val = v;
    l = left;
    r = right;
  }
};

auto merge(int l, int r) { return min(l, r); }

const int id = INT_MAX;
int it = 0, rit = 0, siz;
node pool[MAX];
node *root[MAX];

int size(int n) {
  int res = 1;
  while (res < n)
    res <<= 1;
  return res;
}

node *new_node(int val = 0, node *l = nullptr, node *r = nullptr) {
  pool[it].init(val, l, r);
  return &pool[it++];
}

node *fix(node *x, int p, int val, int lb, int ub) {
  if (p < lb || ub <= p)
    return x;
  if (lb + 1 == ub)
    return new_node(val);
  int c = (lb + ub) / 2;
  auto nl = fix(x->l, p, val, lb, c);
  auto nr = fix(x->r, p, val, c, ub);
  return new_node(merge(nl->val, nr->val), nl, nr);
}

int sub(node *x, int l, int r, int lb, int ub) {
  if (ub <= l || r <= lb)
    return id;
  if (l <= lb && ub <= r)
    return x->val;
  int c = (lb + ub) / 2;
  return merge(sub(x->l, l, r, lb, c), sub(x->r, l, r, c, ub));
}

node *update(int p, int val, node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  return root[rit++] = fix(rt, p, val, 0, siz);
}

int find(int l, int r, node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  return sub(rt, l, r + 1, 0, siz);
}

node *init(int n) {
  siz = size(n);
  for (int i = 1; i < siz * 2; i++) {
    if (i < siz) {
      pool[i].init(id, &(pool[i * 2]), &(pool[i * 2 + 1]));
    } else {
      pool[i].init(id, nullptr, nullptr);
    }
  }
  it = siz * 2;
  return root[rit++] = &pool[1];
}

void print(node *rt = nullptr) {
  if (rt == nullptr)
    rt = root[rit - 1];
  if (rt->l == nullptr && rt->r == nullptr) {
    printf(" %d", rt->val);
    return;
  }
  print(rt->l);
  print(rt->r);
}

int main() {
  cin.sync_with_stdio(false);
  int n, q, com, x, y;
  cin >> n >> q;
  init(n);
  while (q--) {
    cin >> com >> x >> y;
    if (com) {
      printf("%d\\n", find(x, y));
    } else {
      update(x, y);
    }
  }
  return 0;
}