Problem p00998 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <ctime>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl

using Key = int;
const Key INF = 1e9;
const int MAX_N = 1000010; // ?????\?????????° + EPS

struct node {
  Key key;
  int priority;
  node *left, *right;
  int size;
  Key min;

  static node *const NIL;

  node() {}

  node(const Key &x) : node(x, xor128(), NIL, NIL, 1, x) {}

  node(const Key &key_, int priority_, node *left_, node *right_, int size_,
       const Key &min_)
      : key(key_), priority(priority_), left(left_), right(right_), size(size_),
        min(min_) {}

  void *operator new(std::size_t) {
    static node pool[MAX_N];
    static int p = 0;
    return pool + p++;
  }

  static uint32_t xor128() {
    static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0);
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w % 100;
  }
};

using np = node *;
using cnp = const node *;
using npair = std::pair<np, np>;

node *const node::NIL = new node(Key(), -1, nullptr, nullptr, 0, INF);

bool is_leaf(cnp n) { return n->left == node::NIL; }

class treap {
protected:
  np root;

public:
  treap() : root(node::NIL) {}

  treap(np root_) : root(root_) {}

  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1
  treap(Key *left, Key *right) : root(__make_tree(left, right)) {
    int n = right - left;
    std::vector<int> ps(n - 1);
    std::generate(ps.begin(), ps.end(), node::xor128);
    std::sort(ps.begin(), ps.end());
    std::queue<np> que;
    que.push(root);
    while (que.size()) {
      np n = que.front();
      que.pop();
      if (is_leaf(n))
        continue;
      n->priority = ps.back();
      ps.pop_back();
      que.push(n->left);
      que.push(n->right);
    }
  }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1
  np __make_tree(Key *left, Key *right) {
    int sz = right - left;
    Key *mid = left + sz / 2;
    if (sz == 0) {
      return node::NIL;
    } else if (sz == 1) {
      return new node(*mid, -1, node::NIL, node::NIL, 1, *mid);
    } else {
      np lc = __make_tree(left, mid);
      np rc = __make_tree(mid, right);
      return new node(-1, -1, lc, rc, sz, std::min(lc->min, rc->min));
    }
  }

public:
  int size() const { return root->size; }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  void set(int k, const Key &x) { __set(root, k, x); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  void __set(np n, int k, const Key &x) {
    if (n->size == 1) {
      n->key = x;
    } else if (k < n->left->size)
      __set(n->left, k, x);
    else
      __set(n->right, k - n->left->size, x);
    __update(n);
  }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key get(int k) const { return __get(root, k); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key __get(np n, int k) const {
    if (n->size == 1) {
      return n->key;
    } else if (k < n->left->size)
      return __get(n->left, k);
    else
      return __get(n->right, k - n->left->size);
  }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key range_min(int l, int r) { return __range_min(root, l, r); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key __range_min(cnp n, int l, int r) const {
    l = std::max(l, 0);
    r = std::min(r, n->size);
    // std::cout << n << ' ' << l << ' ' << r << std::endl;
    if (l >= r)
      return INF;
    if (l == 0 && r == n->size)
      return n->min;
    Key res = INF;
    int sl = n->left->size;
    res = std::min(res, __range_min(n->left, l, r));
    res = std::min(res, __range_min(n->right, l - sl, r - sl));
    return res;
  }

public:
  void merge_from_left(treap &t) { root = __merge(t.root, root); }

  void merge_from_right(treap &t) { root = __merge(root, t.root); }

protected:
  // yukicoder 449 http://yukicoder.me/submissions/131855
  np __merge(np l, np r, int pri) const {
    if (is_leaf(l) && is_leaf(r))
      return new node(-1, pri, l, r, 2, std::min(l->key, r->key));
    if (is_leaf(l)) {
      r->left = __merge(l, r->left, pri);
      return __update(r);
    }
    if (is_leaf(r)) {
      l->right = __update(__merge(l->right, r, pri));
      return __update(l);
    }
    if (pri > l->priority && pri > r->priority) {
      return new node(-1, pri, l, r, l->size + r->size,
                      std::min(l->min, r->min));
    }
    if (l->priority > r->priority) {
      l->right = __merge(l->right, r, pri);
      return __update(l);
    }
    {
      r->left = __merge(l, r->left, pri);
      return __update(r);
    }
  }

  np __merge(np l, np r) const {
    if (l == node::NIL)
      return r;
    if (r == node::NIL)
      return l;
    int pri = node::xor128();
    return __merge(l, r, pri);
  }

public:
  // yukicoder 449 http://yukicoder.me/submissions/131855
  std::pair<treap, treap> split_at(int k) {
    np l, r;
    std::tie(l, r) = __split_at(root, k);
    return std::make_pair(treap(l), treap(r));
  }

protected:
  // yukicoder 449 http://yukicoder.me/submissions/131855

  npair __split_at(np n, int k) const {
    if (n == node::NIL)
      return npair(node::NIL, node::NIL);
    else
      return __split_at_sub(n, k);
  }

  npair __split_at_sub(np n, int k) const {
    if (is_leaf(n))
      return k == 0 ? npair(node::NIL, n) : npair(n, node::NIL);
    if (k == n->left->size)
      return npair(n->left, n->right);
    if (k < n->left->size) {
      npair p = __split_at_sub(n->left, k);
      n->left = p.second;
      return npair(p.first, __update(n));
    } else {
      npair p = __split_at_sub(n->right, k - n->left->size);
      n->right = p.first;
      return npair(__update(n), p.second);
    }
  }

protected:
  np __update(np n) const {
    n->size = n->left->size + n->right->size + (n->left == node::NIL);
    n->min = std::min(
        {n->left->min, n->right->min, (n->left == node::NIL ? n->key : INF)});
    return n;
  }

public:
  std::string to_string() const {
    std::string res;
    __to_string(root, res);
    return res;
  }

protected:
  void __to_string(cnp n, std::string &res) const {
    res += "(";
    if (n != node::NIL) {
      __to_string(n->left, res);
      res += ")" + std::to_string(n->key) + "(";
      __to_string(n->right, res);
    }
    res += ")";
  }

public:
  void show() {
    return;
    puts("=== show ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show(root, 0);
    }
  }

  void show_array() {
    return;
    puts("=== show array ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show_array(root);
      std::cout << '\n';
    }
  }

  void __show(np n, int ofs) {
    using std::cout;
    using std::endl;
    using std::string;
    assert((n->left == node::NIL && n->right == node::NIL) ||
           (n->left != node::NIL && n->right != node::NIL));
    if (is_leaf(n)) { // leaf
      printf("%s-- (%d %d %d)\n", string(ofs * 8, ' ').c_str(), n->priority,
             n->min, n->key);
    } else {
      __show(n->left, ofs + 1);
      printf("%s(%d %d %d) < \n", string(ofs * 8, ' ').c_str(), n->priority,
             n->min, n->key);
      __show(n->right, ofs + 1);
    }
  }

  void __show_array(np n) {
    using std::cout;
    using std::endl;
    using std::string;
    assert((n->left == node::NIL && n->right == node::NIL) ||
           (n->left != node::NIL && n->right != node::NIL));
    if (is_leaf(n)) { // leaf
      printf("%d ", n->key);
    } else {
      __show_array(n->left);
      __show_array(n->right);
    }
  }

public:
  void shift(int l, int r) {
    // std::cout << "shift " << l << ' ' << r << std::endl;
    np a, b, c, d;
    std::tie(a, b) = __split_at(root, l);
    std::tie(b, c) = __split_at(b, r - l);

    // puts("cd");
    // treap(c).show_array();
    std::tie(c, d) = __split_at(c, 1);

    // puts("a");
    // treap(a).show_array();
    // puts("b");
    // treap(b).show_array();
    // puts("c");
    // treap(c).show_array();
    // puts("d");
    // treap(d).show_array();

    np ac = __merge(a, c);
    np bd = __merge(b, d);
    root = __merge(ac, bd);
  }
};

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) begin(c), end(c)

using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, q;
  cin >> n >> q;

  static int a[200010];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  treap t(a, a + n);

  t.show();
  t.show_array();

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    // cout << "== query " << x << ' ' << y << ' ' << z << " ==" << endl;
    if (x == 0) {
      t.shift(y, z);
    }
    if (x == 1) {
      // puts("=====");
      // Treap::show(root);
      cout << t.range_min(y, z + 1) << '\n';
    }
    if (x == 2) {
      t.set(y, z);
    }

    t.show();
    t.show_array();
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <ctime>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl

using Key = int;
const Key INF = 1e9;
const int MAX_N = 1000010; // ?????\?????????° + EPS

struct node {
  Key key;
  int priority;
  node *left, *right;
  int size;
  Key min;

  static node *const NIL;

  node() {}

  node(const Key &x) : node(x, xor128(), NIL, NIL, 1, x) {}

  node(const Key &key_, int priority_, node *left_, node *right_, int size_,
       const Key &min_)
      : key(key_), priority(priority_), left(left_), right(right_), size(size_),
        min(min_) {}

  void *operator new(std::size_t) {
    static node pool[MAX_N];
    static int p = 0;
    return pool + p++;
  }

  static uint32_t xor128() {
    static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0);
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w % 100;
  }
};

using np = node *;
using cnp = const node *;
using npair = std::pair<np, np>;

node *const node::NIL = new node(Key(), -1, nullptr, nullptr, 0, INF);

bool is_leaf(cnp n) { return n->left == node::NIL; }

class treap {
protected:
  np root;

public:
  treap() : root(node::NIL) {}

  treap(np root_) : root(root_) {}

  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1
  treap(Key *left, Key *right) : root(__make_tree(left, right)) {
    int n = right - left;
    std::vector<int> ps(n - 1);
    std::generate(ps.begin(), ps.end(), node::xor128);
    std::sort(ps.begin(), ps.end());
    std::queue<np> que;
    que.push(root);
    while (que.size()) {
      np n = que.front();
      que.pop();
      if (is_leaf(n))
        continue;
      n->priority = ps.back();
      ps.pop_back();
      que.push(n->left);
      que.push(n->right);
    }
  }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1
  np __make_tree(Key *left, Key *right) {
    int sz = right - left;
    Key *mid = left + sz / 2;
    if (sz == 0) {
      return node::NIL;
    } else if (sz == 1) {
      return new node(*mid, -1, node::NIL, node::NIL, 1, *mid);
    } else {
      np lc = __make_tree(left, mid);
      np rc = __make_tree(mid, right);
      return new node(-1, -1, lc, rc, sz, std::min(lc->min, rc->min));
    }
  }

public:
  int size() const { return root->size; }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  void set(int k, const Key &x) { __set(root, k, x); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  void __set(np n, int k, const Key &x) {
    if (n->size == 1) {
      n->key = x;
    } else if (k < n->left->size)
      __set(n->left, k, x);
    else
      __set(n->right, k - n->left->size, x);
    __update(n);
  }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key get(int k) const { return __get(root, k); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key __get(np n, int k) const {
    if (n->size == 1) {
      return n->key;
    } else if (k < n->left->size)
      return __get(n->left, k);
    else
      return __get(n->right, k - n->left->size);
  }

public:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key range_min(int l, int r) { return __range_min(root, l, r); }

protected:
  // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288
  Key __range_min(cnp n, int l, int r) const {
    l = std::max(l, 0);
    r = std::min(r, n->size);
    // std::cout << n << ' ' << l << ' ' << r << std::endl;
    if (l >= r)
      return INF;
    if (l == 0 && r == n->size)
      return n->min;
    Key res = INF;
    int sl = n->left->size;
    res = std::min(res, __range_min(n->left, l, r));
    res = std::min(res, __range_min(n->right, l - sl, r - sl));
    return res;
  }

public:
  void merge_from_left(treap &t) { root = __merge(t.root, root); }

  void merge_from_right(treap &t) { root = __merge(root, t.root); }

protected:
  // yukicoder 449 http://yukicoder.me/submissions/131855
  np __merge(np l, np r, int pri) const {
    if (is_leaf(l) && is_leaf(r))
      return new node(-1, pri, l, r, 2, std::min(l->key, r->key));
    if (is_leaf(l)) {
      r->left = __merge(l, r->left, pri);
      return __update(r);
    }
    if (is_leaf(r)) {
      l->right = __update(__merge(l->right, r, pri));
      return __update(l);
    }
    if (pri > l->priority && pri > r->priority) {
      return new node(-1, pri, l, r, l->size + r->size,
                      std::min(l->min, r->min));
    }
    if (pri < l->priority && pri < r->priority) {
      if (node::xor128() & 1) {
        l->right = __merge(l->right, r, pri);
        return __update(l);
      } else {
        r->left = __merge(l, r->left, pri);
        return __update(r);
      }
    }
    if (l->priority > r->priority) {
      l->right = __merge(l->right, r, pri);
      return __update(l);
    }
    {
      r->left = __merge(l, r->left, pri);
      return __update(r);
    }
  }

  np __merge(np l, np r) const {
    if (l == node::NIL)
      return r;
    if (r == node::NIL)
      return l;
    int pri = node::xor128();
    return __merge(l, r, pri);
  }

public:
  // yukicoder 449 http://yukicoder.me/submissions/131855
  std::pair<treap, treap> split_at(int k) {
    np l, r;
    std::tie(l, r) = __split_at(root, k);
    return std::make_pair(treap(l), treap(r));
  }

protected:
  // yukicoder 449 http://yukicoder.me/submissions/131855

  npair __split_at(np n, int k) const {
    if (n == node::NIL)
      return npair(node::NIL, node::NIL);
    else
      return __split_at_sub(n, k);
  }

  npair __split_at_sub(np n, int k) const {
    if (is_leaf(n))
      return k == 0 ? npair(node::NIL, n) : npair(n, node::NIL);
    if (k == n->left->size)
      return npair(n->left, n->right);
    if (k < n->left->size) {
      npair p = __split_at_sub(n->left, k);
      n->left = p.second;
      return npair(p.first, __update(n));
    } else {
      npair p = __split_at_sub(n->right, k - n->left->size);
      n->right = p.first;
      return npair(__update(n), p.second);
    }
  }

protected:
  np __update(np n) const {
    n->size = n->left->size + n->right->size + (n->left == node::NIL);
    n->min = std::min(
        {n->left->min, n->right->min, (n->left == node::NIL ? n->key : INF)});
    return n;
  }

public:
  std::string to_string() const {
    std::string res;
    __to_string(root, res);
    return res;
  }

protected:
  void __to_string(cnp n, std::string &res) const {
    res += "(";
    if (n != node::NIL) {
      __to_string(n->left, res);
      res += ")" + std::to_string(n->key) + "(";
      __to_string(n->right, res);
    }
    res += ")";
  }

public:
  void show() {
    return;
    puts("=== show ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show(root, 0);
    }
  }

  void show_array() {
    return;
    puts("=== show array ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show_array(root);
      std::cout << '\n';
    }
  }

  void __show(np n, int ofs) {
    using std::cout;
    using std::endl;
    using std::string;
    assert((n->left == node::NIL && n->right == node::NIL) ||
           (n->left != node::NIL && n->right != node::NIL));
    if (is_leaf(n)) { // leaf
      printf("%s-- (%d %d %d)\n", string(ofs * 8, ' ').c_str(), n->priority,
             n->min, n->key);
    } else {
      __show(n->left, ofs + 1);
      printf("%s(%d %d %d) < \n", string(ofs * 8, ' ').c_str(), n->priority,
             n->min, n->key);
      __show(n->right, ofs + 1);
    }
  }

  void __show_array(np n) {
    using std::cout;
    using std::endl;
    using std::string;
    assert((n->left == node::NIL && n->right == node::NIL) ||
           (n->left != node::NIL && n->right != node::NIL));
    if (is_leaf(n)) { // leaf
      printf("%d ", n->key);
    } else {
      __show_array(n->left);
      __show_array(n->right);
    }
  }

public:
  void shift(int l, int r) {
    // std::cout << "shift " << l << ' ' << r << std::endl;
    np a, b, c, d;
    std::tie(a, b) = __split_at(root, l);
    std::tie(b, c) = __split_at(b, r - l);

    // puts("cd");
    // treap(c).show_array();
    std::tie(c, d) = __split_at(c, 1);

    // puts("a");
    // treap(a).show_array();
    // puts("b");
    // treap(b).show_array();
    // puts("c");
    // treap(c).show_array();
    // puts("d");
    // treap(d).show_array();

    np ac = __merge(a, c);
    np bd = __merge(b, d);
    root = __merge(ac, bd);
  }
};

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) begin(c), end(c)

using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, q;
  cin >> n >> q;

  static int a[200010];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  treap t(a, a + n);

  t.show();
  t.show_array();

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    // cout << "== query " << x << ' ' << y << ' ' << z << " ==" << endl;
    if (x == 0) {
      t.shift(y, z);
    }
    if (x == 1) {
      // puts("=====");
      // Treap::show(root);
      cout << t.range_min(y, z + 1) << '\n';
    }
    if (x == 2) {
      t.set(y, z);
    }

    t.show();
    t.show_array();
  }
}