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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<int, int> P;

void queue_to_vector(queue<P> &stk, vector<P> &v) {
  while (!stk.empty()) {
    v.push_back(stk.front());
    stk.pop();
  }
}

int main() {
  int N, M, p, q, r;
  while (cin >> N >> M >> p >> q >> r) {
    vector<P> card;
    card.push_back(P(1, N));
    while (M-- > 0) {
      int x, y;
      cin >> x >> y;
      int C = 0;
      queue<P> bottom, mid, top;
      for (int i = 0; i < card.size(); i++) {
        int num = card[i].second - card[i].first + 1;
        if (C < x) {
          if (C + num <= x) {
            bottom.push(card[i]);
            C += num;
          } else {
            bottom.push(P(card[i].first, card[i].second - (C + num - x)));
            card.insert(card.begin() + i + 1,
                        P(card[i].second - (C + num - x) + 1, card[i].second));
            C = x;
          }
        } else if (C < y) {
          if (C + num <= y) {
            mid.push(card[i]);
            C += num;
          } else {
            mid.push(P(card[i].first, card[i].second - (C + num - y)));
            card.insert(card.begin() + i + 1,
                        P(card[i].second - (C + num - y) + 1, card[i].second));
            C = y;
          }
        } else {
          top.push(card[i]);
        }
      }
      card.clear();
      queue_to_vector(top, card);
      queue_to_vector(mid, card);
      queue_to_vector(bottom, card);
    }
    int C = 0;
    int ans = 0;
    p--;
    for (int i = 0; i < card.size(); i++) {
      int num = card[i].second - card[i].first + 1;
      if (C < p) {
        if (C + num <= p) {
          C += num;
        } else {
          card.insert(card.begin() + i + 1,
                      P(card[i].second - (C + num - p) + 1, card[i].second));
          C = p;
        }
      } else if (C < q) {
        if (C + num <= q) {
          ans += max(0, min(card[i].second, r) - card[i].first + 1);
          C += num;
        } else {
          int E = card[i].second - (C + num - q);
          ans += max(0, min(E, r) - card[i].first + 1);
          break;
        }
      } else {
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
typedef pair<int, int> P;
list<P> card;
int cnum(P p) { return p.second - p.first + 1; }
void printcard(list<P> card) {
  for (list<P>::iterator it = card.begin(); it != card.end(); it++) {
    printf("%d-%d ", it->first, it->second);
  }
  putchar('\\n');
}

void shuffle(int x, int y) { //[y,n) [x,y) [0,x)
  list<P> C;
  list<P>::iterator ins = C.begin();
  int count = 0;
  for (list<P>::iterator it = card.begin(); it != card.end(); it++) {
    P ncd = *it;
    while (true) {
      if (count < x) {
        if (count + cnum(ncd) > x) {
          C.push_back(P(ncd.first, ncd.first + (x - count - 1)));
          ncd = P(ncd.first + (x - count), ncd.second);
          count += cnum(C.back());
        } else {
          C.push_back(ncd);
          count += cnum(ncd);
          break;
        }
        ins = C.begin();
      } else if (count < y) {
        if (count + cnum(ncd) > y) {
          ins = C.insert(ins, P(ncd.first, ncd.first + (y - count - 1)));
          ncd = P(ncd.first + (y - count), ncd.second);
          count += y - count;
        } else {
          ins = C.insert(ins, ncd);
          count += cnum(ncd);
          break;
        }
        if (count >= y)
          ins = C.begin();
      } else {
        ins = C.insert(ins, ncd);
        break;
      }
    }
  }
  /*
  printcard(A);
  printcard(B);
  printcard(C);
  */
  /*
  C.insert(C.end(),B.begin(),B.end());
  C.insert(C.end(),A.begin(),A.end());
  */
  card = C;
}
int calc(int p, int q, int r) {
  int ret = 0;
  int count = 0;
  for (list<P>::iterator it = card.begin(); it != card.end(); it++) {
    int lw = count + 1;
    int up = count + cnum(*it);
    if (lw >= p && lw < q) {
      if (up >= p && up < q) {
        ret += max(0, it->second - max(r + 1, it->first) + 1);
      } else {
        ret += max(0, (it->first + (q - lw)) - max(r + 1, it->first) + 1);
        break;
      }
    } else {
      if (up >= p && up < q) {
        ret += max(0, it->second - max(r + 1, it->second - (up - p)) + 1);
      } else {
        // nothing
      }
    }
    // printf("%d->%d:%d ",lw,up, ret);
    count = up;
  }
  return (q - p + 1) - ret;
}

int main() {
  int n, m, p, q, r;
  while (cin >> n >> m >> p >> q >> r) {
    card.clear();
    card.push_back(P(1, n));
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      shuffle(x, y);
    }
    int ans = calc(p, q, r);
    cout << ans << endl;
  }
  return 0;
}