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) { clear(); }

  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 D, class L, D (*f)(D, D), D (*g)(D, L), L (*h)(L, L)>
struct RandomizedBinarySearchTree {
  inline int xor128() {
    static int x = 123456789;
    static int y = 362436069;
    static int z = 521288629;
    static int w = 88675123;
    int t;

    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  struct Node {
    Node *l, *r;
    int cnt;
    D key, sum;
    L lazy;

    Node() {}

    Node(const D &k, const L &p)
        : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {}
  };

  ArrayPool<Node> pool;

  const D M1;
  const L OM0;

  RandomizedBinarySearchTree(int sz, const D &M1, const L &OM0)
      : pool(sz), M1(M1), OM0(OM0) {}

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

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

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

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

  inline Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + 1;
    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) {
      t->key = g(t->key, t->lazy);
      if (t->l) {
        t->l = clone(t->l);
        t->l->lazy = h(t->l->lazy, t->lazy);
        t->l->sum = g(t->l->sum, t->lazy);
      }
      if (t->r) {
        t->r = clone(t->r);
        t->r->lazy = h(t->r->lazy, t->lazy);
        t->r->sum = g(t->r->sum, t->lazy);
      }
      t->lazy = OM0;
    }
    return update(t);
  }

  Node *merge(Node *l, Node *r) {
    if (!l || !r)
      return l ? l : r;
    if (xor128() % (l->cnt + r->cnt) < l->cnt) {
      l = propagate(l);
      l->r = merge(l->r, r);
      return update(l);
    } else {
      r = propagate(r);
      r->l = merge(l, r->l);
      return update(r);
    }
  }

  pair<Node *, Node *> split(Node *t, int k) {
    if (!t)
      return {t, t};
    t = propagate(t);
    if (k <= count(t->l)) {
      auto s = split(t->l, k);
      t->l = s.second;
      return {s.first, update(t)};
    } else {
      auto s = split(t->r, k - count(t->l) - 1);
      t->r = s.first;
      return {update(t), s.second};
    }
  }

  Node *build(int l, int r, const vector<D> &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<D> &v) {
    // ptr = 0;
    return build(0, (int)v.size(), v);
  }

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

  vector<D> dump(Node *r) {
    vector<D> 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 += ", ";
    return (ret);
  }

  void insert(Node *&t, int k, const D &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);
  }

  D 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 L &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 D &x) {
    t = propagate(t);
    if (k < count(t->l))
      set_element(t->l, k, x);
    else if (k == count(t->l))
      t->key = t->sum = x;
    else
      set_element(t->r, k - count(t->l) - 1, 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 mymin(int a, int b) { return min(a, b); }

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]);

  const int INF = 1 << 30;
  RandomizedBinarySearchTree<int, int, mymin, mymin, mymin> rbst(n, INF, 0);
  auto root = rbst.build(v);
  for (int i = 0; i < q; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    if (x == 0) {
      auto vv = rbst.query(root, z, z + 1);
      rbst.erase(root, z);
      rbst.insert(root, y, vv);
    } else if (x == 1) {
      printf("%d\\n", rbst.query(root, y, z + 1));
    } else if (x == 2) {
      rbst.set_element(root, y, z);
    }
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

template <class D, class L, D (*f)(D, D), D (*g)(D, L), L (*h)(L, L)>
struct RandomizedBinarySearchTree {
  inline int xor128() {
    static int x = 123456789;
    static int y = 362436069;
    static int z = 521288629;
    static int w = 88675123;
    int t;

    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  struct Node {
    Node *l, *r;
    int cnt;
    D key, sum;
    L lazy;

    Node() {}

    Node(const D &k, const L &p)
        : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {}
  };

  vector<Node> pool;
  int ptr;

  const D M1;
  const L OM0;

  RandomizedBinarySearchTree(int sz, const D &M1, const L &OM0)
      : pool(sz), ptr(0), M1(M1), OM0(OM0) {}

  inline Node *alloc(const D &key) { return &(pool[ptr++] = Node(key, OM0)); }

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

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

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

  inline Node *update(Node *t) {
    t->cnt = count(t->l) + count(t->r) + 1;
    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) {
      t->key = g(t->key, t->lazy);
      if (t->l) {
        t->l = clone(t->l);
        t->l->lazy = h(t->l->lazy, t->lazy);
        t->l->sum = g(t->l->sum, t->lazy);
      }
      if (t->r) {
        t->r = clone(t->r);
        t->r->lazy = h(t->r->lazy, t->lazy);
        t->r->sum = g(t->r->sum, t->lazy);
      }
      t->lazy = OM0;
    }
    return update(t);
  }

  Node *merge(Node *l, Node *r) {
    if (!l || !r)
      return l ? l : r;
    if (xor128() % (l->cnt + r->cnt) < l->cnt) {
      l = propagate(l);
      l->r = merge(l->r, r);
      return update(l);
    } else {
      r = propagate(r);
      r->l = merge(l, r->l);
      return update(r);
    }
  }

  pair<Node *, Node *> split(Node *t, int k) {
    if (!t)
      return {t, t};
    t = propagate(t);
    if (k <= count(t->l)) {
      auto s = split(t->l, k);
      t->l = s.second;
      return {s.first, update(t)};
    } else {
      auto s = split(t->r, k - count(t->l) - 1);
      t->r = s.first;
      return {update(t), s.second};
    }
  }

  Node *build(int l, int r, const vector<D> &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<D> &v) {
    // ptr = 0;
    return build(0, (int)v.size(), v);
  }

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

  vector<D> dump(Node *r) {
    vector<D> 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 += ", ";
    return (ret);
  }

  void insert(Node *&t, int k, const D &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);
    t = merge(x.first, split(x.second, 1).second);
  }

  D 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 L &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 D &x) {
    t = propagate(t);
    if (k < count(t->l))
      set_element(t->l, k, x);
    else if (k == count(t->l))
      t->key = t->sum = x;
    else
      set_element(t->r, k - count(t->l) - 1, 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 mymin(int a, int b) { return min(a, b); }

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]);

  const int INF = 1 << 30;
  RandomizedBinarySearchTree<int, int, mymin, mymin, mymin> rbst(n, INF, 0);
  auto root = rbst.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 = rbst.query(root, z, z + 1);
      rbst.erase(root, z);
      rbst.insert(root, y, v);
    } else if (x == 1) {
      printf("%d\\n", rbst.query(root, y, z + 1));
    } else if (x == 2) {
      rbst.set_element(root, y, z);
    }
  }
}