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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

inline int xor32() {
  static int y = 2463534242;
  y = y ^ (y << 13);
  y = y ^ (y >> 17);
  return (y = y ^ (y << 5));
}

struct Node {
  int Value;
  int SubTreeSize;
  Node *Lch, *Rch;

  int RMQ;

  Node(int V) : Value(V), SubTreeSize(1), RMQ(V) {
    Lch = (Node *)NULL;
    Rch = (Node *)NULL;
  };
};
inline int Count(Node *t) {
  if (t == (Node *)NULL)
    return (0);
  else
    return (t->SubTreeSize);
}
inline int Sum(Node *t) {
  if (t == (Node *)NULL)
    return (INF);
  else
    return (t->RMQ);
}

inline Node *Update(Node *t) {
  t->SubTreeSize = Count(t->Lch) + Count(t->Rch) + 1;
  t->RMQ = min(Sum(t->Lch), min(Sum(t->Rch), t->Value));
  return (t);
}
inline void Push(Node *t) {}

inline Node *MakeRoot(int value) { return (new Node(value)); }

Node *Merge(Node *l, Node *r) {
  Push(l), Push(r);
  if (l == (Node *)NULL)
    return (r);
  if (r == (Node *)NULL)
    return (l);
  int Left = l->SubTreeSize;
  int Right = r->SubTreeSize;
  if (rand() % (Left + Right) < Left) { // ???(left)??????
    l->Rch = Merge(l->Rch, r);
    return (Update(l));
  } else { // ???(right)??????
    r->Lch = Merge(l, r->Lch);
    return (Update(r));
  }
}

pair<Node *, Node *> Split(Node *t, int k) // [0, k), [k, n)
{
  if (t == (Node *)NULL)
    return (make_pair((Node *)NULL, (Node *)NULL));
  Push(t);
  if (k <= Count(t->Lch)) { // ?????´?????????
    pair<Node *, Node *> s = Split(t->Lch, k);
    t->Lch = s.second;
    return (make_pair(s.first, Update(t)));
  } else { // ?????´?????????
    pair<Node *, Node *> s = Split(t->Rch, k - Count(t->Lch) - 1);
    t->Rch = s.first;
    return (make_pair(Update(t), s.second));
  }
}

Node *Insert(Node *root, int pos, int value) {
  Node *p = MakeRoot(value);
  pair<Node *, Node *> s = Split(root, pos);
  return (Merge(Merge(s.first, p), s.second));
}
Node *Erase(Node *root, int pos) {
  pair<Node *, Node *> s = Split(root, pos);
  pair<Node *, Node *> t = Split(s.second, 1);
  delete t.first;
  return (Merge(s.first, t.second));
}

void Dump(Node *root) {
  if (root == (Node *)NULL)
    return;
  cout << "(";
  Dump(root->Lch);
  cout << "" << root->Value << "";
  Dump(root->Rch);
  cout << ")";
}

int main() {
  Node *root = NULL;
  int n, q, a, x, y, z;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    root = Insert(root, i, a);
  }
  while (q--) {
    scanf("%d %d %d", &x, &y, &z);
    if (x == 0) {
      ++z;
      pair<Node *, Node *> a, b, c;
      c = Split(root, z);
      b = Split(c.first, z - 1);
      a = Split(b.first, y);
      root = Merge(Merge(Merge(a.first, b.second), a.second), c.second);
    } else if (x == 1) {
      ++z;
      pair<Node *, Node *> p, q;
      p = Split(root, y);
      q = Split(p.second, z - y);
      printf("%d\\n", Sum(q.first));
      root = Merge(p.first, Merge(q.first, q.second));
    } else {
      root = Erase(root, y);
      root = Insert(root, y, z);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

inline int xor32() {
  static int y = 2463534242;
  y = y ^ (y << 13);
  y = y ^ (y >> 17);
  return (y = y ^ (y << 5));
}

struct Node {
  int Value;
  int SubTreeSize;
  Node *Lch, *Rch;

  int RMQ;

  Node(int V) : Value(V), SubTreeSize(1), RMQ(V) {
    Lch = (Node *)NULL;
    Rch = (Node *)NULL;
  };
};
inline int Count(Node *t) {
  if (t == (Node *)NULL)
    return (0);
  else
    return (t->SubTreeSize);
}
inline int Sum(Node *t) {
  if (t == (Node *)NULL)
    return (INF);
  else
    return (t->RMQ);
}

inline Node *Update(Node *t) {
  t->SubTreeSize = Count(t->Lch) + Count(t->Rch) + 1;
  t->RMQ = min(Sum(t->Lch), min(Sum(t->Rch), t->Value));
  return (t);
}
inline void Push(Node *t) {}

inline Node *MakeRoot(int value) { return (new Node(value)); }

Node *Merge(Node *l, Node *r) {
  Push(l), Push(r);
  if (l == (Node *)NULL)
    return (r);
  if (r == (Node *)NULL)
    return (l);
  int Left = l->SubTreeSize;
  int Right = r->SubTreeSize;
  if (xor32() % (Left + Right) < Left) { // ???(left)??????
    l->Rch = Merge(l->Rch, r);
    return (Update(l));
  } else { // ???(right)??????
    r->Lch = Merge(l, r->Lch);
    return (Update(r));
  }
}

pair<Node *, Node *> Split(Node *t, int k) // [0, k), [k, n)
{
  if (t == (Node *)NULL)
    return (make_pair((Node *)NULL, (Node *)NULL));
  Push(t);
  if (k <= Count(t->Lch)) { // ?????´?????????
    pair<Node *, Node *> s = Split(t->Lch, k);
    t->Lch = s.second;
    return (make_pair(s.first, Update(t)));
  } else { // ?????´?????????
    pair<Node *, Node *> s = Split(t->Rch, k - Count(t->Lch) - 1);
    t->Rch = s.first;
    return (make_pair(Update(t), s.second));
  }
}

Node *Insert(Node *root, int pos, int value) {
  Node *p = MakeRoot(value);
  pair<Node *, Node *> s = Split(root, pos);
  return (Merge(Merge(s.first, p), s.second));
}
Node *Erase(Node *root, int pos) {
  pair<Node *, Node *> s = Split(root, pos);
  pair<Node *, Node *> t = Split(s.second, 1);
  delete t.first;
  return (Merge(s.first, t.second));
}

void Dump(Node *root) {
  if (root == (Node *)NULL)
    return;
  cout << "(";
  Dump(root->Lch);
  cout << "" << root->Value << "";
  Dump(root->Rch);
  cout << ")";
}

int main() {
  Node *root = NULL;
  int n, q, a, x, y, z;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    root = Insert(root, i, a);
  }
  while (q--) {
    scanf("%d %d %d", &x, &y, &z);
    if (x == 0) {
      ++z;
      pair<Node *, Node *> a, b, c;
      c = Split(root, z);
      b = Split(c.first, z - 1);
      a = Split(b.first, y);
      root = Merge(Merge(Merge(a.first, b.second), a.second), c.second);
    } else if (x == 1) {
      ++z;
      pair<Node *, Node *> p, q;
      p = Split(root, y);
      q = Split(p.second, z - y);
      printf("%d\\n", Sum(q.first));
      root = Merge(p.first, Merge(q.first, q.second));
    } else {
      root = Erase(root, y);
      root = Insert(root, y, z);
    }
  }
}