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

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

#define MAX_NODE 10000000

typedef long long ll;
typedef unsigned int uint;
ll INF = (1LL << 60);

extern inline uint xor128(void) {
  static uint x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  uint t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

struct treap {
  treap *lch;
  treap *rch;
  uint pri;
  int cnt;
  ll val;
  ll min;
  treap() {}
};

treap Field[MAX_NODE];
typedef treap *Treap;

int cnt(Treap t) { return (t ? t->cnt : 0); }
ll min(Treap t) { return (t ? t->min : INF); }

Treap update(Treap t) {
  if (!t)
    return NULL;
  t->cnt = cnt(t->lch) + cnt(t->rch) + 1;
  ll dat = t->val;
  dat = min(dat, min(t->lch));
  dat = min(dat, min(t->rch));
  t->min = dat;
  return t;
}

Treap makenode(ll v) {
  static int cc = 1;
  treap &res = Field[cc++];
  res.lch = res.rch = NULL;
  res.val = v;
  res.cnt = 1;
  res.pri = xor128();
  return &res;
}

ll Find(Treap t, int k = 1e9) {
  k = min(k, cnt(t) - 1);
  int f = cnt(t->lch);
  ll res;
  if (f > k)
    res = Find(t->lch, k);
  if (f < k)
    res = Find(t->rch, k - f - 1);
  if (f == k)
    res = t->val;
  return res;
}

Treap merge(Treap l, Treap r) {
  if (!l)
    return update(r);
  if (!r)
    return update(l);

  if (l->pri > r->pri) {
    l->rch = merge(l->rch, r);
    return update(l);
  } else {
    r->lch = merge(l, r->lch);
    return update(r);
  }
}

typedef pair<Treap, Treap> treaps;
treaps split(Treap t, int k) {
  if (!t)
    return treaps(NULL, NULL);

  if (k <= cnt(t->lch)) {
    treaps s = split(t->lch, k);
    t->lch = s.second;
    return treaps(s.first, update(t));
  } else {
    treaps s = split(t->rch, k - cnt(t->lch) - 1);
    t->rch = s.first;
    return treaps(update(t), s.second);
  }
}

Treap insertk(Treap t, int k, ll val) {
  Treap f = makenode(val);
  treaps s = split(t, k);
  Treap res = merge(merge(s.first, f), s.second);
  return res;
}

Treap erasek(Treap t, int k) {
  treaps a = split(t, k);
  treaps b = split(a.second, 1);
  Treap res = merge(a.first, b.second);
  return res;
}

int main() {
  Treap root = NULL;
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    root = insertk(root, i, 2147483647);
  }
  while (q--) {
    int type;
    scanf("%d", &type);
    if (type == 0) {
      int i, x;
      scanf("%d %d", &i, &x);
      root = erasek(root, i);
      root = insertk(root, i, x);
    } else {
      int l, r;
      scanf("%d %d", &l, &r);
      r++;
      treaps tmp = split(root, r);
      treaps tmp2 = split(tmp.first, l);

      cout << tmp2.second->min << endl;
      root = merge(merge(tmp2.first, tmp2.second), tmp.second);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

#define MAX_NODE 10000000

typedef long long ll;
typedef unsigned int uint;
ll INF = (1LL << 60);

extern inline uint xor128(void) {
  static uint x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  uint t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

struct treap {
  treap *lch;
  treap *rch;
  uint pri;
  int cnt;
  ll val;
  ll min;
  treap() {}
};

treap Field[MAX_NODE];
typedef treap *Treap;

int cnt(Treap t) { return (t ? t->cnt : 0); }
ll min(Treap t) { return (t ? t->min : INF); }

Treap update(Treap t) {
  if (!t)
    return NULL;
  t->cnt = cnt(t->lch) + cnt(t->rch) + 1;
  ll dat = t->val;
  dat = min(dat, min(t->lch));
  dat = min(dat, min(t->rch));
  t->min = dat;
  return t;
}

Treap makenode(ll v) {
  static int cc = 1;
  treap &res = Field[cc++];
  res.lch = res.rch = NULL;
  res.val = v;
  res.cnt = 1;
  res.pri = xor128();
  return &res;
}

ll Find(Treap t, int k = 1e9) {
  k = min(k, cnt(t) - 1);
  int f = cnt(t->lch);
  if (f > k)
    return Find(t->lch, k);
  if (f < k)
    return Find(t->rch, k - f - 1);
  if (f == k)
    return t->val;
}

Treap merge(Treap l, Treap r) {
  if (!l)
    return r;
  if (!r)
    return l;
  if (l->pri > r->pri) {
    l->rch = merge(l->rch, r);
    return update(l);
  } else {
    r->lch = merge(r->rch, l);
    return update(l);
  }
}

typedef pair<Treap, Treap> treaps;
treaps split(Treap t, int k) {
  if (!t)
    return treaps(NULL, NULL);

  if (k <= cnt(t->lch)) {
    treaps s = split(t->lch, k);
    t->lch = s.second;
    return treaps(s.first, update(t));
  } else {
    treaps s = split(t->rch, k - cnt(t->lch) - 1);
    t->rch = s.first;
    return treaps(update(t), s.second);
  }
}

Treap insertk(Treap &t, int k, ll val) {
  Treap f = makenode(val);
  treaps s = split(t, k);
  return t = merge(merge(s.first, f), s.second);
}

Treap erasek(Treap &t, int k) {
  treaps a = split(t, k);
  treaps b = split(a.second, 1);
  return t = merge(a.first, b.second);
}

int main() {
  Treap root = NULL;
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    insertk(root, i, 2147483647);
  }
  while (q--) {
    int type;
    scanf("%d", &type);
    if (type == 0) {
      int i, x;
      scanf("%d %d", &i, &x);
      erasek(root, i);
      insertk(root, i, x);
    } else {
      int l, r;
      scanf("%d %d", &l, &r);
      r++;
      treaps tmp = split(root, r);
      treaps tmp2 = split(tmp.first, l);
      printf("%lld\\n", tmp2.second->min);
      root = merge(merge(tmp2.first, tmp2.second), tmp.second);
    }
  }
  return 0;
}