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

#include <bits/stdc++.h>
using namespace std;

int id = INT_MAX;
int merge(int l, int r) { return min(l, r); }

struct node {
  int val;
  int all;
  node *lch, *rch;
  int size;
  node(int v) : val(v), all(v), size(1) { lch = rch = nullptr; }
};

int count(node *t) { return t == nullptr ? 0 : t->size; }
int que(node *t) { return t == nullptr ? id : t->all; }
node *update(node *t) {
  t->size = count(t->lch) + count(t->rch) + 1;
  t->all = merge(que(t->lch), merge(t->val, que(t->rch)));
  return t;
}
node *merge(node *l, node *r) {
  if (l == nullptr || r == nullptr)
    return l == nullptr ? r : l;
  if (rand() % (l->size + r->size) < l->size) {
    l->rch = merge(l->rch, r);
    return update(l);
  } else {
    r->lch = merge(l, r->lch);
    return update(r);
  }
}
pair<node *, node *> split(node *t, int k) {
  if (t == nullptr)
    return make_pair(nullptr, nullptr);
  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 *t, int k, int v) {
  pair<node *, node *> s = split(t, k);
  node *r = merge(s.first, new node(v));
  r = merge(r, s.second);
  return r;
}
node *erase(node *t, int k) {
  pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1);
  if (s2.first != nullptr)
    delete s2.first;
  return merge(s1.first, s2.second);
}
node *find(node *t, int k) {
  if (t == nullptr)
    return t;
  int c = count(t->lch);
  return k < c ? find(t->lch, k) : k == c ? t : find(t->rch, k - (c + 1));
}
int find(node *t, int l, int r) {
  if (t == nullptr)
    return id;
  if (r < 0 || l >= count(t))
    return id;
  if (l <= 0 && r >= count(t))
    return t->all;
  int c = count(t->lch);
  return merge(
      t->lch == nullptr ? id : find(t->lch, l, r),
      merge(l <= c && c < r ? t->val : id,
            t->rch == nullptr ? id : find(t->rch, l - (c + 1), r - (c + 1))));
}
int cnt(node *t, int v) {
  if (t == nullptr)
    return 0;
  if (t->val < v)
    return count(t->lch) + 1 + cnt(t->rch, v);
  if (t->val == v)
    return count(t->lch);
  return cnt(t->lch, v);
}
void print(node *t) {
  if (t == nullptr)
    return;
  print(t->lch);
  printf(" %d", t->val);
  print(t->rch);
}
node *shift(node *t, int l, int r) {
  pair<node *, node *> s1 = split(t, l), s2 = split(s1.second, r - l),
                       s3 = split(s2.second, 1);
  return merge(s1.first, merge(s3.first, merge(s2.first, s3.second)));
}
void change(node *t, int k, int v) {
  if (t == nullptr)
    return;
  int c = count(t->lch);
  if (k < c) {
    change(t->lch, k, v);
    update(t);
  } else if (k > c) {
    change(t->rch, k - (c + 1), v);
    update(t);
  } else {
    t->val = v;
    update(t);
  }
}

int main() {
  cin.sync_with_stdio(false);
  int n, q, x, y, z;
  cin >> n >> q;
  node *root = nullptr;
  srand((unsigned int)time(NULL));
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    root = insert(root, i, a);
  }
  while (q--) {
    cin >> x >> y >> z;
    if (x == 0) {
      root = shift(root, y, z);
    } else if (x == 1) {
      printf("%d\\n", find(root, y, z + 1));
    } else {
      change(root, y, z);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int id = INT_MAX;
int merge(int l, int r) { return min(l, r); }

struct node {
  int val;
  int all;
  node *lch, *rch;
  int size;
  node(int v) : val(v), all(v), size(1) { lch = rch = nullptr; }
};

random_device rd;

int count(node *t) { return t == nullptr ? 0 : t->size; }
int que(node *t) { return t == nullptr ? id : t->all; }
node *update(node *t) {
  t->size = count(t->lch) + count(t->rch) + 1;
  t->all = merge(que(t->lch), merge(t->val, que(t->rch)));
  return t;
}
node *merge(node *l, node *r) {
  if (l == nullptr || r == nullptr)
    return l == nullptr ? r : l;
  if (rd() % (l->size + r->size) < (unsigned)r->size) {
    l->rch = merge(l->rch, r);
    return update(l);
  } else {
    r->lch = merge(l, r->lch);
    return update(r);
  }
}
pair<node *, node *> split(node *t, int k) {
  if (t == nullptr)
    return make_pair(nullptr, nullptr);
  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 *t, int k, int v) {
  pair<node *, node *> s = split(t, k);
  node *r = merge(s.first, new node(v));
  r = merge(r, s.second);
  return r;
}
node *erase(node *t, int k) {
  pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1);
  if (s2.first != nullptr)
    delete s2.first;
  return merge(s1.first, s2.second);
}
node *find(node *t, int k) {
  if (t == nullptr)
    return t;
  int c = count(t->lch);
  return k < c ? find(t->lch, k) : k == c ? t : find(t->rch, k - (c + 1));
}
int find(node *t, int l, int r) {
  if (t == nullptr)
    return id;
  if (r < 0 || l >= count(t))
    return id;
  if (l <= 0 && r >= count(t))
    return t->all;
  int c = count(t->lch);
  return merge(
      t->lch == nullptr ? id : find(t->lch, l, r),
      merge(l <= c && c < r ? t->val : id,
            t->rch == nullptr ? id : find(t->rch, l - (c + 1), r - (c + 1))));
}
int cnt(node *t, int v) {
  if (t == nullptr)
    return 0;
  if (t->val < v)
    return count(t->lch) + 1 + cnt(t->rch, v);
  if (t->val == v)
    return count(t->lch);
  return cnt(t->lch, v);
}
void print(node *t) {
  if (t == nullptr)
    return;
  print(t->lch);
  printf(" %d", t->val);
  print(t->rch);
}
node *shift(node *t, int l, int r) {
  pair<node *, node *> s1 = split(t, l), s2 = split(s1.second, r - l),
                       s3 = split(s2.second, 1);
  return merge(s1.first, merge(s3.first, merge(s2.first, s3.second)));
}
void change(node *t, int k, int v) {
  if (t == nullptr)
    return;
  int c = count(t->lch);
  if (k < c) {
    change(t->lch, k, v);
    update(t);
  } else if (k > c) {
    change(t->rch, k - (c + 1), v);
    update(t);
  } else {
    t->val = v;
    update(t);
  }
}

int main() {
  cin.sync_with_stdio(false);
  int n, q, x, y, z;
  cin >> n >> q;
  node *root = nullptr;
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    root = insert(root, i, a);
  }
  while (q--) {
    cin >> x >> y >> z;
    if (x == 0) {
      root = shift(root, y, z);
    } else if (x == 1) {
      printf("%d\\n", find(root, y, z + 1));
    } else {
      change(root, y, z);
    }
  }
  return 0;
}