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

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <tuple>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, n) loop(i, 0, n)

using namespace std;

#define np nullptr
struct Node {
  int val;
  Node *ch[2];
  int pri, cnt, sum;
  int min;
  Node(int v, int p) : val(v), pri(p), cnt(1), sum(v), min(v) {
    ch[0] = ch[1] = np;
  }
};

void destory(Node *t) {
  if (t == np)
    return;
  destory(t->ch[0]);
  destory(t->ch[1]);
  delete t;
}

int count(Node *t) { return t == np ? 0 : t->cnt; }
int sum(Node *t) { return t == np ? 0 : t->sum; }

int const inf = 1e9;

int min(Node *t) { return t == np ? inf : t->min; }

int min(int x, int y, int z) { return min(x, min(y, z)); }

void show(Node *n, int ofs = 0) {
  if (n == nullptr)
    return;
  show(n->ch[0], ofs + 1);
  rep(i, ofs * 2) putchar(' ');
  printf("v:%d m:%d (%p)\\n", n->val, n->min, n);
  show(n->ch[1], ofs + 1);
}

void update(Node *t) {
  t->cnt = count(t->ch[0]) + count(t->ch[1]) + 1;
  t->sum = sum(t->ch[0]) + sum(t->ch[1]) + t->val;
  t->min = min(min(t->ch[0]), min(t->ch[1]), t->val);
}

Node *merge(Node *l, Node *r) {
  if (l == np || r == np)
    return l == np ? r : l;
  if (l->pri > r->pri) {
    l->ch[1] = merge(l->ch[1], r);
    update(l);
    return l;
  } else {
    r->ch[0] = merge(l, r->ch[0]);
    update(r);
    return r;
  }
}

pair<Node *, Node *> split(Node *t, int k) {
  if (t == np)
    return make_pair(np, np);
  if (k <= count(t->ch[0])) {
    auto s = split(t->ch[0], k);
    t->ch[0] = s.second;
    update(t);
    return make_pair(s.first, t);
  } else {
    k -= count(t->ch[0]) + 1;
    auto s = split(t->ch[1], k);
    t->ch[1] = s.first;
    update(t);
    return make_pair(t, s.second);
  }
}

Node *erase(Node *t, int k) {
  Node *l, *m, *r;
  tie(l, m) = split(t, k);
  tie(m, r) = split(m, 1);
  destory(m);
  return merge(l, r);
}

Node *at(Node *t, int k) {
  int lc = count(t->ch[0]);
  if (k < lc)
    return at(t->ch[0], k);
  else if (k == lc)
    return t;
  else
    return at(t->ch[1], k - lc - 1);
}

Node *insert(Node *t, int k, int v) {
  Node *l, *m = new Node(v, rand()), *r;
  tie(l, r) = split(t, k);
  return merge(merge(l, m), r);
}

int min(Node *t, int l, int r) {
  if (r - l <= 0)
    return inf;
  if (r - l == count(t))
    return min(t);
  int s = count(t->ch[0]);
  if (l == s && r - s == 1)
    return t->val;
  int res = inf;
  if (l < s)
    res = min(res, min(t->ch[0], l, min(s, r)));
  if (l <= s && s < r)
    res = min(res, min(t, s, s + 1));
  if (s + 1 < r)
    res = min(res, min(t->ch[1], max(0, l - s - 1), r - s - 1));
  return res;
}

int get() {
  int x;
  scanf("%d", &x);
  return x;
}
void put(int x) {
  // printf("ans : %d\\n",x);
  printf("%d\\n", x);
}

#define eputs(s) fprintf(stderr, s "\\n")

int main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  srand(time(0));
  int n = get(), q = get();
  Node *root = np;
  rep(i, n) { root = insert(root, i, get()); }
  // show(root);
  // puts("---");

  rep(i, q) {
    int op = get();
    if (false) {
    } else if (op == 0) {
      int l = get(), r = get();
      // printf("shift %d %d\\n",l,r);
      int x = at(root, r)->val;
      root = erase(root, r);
      root = insert(root, l, x);
    } else if (op == 1) {
      int l = get(), r = get();
      r++;
      // printf("min[%d,%d)\\n",l,r);
      put(min(root, l, r));
    } else if (op == 2) {
      int p = get(), v = get(); // /10000;
      // printf("change %d %d\\n",p,v);
      root = erase(root, p);
      root = insert(root, p, v);
    }
    // show(root);
    // puts("---");
  }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it)

ll const inf = 1 << 28;
double const pi = acos(-1);

int n, q;
int a[300000];

void view(vvi const &a) {
  return;
  rep(i, a.size()) {
    rep(j, a[i].size()) {
      if (a[i][j] == inf)
        cout << "inf";
      else {
        if (a[i][j] >= 100)
          cout << a[i][j] / 1000;
        else
          cout << a[i][j];
      }
      if (j != a[i].size() - 1)
        cout << " ";
    }
    if (i != a.size() - 1)
      cout << "|";
  }
  cout << endl;
}

pair<int, int> index(const vvi &tree, int k) {
  int s = 0;
  int i = 0;
  while (s + (int)tree[i].size() <= k) {
    s += tree[i].size();
    i++;
  }
  int j = 0;
  while (s + j < k)
    j++;
  return make_pair(i, j);
}

int solve() {

  int w = sqrt(n);
  if (w * w != n)
    w++;
  vvi tree(w, vi(w, inf));
  vi rmq(w, inf);
  rep(i, w) {
    rep(j, w) {
      int x = a[i * w + j];
      rmq[i] = min(x, rmq[i]);
      tree[i][j] = x;
    }
  }

  // view(tree);

  rep(_, q) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    // printf("----------\\n%d %d %d\\n", x, y, z);
    if (x == 0) {

      // shift
      int L = y, R = z;
      auto p = index(tree, L), q = index(tree, R);
      int i1 = p.first, j1 = p.second;
      int i2 = q.first, j2 = q.second;
      int t = tree[i2][j2];
      tree[i2].erase(tree[i2].begin() + j2);
      tree[i1].insert(tree[i1].begin() + i2, t);

    } else if (x == 1) {

      // rmq
      int L = y, R = z;
      auto p = index(tree, L), q = index(tree, R);
      int i1 = p.first, j1 = p.second;
      int i2 = q.first, j2 = q.second;
      int res = inf;
      if (i1 == i2) {
        loop(t, j1, j2 + 1) { res = min(res, tree[i1][t]); }
      } else {
        loop(t, j1, tree[i1].size()) { res = min(res, tree[i1][t]); }
        loop(t, i1 + 1, i2) { res = min(res, rmq[t]); }
        loop(t, 0, j2 + 1) { res = min(res, tree[i2][t]); }
      }
      printf("%d\\n", res);

    } else if (x == 2) {

      // update
      int k = y, x = z;
      auto p = index(tree, k);
      tree[p.first][p.second] = x;
      rmq[p.first] = min(rmq[p.first], x);
    }

    // view(tree);
  }
  return 0;
}

int main() {
  while (scanf("%d%d", &n, &q) != EOF) {
    rep(i, 300000) a[i] = inf;
    rep(i, n) scanf("%d", a + i);
    solve();
  }
}