Problem p02431 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
template <class T> class treap {
public:
  struct node {
    T val, sum, mini, maxi;
    node *left, *right;
    int pri;
    unsigned sz;
    node(T val, int pri)
        : val(val), sum(val), mini(val), maxi(val), pri(pri), sz(1) {
      left = right = NULL;
    }
  };

  node *root;

  treap() : root(NULL) { srand(time(NULL)); }

  unsigned size() { return size(root); }
  unsigned size(node *v) { return !v ? 0 : v->sz; }
  T sum(node *v) { return !v ? 0 : v->sum; }
  T MIN(node *v) { return !v ? INF : v->mini; }
  T MAX(node *v) { return !v ? -INF : v->maxi; }

  node *update(node *v) {
    v->sz = size(v->left) + size(v->right) + 1;
    v->sum = sum(v->left) + sum(v->right) + v->val;
    v->mini = min(MIN(v->left), MIN(v->right));
    v->maxi = max(MAX(v->left), MAX(v->right));
    return v;
  }

  node *merge(node *s, node *t) {
    if (!s || !t)
      return s ? s : t;
    if (s->pri > t->pri) {
      s->right = merge(s->right, t);
      return update(s);
    } else {
      t->left = merge(s, t->left);
      return update(t);
    }
  }

  pair<node *, node *> split(node *v, unsigned k) {

    if (!v)
      return pair<node *, node *>(NULL, NULL);

    if (k <= size(v->left)) {
      pair<node *, node *> s = split(v->left, k);
      v->left = s.second;
      return make_pair(s.first, update(v));
    }

    else {
      pair<node *, node *> s = split(v->right, k - size(v->left) - 1);
      v->right = s.first;
      return make_pair(update(v), s.second);
    }
  }

  node *find(unsigned k) {
    node *v = root;
    while (v) {
      unsigned s = size(v->left);
      if (s > k)
        v = v->left;
      else if (s == k)
        break;
      else {
        v = v->right;
        k -= s + 1;
      }
    }
    return v;
  }

  int getval(int k) {
    node *t = find(k);
    return t->val;
  }

  void insert(unsigned k, T val) { root = insert(root, k, val, rand()); }
  node *insert(node *t, unsigned k, T val, int pri) {
    pair<node *, node *> s = split(t, k);
    t = merge(s.first, new node(val, pri));
    t = merge(t, s.second);
    return update(t);
  }

  void erase(int k) { root = erase(root, k); }
  node *erase(node *t, unsigned k) {
    pair<node *, node *> u, v;
    u = split(t, k + 1);
    v = split(u.first, k);
    t = merge(v.first, u.second);
    return update(t);
  }

  void range_erase(int l, int r) { root = range_erase(root, l, r); }
  node range_erase(node *t, unsigned k) { pair<node *, node *> u, v; }

  void push_back(T val) { insert(root ? root->sz : 0, val); }
  void pop_back() { erase(root->sz - 1); }

  void print_array() {
    int k = root->sz;
    cout << "root value = " << root->val << endl;
    cout << "size of array = " << k << endl;
    for (int i = 0; i < k; i++) {
      if (i)
        cout << " ";
      cout << getval(i);
    }
    cout << endl;
  }
};

int main() {
  treap<int> tri;
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a;
    if (a != 2)
      cin >> b;
    if (a == 0)
      tri.push_back(b);
    if (a == 2)
      tri.pop_back();
    if (a == 1)
      cout << tri.getval(b) << endl;
  }
  /*while(1){
          int a,b,c;
          cin>>a>>b;

          if(a!=-1)tri.insert(b,a);
          else tri.erase(b);
          tri.print_array();
  }*/
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
template <class T> class treap {
public:
  struct node {
    T val, sum, mini, maxi;
    node *left, *right;
    int pri;
    unsigned sz;
    node(T val, int pri)
        : val(val), sum(val), mini(val), maxi(val), pri(pri), sz(1) {
      left = right = NULL;
    }
  };

  node *root;

  treap() : root(NULL) { srand(time(NULL)); }

  unsigned size() { return size(root); }
  unsigned size(node *v) { return !v ? 0 : v->sz; }
  T sum(node *v) { return !v ? 0 : v->sum; }
  T MIN(node *v) { return !v ? INF : v->mini; }
  T MAX(node *v) { return !v ? -INF : v->maxi; }

  node *update(node *v) {
    v->sz = size(v->left) + size(v->right) + 1;
    v->sum = sum(v->left) + sum(v->right) + v->val;
    v->mini = min(MIN(v->left), MIN(v->right));
    v->maxi = max(MAX(v->left), MAX(v->right));
    return v;
  }

  node *merge(node *s, node *t) {
    if (!s || !t)
      return s ? s : t;
    if (s->pri > t->pri) {
      s->right = merge(s->right, t);
      return update(s);
    } else {
      t->left = merge(s, t->left);
      return update(t);
    }
  }

  pair<node *, node *> split(node *v, unsigned k) {

    if (!v)
      return pair<node *, node *>(NULL, NULL);

    if (k <= size(v->left)) {
      pair<node *, node *> s = split(v->left, k);
      v->left = s.second;
      return make_pair(s.first, update(v));
    }

    else {
      pair<node *, node *> s = split(v->right, k - size(v->left) - 1);
      v->right = s.first;
      return make_pair(update(v), s.second);
    }
  }

  node *find(unsigned k) {
    node *v = root;
    while (v) {
      unsigned s = size(v->left);
      if (s > k)
        v = v->left;
      else if (s == k)
        break;
      else {
        v = v->right;
        k -= s + 1;
      }
    }
    return v;
  }

  int getval(int k) {
    node *t = find(k);
    return t->val;
  }

  void insert(unsigned k, T val) { root = insert(root, k, val, rand()); }
  node *insert(node *t, unsigned k, T val, int pri) {
    pair<node *, node *> s = split(t, k);
    t = merge(s.first, new node(val, pri));
    t = merge(t, s.second);
    return update(t);
  }

  void erase(int k) { root = erase(root, k); }
  node *erase(node *t, unsigned k) {
    if (root->sz == 1) {
      root = NULL;
      return NULL;
    }
    pair<node *, node *> u, v;
    u = split(t, k + 1);
    v = split(u.first, k);
    t = merge(v.first, u.second);
    return update(t);
  }

  void range_erase(int l, int r) { root = range_erase(root, l, r); }
  node range_erase(node *t, unsigned k) { pair<node *, node *> u, v; }

  void push_back(T val) { insert(root ? root->sz : 0, val); }
  void pop_back() { erase(root->sz - 1); }

  void print_array() {
    int k = root->sz;
    cout << "root value = " << root->val << endl;
    cout << "size of array = " << k << endl;
    for (int i = 0; i < k; i++) {
      if (i)
        cout << " ";
      cout << getval(i);
    }
    cout << endl;
  }
};

int main() {
  treap<int> tri;
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a;
    if (a != 2)
      cin >> b;
    if (a == 0)
      tri.push_back(b);
    if (a == 2)
      tri.pop_back();
    if (a == 1)
      cout << tri.getval(b) << endl;
  }
  /*while(1){
          int a,b,c;
          cin>>a>>b;

          if(a!=-1)tri.insert(b,a);
          else tri.erase(b);
          tri.print_array();
  }*/
}