Problem p00998 (C++) — buggy submission, verdict: 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;
  node *left, *right;
  int size;
  Key min;

  static node *const NIL;

  node() {}

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

  node(const Key &key_, node *left_, node *right_, int size_, const Key &min_)
      : key(key_), 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;
  }
};

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

node *const node::NIL = new node(Key(), 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)) {}

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, node::NIL, node::NIL, 1, *mid);
    } else {
      np lc = __make_tree(left, mid);
      np rc = __make_tree(mid, right);
      return new node(-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);
    if (l >= r)
      return INF;
    if (l == 0 && r == n->size) {
      // dump(l);
      // dump(r);
      // dump(n->size);
      // dump(n->min);
      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_sub(np l, np r) const {
    int sl = l->size, sr = r->size;
    int rand = node::xor128() % (sl + sr + 1);
    if (rand < sl) {
      if (is_leaf(l)) {
        return new node(-1, l, r, l->size + r->size, std::min(l->min, r->min));
      } else {
        l->right = __merge_sub(l->right, r);
        return __update(l);
      }
    } else if (rand == sl) {
      return new node(-1, l, r, l->size + r->size, std::min(l->min, r->min));
    } else {
      if (is_leaf(r)) {
        return new node(-1, l, r, l->size + r->size, std::min(l->min, r->min));
      } else {
        r->left = __merge_sub(l, r->left);
        return __update(r);
      }
    }
    // if(sl == sr){
    //     return new node(-1, l, r, l->size + r->size, std::min(l->min,
    //     r->min));
    // } else if (is_leaf(l)) {
    //     r->left = __merge_sub(l, r->left);
    //     return __update(r);
    // } else if (is_leaf(r)) {
    //     l->right = __merge_sub(l->right, r);
    //     return __update(l);
    // } else  {
    //     // std::cout << "L";
    //     l->right = __merge_sub(l->right, r);
    //     return __update(l);
    // } else {
    //     // std::cout << "R";
    //     r->left = __merge_sub(l, r->left);
    //     return __update(r);
    // }
  }

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

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);
    } else if (k < n->left->size) {
      npair p = __split_at_sub(n->left, k);
      n->left = p.second;
      return npair(p.first, __update(n));
    } else if (k == n->left->size) {
      return npair(n->left, n->right);
    } 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:
  void show() {
    puts("=== show ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show(root, 0);
    }
    puts("");
  }

  void show_array() {
    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("%d (%d) ", ofs, n->size);
    } else {
      __show(n->left, ofs + 1);
      printf("%d (%d) ", ofs, n->size);
      __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) {
      cout << t.range_min(y, z + 1) << '\\n';
    }
    if (x == 2) {
      t.set(y, z);
    }

    // t.show();
  }
  // t.show();
}

Error: TLE

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;
  node *left, *right;
  int size;
  Key min;

  static node *const NIL;

  node() {}

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

  node(const Key &key_, node *left_, node *right_, int size_, const Key &min_)
      : key(key_), 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;
  }
};

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

node *const node::NIL = new node(Key(), 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)) {}

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, node::NIL, node::NIL, 1, *mid);
    } else {
      np lc = __make_tree(left, mid);
      np rc = __make_tree(mid, right);
      return new node(-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);
    if (l >= r)
      return INF;
    if (l == 0 && r == n->size) {
      // dump(l);
      // dump(r);
      // dump(n->size);
      // dump(n->min);
      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_sub(np l, np r) const {
    int sl = l->size, sr = r->size;
    if (sl == sr) {
      return new node(-1, l, r, l->size + r->size, std::min(l->min, r->min));
    } else if (is_leaf(l)) {
      r->left = __merge_sub(l, r->left);
      return __update(r);
    } else if (is_leaf(r)) {
      l->right = __merge_sub(l->right, r);
      return __update(l);
    } else if ((int)node::xor128() % (sl + sr) < sl) {
      // std::cout << "L";
      l->right = __merge_sub(l->right, r);
      return __update(l);
    } else {
      // std::cout << "R";
      r->left = __merge_sub(l, r->left);
      return __update(r);
    }
  }

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

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);
    } else if (k < n->left->size) {
      npair p = __split_at_sub(n->left, k);
      n->left = p.second;
      return npair(p.first, __update(n));
    } else if (k == n->left->size) {
      return npair(n->left, n->right);
    } 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:
  void show() {
    puts("=== show ===");
    if (root == node::NIL) {
      std::cout << "nil" << std::endl;
    } else {
      __show(root, 0);
    }
  }

  void show_array() {
    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("%d ", ofs);
    } else {
      __show(n->left, ofs + 1);
      // printf("%s(%d %d) < \\n", string(ofs*8, ' ').c_str(), 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) {
      cout << t.range_min(y, z + 1) << '\\n';
    }
    if (x == 2) {
      t.set(y, z);
    }

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