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

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

struct treap {
  static const int INF = INT_MAX;

  struct node {
    int val, mini, cnt, pri;
    node *lch, *rch;

    node(int val, int pri) : val(val), pri(pri), cnt(1), mini(val) {
      lch = rch = NULL;
    }
  };

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

  int count(node *t) { return !t ? 0 : t->cnt; }
  int mini(node *t) { return !t ? INF : t->mini; }

  node *update(node *t) {
    if (!t)
      return t;
    t->cnt = count(t->lch) + count(t->rch) + 1;
    t->mini = min(min(mini(t->lch), mini(t->rch)), t->val);
    return t;
  }

  node *merge(node *l, node *r) {
    if (!l || !r)
      return l ? l : r;
    if (l->pri > r->pri) {
      l->rch = merge(l->rch, r);
      return update(l);
    }
    r->lch = merge(l, r->lch);
    return update(r);
  }

  pair<node *, node *> split(node *t, int k) {
    if (!t)
      return make_pair((node *)NULL, (node *)NULL);
    if (count(t->lch) >= k) {
      pair<node *, node *> s = split(t->lch, k);
      t->lch = s.second;
      return make_pair(s.first, update(t));
    }
    pair<node *, node *> s = split(t->rch, k - count(t->lch) - 1);
    t->rch = s.first;
    return make_pair(update(t), s.second);
  }

  node *insert(node *t, int k, int 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);
  }

  node *erase(node *t, int k) {
    pair<node *, node *> s1, s2;
    s2 = split(t, k + 1);
    s1 = split(s2.first, k);
    return update((t = merge(s1.first, s2.second)));
  }

  void insert(int k, int val) { root = insert(root, k, val, rand()); }

  void erase(int k) { root = erase(root, k); }

  int rangeMinimumQuery(node *t, int l, int r) {
    if (!t)
      return INF;
    int sz = count(t);
    if (sz <= l || r <= 0)
      return INF;
    if (l <= 0 && sz <= r)
      return mini(t);

    int cnt = count(t->lch);

    int vl = rangeMinimumQuery(t->lch, l, r);
    int vr = rangeMinimumQuery(t->rch, l - cnt - 1, r - cnt - 1);

    int res = min(vl, vr);
    if (l <= cnt && cnt < r)
      res = min(res, t->val);
    return res;
  }

  int rangeMinimumQuery(int l, int r) { return rangeMinimumQuery(root, l, r); }

  void updateNode(int k, int val) {
    erase(k);
    insert(k, val);
  }

  void Debug(node *t) {
    if (!t)
      return;
    Debug(t->lch);
    cout << t->val << " ";
    Debug(t->rch);
  }

  void Debug() {
    Debug(root);
    cout << endl;
  }
};

int main() {

  int n, q;
  scanf("%d%d", &n, &q);

  treap latte;
  for (int i = 0; i < n; i++)
    latte.insert(i, INT_MAX);

  while (q--) {
    int type, x, y;
    scanf("%d%d%d", &type, &x, &y);
    if (type == 0) {
      latte.updateNode(x, y);
    } else {
      printf("%d\\n", latte.rangeMinimumQuery(x, y + 1));
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

struct treap {
  static const int INF = INT_MAX;

  struct node {
    int val, mini, cnt, pri;
    node *lch, *rch;

    node(int val, int pri) : val(val), pri(pri), cnt(1), mini(val) {
      lch = rch = NULL;
    }
  };

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

  int count(node *t) { return !t ? 0 : t->cnt; }
  int mini(node *t) { return !t ? INF : t->mini; }

  node *update(node *t) {
    t->cnt = count(t->lch) + count(t->rch) + 1;
    t->mini = min(min(mini(t->lch), mini(t->rch)), t->val);
    return t;
  }

  node *merge(node *l, node *r) {
    if (!l || !r)
      return l ? l : r;
    if (l->pri > r->pri) {
      l->rch = merge(l->rch, r);
      return update(l);
    }
    r->lch = merge(l, r->lch);
    return update(r);
  }

  pair<node *, node *> split(node *t, int k) {
    if (!t)
      return make_pair((node *)NULL, (node *)NULL);
    if (count(t->lch) >= k) {
      pair<node *, node *> s = split(t->lch, k);
      t->lch = s.second;
      return make_pair(s.first, update(t));
    }
    pair<node *, node *> s = split(t->rch, k - count(t->lch) - 1);
    t->rch = s.first;
    return make_pair(update(t), s.second);
  }

  node *insert(node *t, int k, int 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);
  }

  node *erase(node *t, int k) {
    pair<node *, node *> s1, s2;
    s2 = split(t, k + 1);
    s1 = split(s2.first, k);
    return update((t = merge(s1.first, s2.second)));
  }

  void insert(int k, int val) { root = insert(root, k, val, rand()); }

  void erase(int k) { root = erase(root, k); }

  int rangeMinimumQuery(node *t, int l, int r) {
    int sz = count(t);
    if (sz <= l || r <= 0)
      return INF;
    if (l <= 0 && sz <= r)
      return mini(t);

    int cnt = count(t->lch);

    int vl = rangeMinimumQuery(t->lch, l, r);
    int vr = rangeMinimumQuery(t->rch, l - cnt - 1, r - cnt - 1);

    int res = min(vl, vr);
    if (l <= cnt && cnt < r)
      res = min(res, t->val);
    return res;
  }

  int rangeMinimumQuery(int l, int r) { return rangeMinimumQuery(root, l, r); }

  void updateNode(int k, int val) {
    erase(k);
    insert(k, val);
  }

  void Debug(node *t) {
    if (!t)
      return;
    Debug(t->lch);
    cout << t->val << " ";
  }

  void Debug() {
    Debug(root);
    cout << endl;
  }
};

int main() {

  int n, q;
  scanf("%d%d", &n, &q);

  treap latte;
  for (int i = 0; i < n; i++)
    latte.insert(i, INT_MAX);

  while (q--) {
    int type, x, y;
    scanf("%d%d%d", &type, &x, &y);
    if (type == 0) {
      latte.updateNode(x, y);
    } else {
      printf("%d\\n", latte.rangeMinimumQuery(x, y + 1));
    }
  }
  return 0;
}