Problem p00998 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v;
  ll w;
};

const int MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

struct Splay {
  Splay *lch, *rch;
  int d, val;
  Splay(int _val = 0) : lch(0), rch(0), d(1), val(_val) {}
};
typedef pair<Splay *, Splay *> P;

Splay *update(Splay *x) {
  x->d = x->lch->d + x->rch->d;
  x->val = min(x->lch->val, x->rch->val);
  return x;
}

Splay *rotr(Splay *x) {
  Splay *y = x->lch;
  x->lch = y->rch;
  y->rch = x;
  update(x);
  return update(y);
}

Splay *rotl(Splay *x) {
  Splay *y = x->rch;
  x->rch = y->lch;
  y->lch = x;
  update(x);
  return update(y);
}

/*
void propagate(Splay* x) {
        x->lch->dval += x->dval;
        x->rch->dval += x->dval;
        x->dval = 0;
}
*/

Splay *splay(Splay *x, int m) {
  if (m == 0 || m == x->d)
    return x;
  int d1 = x->lch->d;
  if (m < d1) {
    int d2 = x->lch->lch->d;
    if (m < d2) {
      x->lch->lch = splay(x->lch->lch, m);
      return rotr(rotr(x));
    } else if (m > d2) {
      x->lch->rch = splay(x->lch->rch, m - d2);
      x->lch = rotl(x->lch);
      return rotr(x);
    } else {
      x->lch = splay(x->lch, m);
      return rotr(x);
    }
  } else if (m > d1) {
    int d2 = x->rch->lch->d;
    if (false && m < d1 + d2) {

    } else if (false && m > d1 + d2) {

    } else {
      x->rch = splay(x->rch, m - d1);
      return rotl(x);
    }
  } else
    return x;
}

Splay *merge(Splay *x, Splay *y) {
  if (!x)
    return y;
  if (!y)
    return x;
  Splay *z = new Splay();
  z->lch = x;
  z->rch = y;
  return update(z);
}

Splay *merges(vector<Splay *> xs) {
  Splay *x = NULL;
  for (Splay *y : xs)
    x = merge(x, y);
  return x;
}

P split(Splay *x, int m) {
  if (m == 0)
    return P(NULL, x);
  if (m == x->d)
    return P(x, NULL);
  x = splay(x, m);
  P p(x->lch, x->rch);
  delete x;
  return p;
}

vector<Splay *> splits(Splay *x, vector<int> is) {
  reverse(is.begin(), is.end());
  vector<Splay *> xs;
  for (int i : is) {
    P p = split(x, i);
    x = p.first;
    xs.pb(p.second);
  }
  xs.pb(x);
  reverse(xs.begin(), xs.end());
  return xs;
}

int get_val(Splay *&x, int i) {
  P p = split(x, i);
  cout << p.second->d << endl;
  P q = split(p.second, 1);
  cout << q.first << endl;
  int val = q.first->val;
  x = merge(p.first, merge(q.first, q.second));
  return val;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  rep(i, N) scanf("%d", &a[i]);
  Splay *x = NULL;
  rep(i, N) x = merge(x, new Splay(a[i]));
  while (Q--) {
    int q;
    scanf("%d", &q);
    if (q == 0) {
      int l, r;
      scanf("%d%d", &l, &r);
      r++;
      vector<Splay *> xs = splits(x, {l, r - 1, r});
      swap(xs[1], xs[2]);
      x = merges(xs);
    }
    if (q == 1) {
      int l, r;
      scanf("%d%d", &l, &r);
      r++;
      vector<Splay *> xs = splits(x, {l, r});
      int val = xs[1]->val;
      x = merges(xs);
      printf("%d\n", val);
    }
    if (q == 2) {
      int i, val;
      scanf("%d%d", &i, &val);
      vector<Splay *> xs = splits(x, {i, i + 1});
      xs[1]->val = val;
      x = merges(xs);
    }
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v;
  ll w;
};

const int MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

struct Splay {
  Splay *lch, *rch;
  int d, val;
  Splay(int _val = 0) : lch(0), rch(0), d(1), val(_val) {}
};
typedef pair<Splay *, Splay *> P;

Splay *update(Splay *x) {
  x->d = x->lch->d + x->rch->d;
  x->val = min(x->lch->val, x->rch->val);
  return x;
}

Splay *rotr(Splay *x) {
  Splay *y = x->lch;
  x->lch = y->rch;
  y->rch = x;
  update(x);
  return update(y);
}

Splay *rotl(Splay *x) {
  Splay *y = x->rch;
  x->rch = y->lch;
  y->lch = x;
  update(x);
  return update(y);
}

/*
void propagate(Splay* x) {
        x->lch->dval += x->dval;
        x->rch->dval += x->dval;
        x->dval = 0;
}
*/

Splay *splay(Splay *x, int m) {
  if (m == 0 || m == x->d)
    return x;
  int d1 = x->lch->d;
  if (m < d1) {
    int d2 = x->lch->lch->d;
    if (m < d2) {
      x->lch->lch = splay(x->lch->lch, m);
      return rotr(rotr(x));
    } else if (m > d2) {
      x->lch->rch = splay(x->lch->rch, m - d2);
      x->lch = rotl(x->lch);
      return rotr(x);
    } else {
      x->lch = splay(x->lch, m);
      return rotr(x);
    }
  } else if (m > d1) {
    int d2 = x->rch->lch->d;
    if (m < d1 + d2) {
      x->rch->lch = splay(x->rch->lch, m - d1);
      x->rch = rotr(x->rch);
      return rotl(x);
    } else if (m > d1 + d2) {
      x->rch->rch = splay(x->rch->rch, m - d1 - d2);
      return rotl(rotl(x));
    } else {
      x->rch = splay(x->rch, m - d1);
      return rotl(x);
    }
  } else
    return x;
}

Splay *merge(Splay *x, Splay *y) {
  if (!x)
    return y;
  if (!y)
    return x;
  Splay *z = new Splay();
  z->lch = x;
  z->rch = y;
  return update(z);
}

Splay *merges(vector<Splay *> xs) {
  Splay *x = NULL;
  for (Splay *y : xs)
    x = merge(x, y);
  return x;
}

P split(Splay *x, int m) {
  if (m == 0)
    return P(NULL, x);
  if (m == x->d)
    return P(x, NULL);
  x = splay(x, m);
  P p(x->lch, x->rch);
  delete x;
  return p;
}

vector<Splay *> splits(Splay *x, vector<int> is) {
  reverse(is.begin(), is.end());
  vector<Splay *> xs;
  for (int i : is) {
    P p = split(x, i);
    x = p.first;
    xs.pb(p.second);
  }
  xs.pb(x);
  reverse(xs.begin(), xs.end());
  return xs;
}

int get_val(Splay *&x, int i) {
  P p = split(x, i);
  cout << p.second->d << endl;
  P q = split(p.second, 1);
  cout << q.first << endl;
  int val = q.first->val;
  x = merge(p.first, merge(q.first, q.second));
  return val;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  rep(i, N) scanf("%d", &a[i]);
  Splay *x = NULL;
  rep(i, N) x = merge(x, new Splay(a[i]));
  while (Q--) {
    int q;
    scanf("%d", &q);
    if (q == 0) {
      int l, r;
      scanf("%d%d", &l, &r);
      r++;
      vector<Splay *> xs = splits(x, {l, r - 1, r});
      swap(xs[1], xs[2]);
      x = merges(xs);
    }
    if (q == 1) {
      int l, r;
      scanf("%d%d", &l, &r);
      r++;
      vector<Splay *> xs = splits(x, {l, r});
      int val = xs[1]->val;
      x = merges(xs);
      printf("%d\n", val);
    }
    if (q == 2) {
      int i, val;
      scanf("%d%d", &i, &val);
      vector<Splay *> xs = splits(x, {i, i + 1});
      xs[1]->val = val;
      x = merges(xs);
    }
  }
}