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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int N;
deque<P> card;

int main() {
  while (scanf("%d", &N) && N) {
    card.clear();
    card.push_back(P(1, N));
    int m, p, q, r;
    scanf("%d%d%d%d", &m, &p, &q, &r);
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      deque<P> A, B, C;
      int pos = 0;
      while (pos <= x) {
        if (pos + (card[0].second - card[0].first + 1) > x) {
          A.push_back(P(card[0].first, card[0].first + x - pos - 1));
          card[0].first += x - pos;
          pos = x + 1;
          break;
        }
        pos += card[0].second - card[0].first + 1;
        A.push_back(card[0]);
        card.pop_front();
      }
      while (pos <= y) {
        if (pos + (card[0].second - card[0].first + 1) > y) {
          B.push_back(P(card[0].first, card[0].first + y - pos));
          card[0].first += y - pos + 1;
          pos = y + 1;
          break;
        }
        pos += card[0].second - card[0].first + 1;
        B.push_back(card[0]);
        card.pop_front();
      }
      card.insert(card.end(), B.begin(), B.end());
      card.insert(card.end(), A.begin(), A.end());
    }

    //    for(int i=0;i<card.size();i++) printf("%d %d\\n", card[i].first,
    //    card[i].second);
    int pos = 1;
    int res = 0;
    for (int i = 0; i < card.size(); i++) {
      if (p <= pos && pos <= q) {
        //	printf("%d 1\\n", i);
        if (pos + card[i].second - card[i].first + 1 < q) {
          if (card[i].first <= r && card[i].second <= r)
            res += card[i].second - card[i].first + 1;
          else if (card[i].first <= r)
            res += r - card[i].first + 1;
        } else if (pos + card[i].second - card[i].first + 1 >= q) {
          int vl = card[i].first, vr = card[i].first + q - pos;
          //	  printf("%d %d %d\\n",pos, vl ,vr);
          if (vl <= r && vr <= r)
            res += vr - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        }
        //	printf("%d\\n", res);
      } else if (pos + card[i].second - card[i].first + 1 > p && pos <= q) {
        //	printf("%d 2\\n", i);
        int vl = card[i].first + p - pos;
        //	printf("vl %d\\n", vl);
        if (pos + card[i].second - card[i].first + 1 < q) {
          if (vl <= r && card[i].second <= r)
            res += card[i].second - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        } else if (pos + card[i].second - card[i].first + 1 >= q) {
          int vr = vl + q - pos;
          if (vl <= r && vr <= r)
            res += vr - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        }
        //	printf("%d\\n", res);
      }
      pos += card[i].second - card[i].first + 1;
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int N;
vector<P> card;

int main() {
  while (scanf("%d", &N) && N) {
    card.clear();
    card.push_back(P(1, N));
    int m, p, q, r;
    scanf("%d%d%d%d", &m, &p, &q, &r);
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      vector<P> A, B, C;
      int pos = 0;
      while (pos <= x) {
        if (pos + (card[0].second - card[0].first + 1) > x) {
          A.push_back(P(card[0].first, card[0].first + x - pos - 1));
          card[0].first += x - pos;
          pos = x + 1;
          break;
        }
        pos += card[0].second - card[0].first + 1;
        A.push_back(card[0]);
        card.erase(card.begin());
      }
      while (pos <= y) {
        if (pos + (card[0].second - card[0].first + 1) > y) {
          B.push_back(P(card[0].first, card[0].first + y - pos));
          card[0].first += y - pos + 1;
          pos = y + 1;
          break;
        }
        pos += card[0].second - card[0].first + 1;
        B.push_back(card[0]);
        card.erase(card.begin());
      }
      card.insert(card.end(), B.begin(), B.end());
      card.insert(card.end(), A.begin(), A.end());
    }

    //    for(int i=0;i<card.size();i++) printf("%d %d\\n", card[i].first,
    //    card[i].second);
    int pos = 1;
    int res = 0;
    for (int i = 0; i < card.size(); i++) {
      if (p <= pos && pos <= q) {
        //	printf("%d 1\\n", i);
        if (pos + card[i].second - card[i].first + 1 < q) {
          if (card[i].first <= r && card[i].second <= r)
            res += card[i].second - card[i].first + 1;
          else if (card[i].first <= r)
            res += r - card[i].first + 1;
        } else if (pos + card[i].second - card[i].first + 1 >= q) {
          int vl = card[i].first, vr = card[i].first + q - pos;
          //	  printf("%d %d %d\\n",pos, vl ,vr);
          if (vl <= r && vr <= r)
            res += vr - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        }
        //	printf("%d\\n", res);
      } else if (pos + card[i].second - card[i].first + 1 > p && pos <= q) {
        //	printf("%d 2\\n", i);
        int vl = card[i].first + p - pos;
        //	printf("vl %d\\n", vl);
        if (pos + card[i].second - card[i].first + 1 < q) {
          if (vl <= r && card[i].second <= r)
            res += card[i].second - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        } else if (pos + card[i].second - card[i].first + 1 >= q) {
          int vr = vl + q - pos;
          if (vl <= r && vr <= r)
            res += vr - vl + 1;
          else if (vl <= r)
            res += r - vl + 1;
        }
        //	printf("%d\\n", res);
      }
      pos += card[i].second - card[i].first + 1;
    }
    printf("%d\\n", res);
  }
  return 0;
}