Problem p02345 (C++) — buggy submission, verdict: Runtime Error

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

template <typename T> struct node {
  T val;
  node *l, *r;
  node() {}
  node(T val) : val(val), l(nullptr), r(nullptr) {}
  void init(T v, node *left, node *right) {
    val = v;
    l = left;
    r = right;
  }
};

node<int> pool[PMAX];
node<int> *root[QMAX];

template <typename T> class PersistentSegmentTree {
  using func_t = function<T(T, T)>;
  const int n;
  const T id;
  func_t merge;
  int it, rit;
  int size(int n) {
    int res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node(int val = 0, node<T> *l = nullptr, node<T> *r = nullptr) {
    pool[it].init(val, l, r);
    return &pool[it++];
  }
  node<T> *fix(node<T> *x, int p, T 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);
  }
  T sub(node<T> *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));
  }

public:
  PersistentSegmentTree(int n_, T id_, func_t merge_)
      : n(size(n_)), id(id_), merge(merge_), rit(0) {
    for (int i = 1; i < n * 2; i++) {
      if (i < n) {
        pool[i - 1].init(id, &(pool[i * 2 - 1]), &(pool[i * 2]));
      } else {
        pool[i - 1].init(id, nullptr, nullptr);
      }
    }
    it = n * 2 - 1;
    root[rit++] = &pool[0];
  }
  node<T> *update(int p, T val, int rt = -1) {
    if (rt == -1)
      rt = rit - 1;
    assert(0 <= rt && rt < rit);
    return root[rit++] = fix(root[rt], p, val, 0, n);
  }
  T find(int l, int r, int rt = -1) {
    if (rt == -1)
      rt = rit - 1;
    assert(0 <= rt && rt < rit);
    return sub(root[rt], l, r + 1, 0, n);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q, com, x, y;
  cin >> n >> q;
  PersistentSegmentTree<int> ps(n, INT_MAX,
                                [](int l, int r) { return min(l, r); });
  while (q--) {
    cin >> com >> x >> y;
    if (com) {
      printf("%d\\n", ps.find(x, y));
    } else {
      ps.update(x, y);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

template <typename T> struct node {
  T val;
  node *l, *r;
  node() {}
  node(T val) : val(val), l(nullptr), r(nullptr) {}
  void init(T v, node *left, node *right) {
    val = v;
    l = left;
    r = right;
  }
};

template <typename T> class PersistentSegmentTree {
  using func_t = function<T(T, T)>;
  const int n;
  const T id;
  func_t merge;
  int it, rit;
  node<T> pool[PMAX];
  node<T> *root[QMAX];
  int size(int n) {
    int res = 1;
    while (res < n)
      res <<= 1;
    return res;
  }
  node<T> *new_node(int val = 0, node<T> *l = nullptr, node<T> *r = nullptr) {
    pool[it].init(val, l, r);
    return &pool[it++];
  }
  node<T> *fix(node<T> *x, int p, T 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);
  }
  T sub(node<T> *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));
  }

public:
  PersistentSegmentTree(int n_, T id_, func_t merge_)
      : n(size(n_)), id(id_), merge(merge_), rit(0) {
    for (int i = 1; i < n * 2; i++) {
      if (i < n) {
        pool[i - 1].init(id, &(pool[i * 2 - 1]), &(pool[i * 2]));
      } else {
        pool[i - 1].init(id, nullptr, nullptr);
      }
    }
    it = n * 2 - 1;
    root[rit++] = &pool[0];
  }
  node<T> *update(int p, T val, int rt = -1) {
    if (rt == -1)
      rt = rit - 1;
    assert(0 <= rt && rt < rit);
    return root[rit++] = fix(root[rt], p, val, 0, n);
  }
  T find(int l, int r, int rt = -1) {
    if (rt == -1)
      rt = rit - 1;
    assert(0 <= rt && rt < rit);
    return sub(root[rt], l, r + 1, 0, n);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q, com, x, y;
  cin >> n >> q;
  PersistentSegmentTree<int> ps(n, INT_MAX,
                                [](int l, int r) { return min(l, r); });
  while (q--) {
    cin >> com >> x >> y;
    if (com) {
      printf("%d\\n", ps.find(x, y));
    } else {
      ps.update(x, y);
    }
  }
  return 0;
}