Problem p00998 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <ctime>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#define dump(x) std::cerr << __LINE__ << ":\\t" #x " = " << x << std::endl
using key_t = int;
const key_t INF = 1000000000;
const int MAX_N = 1000010; // ?????\\???????????° + EPS

enum { L, R };

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;
}

struct node {
  key_t key;
  node *ch[2];
  int size;
  key_t min;

  static node *const nil;

  node() {}

  node(const key_t &x) : key(x), size(1), min(x) {}

  node(node *left_, node *right_, int size_, const key_t &min_)
      : size(size_), min(min_) {
    ch[L] = left_;
    ch[R] = right_;
  }

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

  bool is_leaf() const { return size == 1; }
};

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

node *const node::nil = new node(nullptr, nullptr, 0, INF);

class rbst {
public:
  np root;

  rbst() : root(node::nil) {}

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

  rbst(key_t *left, key_t *right) : root(make_tree_rec(left, right)) {}

  np make_tree_rec(key_t *left, key_t *right) {
    int sz = right - left;
    key_t *mid = left + sz / 2;
    if (sz == 0) {
      return node::nil;
    } else if (sz == 1) {
      return new node(*mid);
    } else {
      np lc = make_tree_rec(left, mid);
      np rc = make_tree_rec(mid, right);
      return new node(lc, rc, sz, std::min(lc->min, rc->min));
    }
  }

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

  void set(int k, const key_t &x) { set(root, k, x); }

  void set(np n, int k, const key_t &x) {
    if (n->is_leaf()) {
      n->key = n->min = x;
    } else {
      if (k < n->ch[L]->size)
        set(n->ch[L], k, x);
      else
        set(n->ch[R], k - n->ch[L]->size, x);
      update(n);
    }
  }

  key_t get(np n, int k) const {
    if (n->size == 1)
      return n->key;
    else if (k < n->ch[L]->size)
      return get(n->ch[L], k);
    else
      return get(n->ch[R], k - n->ch[L]->size);
  }

  key_t range_min(int l, int r) { return range_min(root, l, r); }

  key_t 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)
      return n->min;
    key_t res = INF;
    int sl = n->ch[L]->size;
    res = std::min(res, range_min(n->ch[L], l, r));
    res = std::min(res, range_min(n->ch[R], l - sl, r - sl));
    return res;
  }

  np merge(np l, np r) const {
    if (l == node::nil)
      return r;
    if (r == node::nil)
      return l;
    return merge_sub(l, r);
  }

  np merge_sub(np l, np r) const {
    int sl = l->size, sr = r->size;
    int rand = xor128() % (sl + sr + 1);
    if (rand < sl) {
      if (l->is_leaf())
        return new node(l, r, l->size + r->size, std::min(l->min, r->min));
      l->ch[R] = merge_sub(l->ch[R], r);
      return update(l);
    } else if (rand == sl) {
      return new node(l, r, l->size + r->size, std::min(l->min, r->min));
    } else {
      if (r->is_leaf())
        return new node(l, r, l->size + r->size, std::min(l->min, r->min));
      r->ch[L] = merge_sub(l, r->ch[L]);
      return update(r);
    }
  }

  std::pair<rbst, rbst> split_at(int k) {
    np l, r;
    std::tie(l, r) = split_at(root, k);
    return std::make_pair(rbst(l), rbst(r));
  }

  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 (n->is_leaf())
      return k == 0 ? npair(node::nil, n) : npair(n, node::nil);
    if (k < n->ch[L]->size) {
      npair p = split_at_sub(n->ch[L], k);
      n->ch[L] = p.second;
      return npair(p.first, update(n));
    }
    if (k == n->ch[L]->size)
      return npair(n->ch[L], n->ch[R]);
    npair p = split_at_sub(n->ch[R], k - n->ch[L]->size);
    n->ch[R] = p.first;
    return npair(update(n), p.second);
  }

  np insert_at(np n, int k, const key_t &x) {
    return n == node::nil ? new node(x) : insert_at_sub(n, k, x);
  }

  np insert_at_sub(np n, int k, const key_t &x) {
    // dump(k);
    if (n->is_leaf()) {
      np par = k == 0 ? new node(new node(x), n, 2, INF)
                      : new node(n, new node(x), 2, INF);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      return update(par);
    } else {
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      if (k < n->ch[L]->size)
        n->ch[L] = insert_at_sub(n->ch[L], k, x);
      else
        n->ch[R] = insert_at_sub(n->ch[R], k - n->ch[L]->size, x);
      n = update(n);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      int sl = n->ch[L]->size, sr = n->ch[R]->size;
      int rand = xor128() % (sl + sr + 1);
      return (rand < sl && sl != 1)   ? rotate<R>(n)
             : (rand > sl && sr != 1) ? rotate<L>(n)
                                      : n;
    }
  }

  np erase_at(np n, int k) {
    // dump(k);
    if (n->is_leaf()) {
      return node::nil;
    } else if (k == 0 && n->ch[L]->is_leaf()) {
      return n->ch[R];
    } else if (k == n->size - 1 && n->ch[R]->is_leaf()) {
      return n->ch[L];
    } else {
      if (k < n->ch[L]->size)
        n->ch[L] = erase_at(n->ch[L], k);
      else
        n->ch[R] = erase_at(n->ch[R], k - n->ch[L]->size);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      n = update(n);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      int sl = n->ch[L]->size, sr = n->ch[R]->size;
      int rand = xor128() % (sl + sr + 1);
      return (rand < sl && sl != 1)   ? rotate<R>(n)
             : (rand > sl && sr != 1) ? rotate<L>(n)
                                      : n;
    }
  }

  template <int dir> np rotate(np n) {
    np par = n->ch[1 - dir];
    n->ch[1 - dir] = par->ch[dir];
    par->ch[dir] = update(n);
    return update(par);
  }

  np update(np n) const {
    // assert(n->ch[L] != node::nil);
    n->size = n->ch[L]->size + n->ch[R]->size + (n->is_leaf() ? 1 : 0);
    n->min =
        std::min({n->ch[L]->min, n->ch[R]->min, (n->is_leaf() ? n->key : INF)});
    return n;
  }

  void shift(int l, int r) {
    np a, b, c, d;
    std::tie(a, b) = split_at(root, l);
    std::tie(b, c) = split_at(b, r - l);
    std::tie(c, d) = split_at(c, 1);
    np ac = merge(a, c);
    np bd = merge(b, d);
    root = merge(ac, bd);
  }

  void shift2(int l, int r) {
    key_t x = get(root, r);
    root = erase_at(root, r);
    root = insert_at(root, l, x);
  }

  void show_array() {
    show_array(root);
    std::cout << '\\n';
  }

  void show_array(cnp n) const {
    if (n->is_leaf()) {
      std::cout << n->key << ' ';
      std::cout << std::flush;
    } else {
      show_array(n->ch[L]);
      show_array(n->ch[R]);
    }
  }
};

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)

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];
  rbst t(a, a + n);

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      // dump(y);
      // dump(z);
      // t.show_array();
      t.shift2(y, z);
      // t.show_array();
    }
    if (x == 1) {
      cout << t.range_min(y, z + 1) << '\\n';
    }
    if (x == 2) {
      t.set(y, z);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <ctime>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#define dump(x) std::cerr << __LINE__ << ":\\t" #x " = " << x << std::endl
using key_t = int;
const key_t INF = 1000000000;
const int MAX_N = 1000010; // ?????\\??????????° + EPS

enum { L, R };

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;
}

struct node {
  key_t key;
  node *ch[2];
  int size;
  key_t min;

  static node *const nil;

  node() {}

  node(const key_t &x) : key(x), size(1), min(x) {}

  node(node *left_, node *right_, int size_, const key_t &min_)
      : size(size_), min(min_) {
    ch[L] = left_;
    ch[R] = right_;
  }

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

  bool is_leaf() const { return size == 1; }
};

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

node *const node::nil = new node(nullptr, nullptr, 0, INF);

class rbst {
public:
  np root;

  rbst() : root(node::nil) {}

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

  rbst(key_t *left, key_t *right) : root(make_tree_rec(left, right)) {}

  np make_tree_rec(key_t *left, key_t *right) {
    int sz = right - left;
    key_t *mid = left + sz / 2;
    if (sz == 0) {
      return node::nil;
    } else if (sz == 1) {
      return new node(*mid);
    } else {
      np lc = make_tree_rec(left, mid);
      np rc = make_tree_rec(mid, right);
      return new node(lc, rc, sz, std::min(lc->min, rc->min));
    }
  }

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

  void set(int k, const key_t &x) { set(root, k, x); }

  void set(np n, int k, const key_t &x) {
    if (n->is_leaf()) {
      n->key = n->min = x;
    } else {
      if (k < n->ch[L]->size)
        set(n->ch[L], k, x);
      else
        set(n->ch[R], k - n->ch[L]->size, x);
      update(n);
    }
  }

  key_t get(np n, int k) const {
    if (n->size == 1)
      return n->key;
    else if (k < n->ch[L]->size)
      return get(n->ch[L], k);
    else
      return get(n->ch[R], k - n->ch[L]->size);
  }

  key_t range_min(int l, int r) { return range_min(root, l, r); }

  key_t 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)
      return n->min;
    key_t res = INF;
    int sl = n->ch[L]->size;
    res = std::min(res, range_min(n->ch[L], l, r));
    res = std::min(res, range_min(n->ch[R], l - sl, r - sl));
    return res;
  }

  np merge(np l, np r) const {
    if (l == node::nil)
      return r;
    if (r == node::nil)
      return l;
    return merge_sub(l, r);
  }

  np merge_sub(np l, np r) const {
    int sl = l->size, sr = r->size;
    int rand = xor128() % (sl + sr + 1);
    if (rand < sl) {
      if (l->is_leaf())
        return new node(l, r, l->size + r->size, std::min(l->min, r->min));
      l->ch[R] = merge_sub(l->ch[R], r);
      return update(l);
    } else if (rand == sl) {
      return new node(l, r, l->size + r->size, std::min(l->min, r->min));
    } else {
      if (r->is_leaf())
        return new node(l, r, l->size + r->size, std::min(l->min, r->min));
      r->ch[L] = merge_sub(l, r->ch[L]);
      return update(r);
    }
  }

  std::pair<rbst, rbst> split_at(int k) {
    np l, r;
    std::tie(l, r) = split_at(root, k);
    return std::make_pair(rbst(l), rbst(r));
  }

  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 (n->is_leaf())
      return k == 0 ? npair(node::nil, n) : npair(n, node::nil);
    if (k < n->ch[L]->size) {
      npair p = split_at_sub(n->ch[L], k);
      n->ch[L] = p.second;
      return npair(p.first, update(n));
    }
    if (k == n->ch[L]->size)
      return npair(n->ch[L], n->ch[R]);
    npair p = split_at_sub(n->ch[R], k - n->ch[L]->size);
    n->ch[R] = p.first;
    return npair(update(n), p.second);
  }

  np insert_at(np n, int k, const key_t &x) {
    return n == node::nil ? new node(x) : insert_at_sub(n, k, x);
  }

  np insert_at_sub(np n, int k, const key_t &x) {
    // dump(k);
    if (n->is_leaf()) {
      np par = k == 0 ? new node(new node(x), n, 2, INF)
                      : new node(n, new node(x), 2, INF);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      return update(par);
    } else {
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      if (k < n->ch[L]->size)
        n->ch[L] = insert_at_sub(n->ch[L], k, x);
      else
        n->ch[R] = insert_at_sub(n->ch[R], k - n->ch[L]->size, x);
      n = update(n);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      return n;
      int sl = n->ch[L]->size, sr = n->ch[R]->size;
      int rand = xor128() % (sl + sr + 1);
      return (rand < sl && sl != 1)   ? rotate<R>(n)
             : (rand > sl && sr != 1) ? rotate<L>(n)
                                      : n;
    }
  }

  np erase_at(np n, int k) {
    // dump(k);
    if (n->is_leaf()) {
      return node::nil;
    } else if (k == 0 && n->ch[L]->is_leaf()) {
      return n->ch[R];
    } else if (k == n->size - 1 && n->ch[R]->is_leaf()) {
      return n->ch[L];
    } else {
      if (k < n->ch[L]->size)
        n->ch[L] = erase_at(n->ch[L], k);
      else
        n->ch[R] = erase_at(n->ch[R], k - n->ch[L]->size);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      n = update(n);
      assert(n->ch[L] != node::nil && n->ch[R] != node::nil);
      int sl = n->ch[L]->size, sr = n->ch[R]->size;
      int rand = xor128() % (sl + sr + 1);
      return (rand < sl && sl != 1)   ? rotate<R>(n)
             : (rand > sl && sr != 1) ? rotate<L>(n)
                                      : n;
    }
  }

  template <int dir> np rotate(np n) {
    np par = n->ch[1 - dir];
    n->ch[1 - dir] = par->ch[dir];
    par->ch[dir] = update(n);
    return update(par);
  }

  np update(np n) const {
    // assert(n->ch[L] != node::nil);
    n->size = n->ch[L]->size + n->ch[R]->size + (n->is_leaf() ? 1 : 0);
    n->min =
        std::min({n->ch[L]->min, n->ch[R]->min, (n->is_leaf() ? n->key : INF)});
    return n;
  }

  void shift(int l, int r) {
    np a, b, c, d;
    std::tie(a, b) = split_at(root, l);
    std::tie(b, c) = split_at(b, r - l);
    std::tie(c, d) = split_at(c, 1);
    np ac = merge(a, c);
    np bd = merge(b, d);
    root = merge(ac, bd);
  }

  void shift2(int l, int r) {
    key_t x = get(root, r);
    root = erase_at(root, r);
    root = insert_at(root, l, x);
  }

  void show_array() {
    show_array(root);
    std::cout << '\\n';
  }

  void show_array(cnp n) const {
    if (n->is_leaf()) {
      std::cout << n->key << ' ';
      std::cout << std::flush;
    } else {
      show_array(n->ch[L]);
      show_array(n->ch[R]);
    }
  }
};

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)

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];
  rbst t(a, a + n);

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      // dump(y);
      // dump(z);
      // t.show_array();
      t.shift2(y, z);
      // t.show_array();
    }
    if (x == 1) {
      cout << t.range_min(y, z + 1) << '\\n';
    }
    if (x == 2) {
      t.set(y, z);
    }
  }
}