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

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template <class T> struct ArrayPool {
  vector<T> pool;
  vector<T *> stock;
  int ptr;

  ArrayPool(int sz) : pool(sz), stock(sz) {}

  inline T *alloc() { return stock[--ptr]; }

  inline void free(T *t) { stock[ptr++] = t; }

  void clear() {
    ptr = (int)pool.size();
    for (int i = 0; i < pool.size(); i++)
      stock[i] = &pool[i];
  }
};

template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree {

  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  using P = function<OperatorMonoid(OperatorMonoid, int)>;

  enum COLOR { BLACK, RED };

  struct Node {
    Node *l, *r;
    COLOR color;
    int level, cnt;
    Monoid key, sum;
    OperatorMonoid lazy;

    Node() {}

    Node(const Monoid &k, const OperatorMonoid &p)
        : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0),
          cnt(1), lazy(p) {}

    Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p)
        : key(k), color(RED), l(l), r(r), lazy(p) {}
  };

  ArrayPool<Node> pool;

  const Monoid M1;
  const OperatorMonoid OM0;
  const F f;
  const G g;
  const H h;
  const P p;

  RedBlackTree(int sz, const F &f, const Monoid &M1)
      : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {}

  RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p,
               const Monoid &M1, const OperatorMonoid &OM0)
      : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {}

  inline Node *alloc(const Monoid &key) {
    return &(*pool.alloc() = Node(key, OM0));
  }

  inline Node *alloc(Node *l, Node *r) {
    auto t = &(*pool.alloc() = Node(l, r, M1, OM0));
    return update(t);
  }

  virtual Node *clone(Node *t) { return t; }

  inline int count(const Node *t) { return t ? t->cnt : 0; }

  inline Monoid sum(const Node *t) { return t ? t->sum : M1; }

  Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r);
    t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0;
    t->sum = f(f(sum(t->l), t->key), sum(t->r));
    return t;
  }

  Node *propagate(Node *t) {
    t = clone(t);
    if (t->lazy != OM0) {
      if (!t->l) {
        t->key = g(t->key, t->lazy);
      } else {
        if (t->l) {
          t->l = clone(t->l);
          t->l->lazy = h(t->l->lazy, t->lazy);
          t->l->sum = g(t->l->sum, p(t->lazy, count(t->l)));
        }
        if (t->r) {
          t->r = clone(t->r);
          t->r->lazy = h(t->r->lazy, t->lazy);
          t->r->sum = g(t->r->sum, p(t->lazy, count(t->r)));
        }
      }
      t->lazy = OM0;
    }
    return update(t);
  }

  Node *rotate(Node *t, bool b) {
    t = propagate(t);
    Node *s;
    if (b) {
      s = propagate(t->l);
      t->l = s->r;
      s->r = t;
    } else {
      s = propagate(t->r);
      t->r = s->l;
      s->l = t;
    }
    update(t);
    return update(s);
  }

  Node *submerge(Node *l, Node *r) {
    if (l->level < r->level) {
      r = propagate(r);
      Node *c = (r->l = submerge(l, r->l));
      if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) {
        r->color = RED;
        c->color = BLACK;
        if (r->r->color == BLACK)
          return rotate(r, true);
        r->r->color = BLACK;
      }
      return update(r);
    }
    if (l->level > r->level) {
      l = propagate(l);
      Node *c = (l->r = submerge(l->r, r));
      if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) {
        l->color = RED;
        c->color = BLACK;
        if (l->l->color == BLACK)
          return rotate(l, false);
        l->l->color = BLACK;
      }
      return update(l);
    }
    return alloc(l, r);
  }

  Node *merge(Node *l, Node *r) {
    if (!l || !r)
      return l ? l : r;
    Node *c = submerge(l, r);
    c->color = BLACK;
    return c;
  }

  pair<Node *, Node *> split(Node *t, int k) {
    if (!t)
      return {nullptr, nullptr};
    t = propagate(t);
    if (k == 0)
      return {nullptr, t};
    if (k >= count(t))
      return {t, nullptr};
    if (k < count(t->l)) {
      auto p = split(t->l, k);
      return {p.first, merge(p.second, t->r)};
    }
    if (k > count(t->l)) {
      auto p = split(t->r, k - count(t->l));
      return {merge(t->l, p.first), p.second};
    }
    pair<Node *, Node *> ret = {t->l, t->r};
    pool.free(t);
    return ret;
  }

  Node *build(int l, int r, const vector<Monoid> &v) {
    if (l + 1 >= r)
      return alloc(v[l]);
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

  Node *build(const vector<Monoid> &v) {
    pool.clear();
    return build(0, (int)v.size(), v);
  }

  void dump(Node *r, typename vector<Monoid>::iterator &it) {
    r = propagate(r);
    if (!r->l || !r->r) {
      *it++ = r->key;
      return;
    }
    dump(r->l, it);
    dump(r->r, it);
  }

  vector<Monoid> dump(Node *r) {
    vector<Monoid> v((size_t)count(r));
    auto it = begin(v);
    dump(r, it);
    return v;
  }

  string to_string(Node *r) {
    auto s = dump(r);
    string ret;
    for (int i = 0; i < s.size(); i++) {
      ret += std::to_string(s[i]);
      ret += ", ";
    }
    return (ret);
  }

  void insert(Node *&t, int k, const Monoid &v) {
    auto x = split(t, k);
    t = merge(merge(x.first, alloc(v)), x.second);
  }

  void erase(Node *&t, int k) {
    auto x = split(t, k);
    auto y = split(x.second, 1);
    pool.free(y.first);
    t = merge(x.first, y.second);
  }

  Monoid query(Node *&t, int a, int b) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    auto ret = sum(y.first);
    t = merge(x.first, merge(y.first, y.second));
    return ret;
  }

  void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    y.first->lazy = h(y.first->lazy, p);
    t = merge(x.first, merge(propagate(y.first), y.second));
  }

  void set_element(Node *&t, int k, const Monoid &x) {
    if (!t->l) {
      t->key = t->sum = x;
      return;
    }
    t = propagate(t);
    if (k < count(t->l))
      set_element(t->l, k, x);
    else
      set_element(t->r, k - count(t->l), x);
    t = update(t);
  }

  int size(Node *t) { return count(t); }

  bool empty(Node *t) { return !t; }

  Node *makeset() { return (nullptr); }
};

using int64 = long long;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &v[i]);

  auto f = [](int a, int b) { return min(a, b); };
  const int INF = 1 << 30;

  RedBlackTree<int, int> rbt(5400000, f, INF);
  auto root = rbt.build(v);
  for (int i = 0; i < q; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    if (x == 0) {
      int v = rbt.query(root, z, z + 1);
      rbt.erase(root, z);
      rbt.insert(root, y, v);
    } else if (x == 1) {
      printf("%d\\n", rbt.query(root, y, z + 1));
    } else if (x == 2) {
      rbt.set_element(root, y, z);
    }
    if (rbt.pool.ptr < 100)
      root = rbt.build(rbt.dump(root));
  }
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template <class T> struct ArrayPool {
  vector<T> pool;
  vector<T *> stock;
  int ptr;

  ArrayPool(int sz) : pool(sz), stock(sz) {}

  inline T *alloc() { return stock[--ptr]; }

  inline void free(T *t) { stock[ptr++] = t; }

  void clear() {
    ptr = (int)pool.size();
    for (int i = 0; i < pool.size(); i++)
      stock[i] = &pool[i];
  }
};

template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree {

  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  using P = function<OperatorMonoid(OperatorMonoid, int)>;

  enum COLOR { BLACK, RED };

  struct Node {
    Node *l, *r;
    COLOR color;
    int level, cnt;
    Monoid key, sum;
    OperatorMonoid lazy;

    Node() {}

    Node(const Monoid &k, const OperatorMonoid &p)
        : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0),
          cnt(1), lazy(p) {}

    Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p)
        : key(k), color(RED), l(l), r(r), lazy(p) {}
  };

  ArrayPool<Node> pool;

  const Monoid M1;
  const OperatorMonoid OM0;
  const F f;
  const G g;
  const H h;
  const P p;

  RedBlackTree(int sz, const F &f, const Monoid &M1)
      : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {}

  RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p,
               const Monoid &M1, const OperatorMonoid &OM0)
      : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {}

  inline Node *alloc(const Monoid &key) {
    return &(*pool.alloc() = Node(key, OM0));
  }

  inline Node *alloc(Node *l, Node *r) {
    auto t = &(*pool.alloc() = Node(l, r, M1, OM0));
    return update(t);
  }

  virtual Node *clone(Node *t) { return t; }

  inline int count(const Node *t) { return t ? t->cnt : 0; }

  inline Monoid sum(const Node *t) { return t ? t->sum : M1; }

  Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r);
    t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0;
    t->sum = f(f(sum(t->l), t->key), sum(t->r));
    return t;
  }

  Node *propagate(Node *t) {
    t = clone(t);
    if (t->lazy != OM0) {
      if (!t->l) {
        t->key = g(t->key, t->lazy);
      } else {
        if (t->l) {
          t->l = clone(t->l);
          t->l->lazy = h(t->l->lazy, t->lazy);
          t->l->sum = g(t->l->sum, p(t->lazy, count(t->l)));
        }
        if (t->r) {
          t->r = clone(t->r);
          t->r->lazy = h(t->r->lazy, t->lazy);
          t->r->sum = g(t->r->sum, p(t->lazy, count(t->r)));
        }
      }
      t->lazy = OM0;
    }
    return update(t);
  }

  Node *rotate(Node *t, bool b) {
    t = propagate(t);
    Node *s;
    if (b) {
      s = propagate(t->l);
      t->l = s->r;
      s->r = t;
    } else {
      s = propagate(t->r);
      t->r = s->l;
      s->l = t;
    }
    update(t);
    return update(s);
  }

  Node *submerge(Node *l, Node *r) {
    if (l->level < r->level) {
      r = propagate(r);
      Node *c = (r->l = submerge(l, r->l));
      if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) {
        r->color = RED;
        c->color = BLACK;
        if (r->r->color == BLACK)
          return rotate(r, true);
        r->r->color = BLACK;
      }
      return update(r);
    }
    if (l->level > r->level) {
      l = propagate(l);
      Node *c = (l->r = submerge(l->r, r));
      if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) {
        l->color = RED;
        c->color = BLACK;
        if (l->l->color == BLACK)
          return rotate(l, false);
        l->l->color = BLACK;
      }
      return update(l);
    }
    return alloc(l, r);
  }

  Node *merge(Node *l, Node *r) {
    if (!l || !r)
      return l ? l : r;
    Node *c = submerge(l, r);
    c->color = BLACK;
    return c;
  }

  pair<Node *, Node *> split(Node *t, int k) {
    if (!t)
      return {nullptr, nullptr};
    t = propagate(t);
    if (k == 0)
      return {nullptr, t};
    if (k >= count(t))
      return {t, nullptr};
    if (k < count(t->l)) {
      auto p = split(t->l, k);
      return {p.first, merge(p.second, t->r)};
    }
    if (k > count(t->l)) {
      auto p = split(t->r, k - count(t->l));
      return {merge(t->l, p.first), p.second};
    }
    pair<Node *, Node *> ret = {t->l, t->r};
    pool.free(t);
    return ret;
  }

  Node *build(int l, int r, const vector<Monoid> &v) {
    if (l + 1 >= r)
      return alloc(v[l]);
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

  Node *build(const vector<Monoid> &v) {
    pool.clear();
    return build(0, (int)v.size(), v);
  }

  void dump(Node *r, typename vector<Monoid>::iterator &it) {
    r = propagate(r);
    if (!r->l || !r->r) {
      *it++ = r->key;
      return;
    }
    dump(r->l, it);
    dump(r->r, it);
  }

  vector<Monoid> dump(Node *r) {
    vector<Monoid> v((size_t)count(r));
    auto it = begin(v);
    dump(r, it);
    return v;
  }

  string to_string(Node *r) {
    auto s = dump(r);
    string ret;
    for (int i = 0; i < s.size(); i++) {
      ret += std::to_string(s[i]);
      ret += ", ";
    }
    return (ret);
  }

  void insert(Node *&t, int k, const Monoid &v) {
    auto x = split(t, k);
    t = merge(merge(x.first, alloc(v)), x.second);
  }

  void erase(Node *&t, int k) {
    auto x = split(t, k);
    auto y = split(x.second, 1);
    pool.free(y.first);
    t = merge(x.first, y.second);
  }

  Monoid query(Node *&t, int a, int b) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    auto ret = sum(y.first);
    t = merge(x.first, merge(y.first, y.second));
    return ret;
  }

  void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) {
    auto x = split(t, a);
    auto y = split(x.second, b - a);
    y.first->lazy = h(y.first->lazy, p);
    t = merge(x.first, merge(propagate(y.first), y.second));
  }

  void set_element(Node *&t, int k, const Monoid &x) {
    if (!t->l) {
      t->key = t->sum = x;
      return;
    }
    t = propagate(t);
    if (k < count(t->l))
      set_element(t->l, k, x);
    else
      set_element(t->r, k - count(t->l), x);
    t = update(t);
  }

  int size(Node *t) { return count(t); }

  bool empty(Node *t) { return !t; }

  Node *makeset() { return (nullptr); }
};

using int64 = long long;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &v[i]);

  auto f = [](int a, int b) { return min(a, b); };
  const int INF = 1 << 30;

  RedBlackTree<int, int> rbt(5400000, f, INF);
  auto root = rbt.build(v);
  for (int i = 0; i < q; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    if (x == 0) {
      int v = rbt.query(root, z, z + 1);
      rbt.erase(root, z);
      rbt.insert(root, y, v);
    } else if (x == 1) {
      printf("%d\\n", rbt.query(root, y, z + 1));
    } else if (x == 2) {
      rbt.set_element(root, y, z);
    }
    if (rbt.pool.ptr < 50)
      root = rbt.build(rbt.dump(root));
  }
}