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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

pii place(vector<vi> &v, int p) {
  int cnt = 0;
  REP(i, v.size()) {
    if (cnt <= p && p < cnt + v[i].size()) {
      return pii(i, p - cnt);
    } else
      cnt += v[i].size();
  }
}

vector<vi> copy(vi &from, int s) {
  vector<vi> to(s);
  REP(i, from.size()) { to[i / s].push_back(from[i]); }
  return to;
}

int main() {
  int n, q;
  cin >> n >> q;
  int s = 1500;
  vi a(n), mn(s);
  REP(i, n) cin >> a[i];
  vector<vi> v = copy(a, s);
  REP(i, v.size()) mn[i] = (v[i].size() > 0 ? *min_element(ALL(v[i])) : INF);
  int cntc = 0;
  REP(op, q) {
    if (cntc > s) {
      vi tmp;
      REP(i, v.size()) REP(j, v[i].size()) tmp.push_back(v[i][j]);
      v = copy(tmp, s);
      REP(i, v.size())
      mn[i] = (v[i].size() > 0 ? *min_element(ALL(v[i])) : INF);
      cntc = 0;
    }
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) {
      int rx = place(v, c).first, ry = place(v, c).second;
      int t = v[rx][ry];
      v[rx].erase(v[rx].begin() + ry);
      int lx = place(v, b).first, ly = place(v, b).second;
      v[lx].insert(v[lx].begin() + ly, t);
      mn[lx] = *min_element(ALL(v[lx]));
      mn[rx] = (v[rx].size() > 0 ? *min_element(ALL(v[rx])) : INF);
      cntc++;
    } else if (a == 1) {
      int lx = place(v, b).first, ly = place(v, b).second;
      int rx = place(v, c).first, ry = place(v, c).second;
      int ans = INF;
      if (lx == rx)
        FOR(i, ly, ry + 1) chmin(ans, v[lx][i]);
      else {
        FOR(i, ly, v[lx].size()) chmin(ans, v[lx][i]);
        FOR(i, lx + 1, rx) chmin(ans, mn[i]);
        REP(i, ry + 1) chmin(ans, v[rx][i]);
      }
      cout << ans << endl;
    } else {
      int x = place(v, b).first, y = place(v, b).second;
      v[x][y] = c;
      mn[x] = *min_element(ALL(v[x]));
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

pii place(vector<vi> &v, int p) {
  int cnt = 0;
  REP(i, v.size()) {
    if (cnt <= p && p < cnt + v[i].size()) {
      return pii(i, p - cnt);
    } else
      cnt += v[i].size();
  }
}

void copy(vector<vi> &to, vi &from, int s) {
  REP(i, from.size()) { to[i / s].push_back(from[i]); }
}

int main() {
  int n, q;
  cin >> n >> q;
  int s = ceil(sqrt(n));
  vector<vi> v(s);
  vi a(n), mn(s);
  REP(i, n) cin >> a[i];
  copy(v, a, s);
  REP(i, v.size()) mn[i] = (v[i].size() > 0 ? *min_element(ALL(v[i])) : INF);
  REP(op, q) {
    /*
    if (op % (s - 1) == 0)
    {
            vector<vi> nv(s);
            vi tmp;
            REP(i, v.size())REP(j, v[i].size()) tmp.push_back(v[i][j]);
            copy(nv, tmp, s);
            v = nv;
    }
    */
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) {
      int rx = place(v, c).first, ry = place(v, c).second;
      int t = v[rx][ry];
      v[rx].erase(v[rx].begin() + ry);
      int lx = place(v, b).first, ly = place(v, b).second;
      v[lx].insert(v[lx].begin() + ly, t);
      mn[lx] = *min_element(ALL(v[lx]));
      mn[rx] = (v[rx].size() > 0 ? *min_element(ALL(v[rx])) : INF);
    } else if (a == 1) {
      int lx = place(v, b).first, ly = place(v, b).second;
      int rx = place(v, c).first, ry = place(v, c).second;
      int ans = INF;
      if (lx == rx)
        FOR(i, ly, ry + 1) chmin(ans, v[lx][i]);
      else {
        FOR(i, ly, v[lx].size()) chmin(ans, v[lx][i]);
        FOR(i, lx + 1, rx) chmin(ans, mn[i]);
        REP(i, ry + 1) chmin(ans, v[rx][i]);
      }
      cout << ans << endl;
    } else {
      int x = place(v, b).first, y = place(v, b).second;
      v[x][y] = c;
      mn[x] = *min_element(ALL(v[x]));
    }
  }
  return 0;
}