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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int n, m;
vector<P> S, A, B, C;

void shuffle(int x, int y) {

  A.clear();
  B.clear();
  C.clear();

  int idx = 0;

  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;
    int f = 0;

    if (d <= x) {
      if (d == x)
        f = 1;
      A.push_back(S[i]);

      x -= d;

    } else {

      A.push_back(P(S[i].first, S[i].first + x - 1));

      S[i] = P(S[i].first + x, S[i].second);

      x = 0;
    }

    idx = i + f;

    if (!x)
      break;
  }

  for (int i = idx; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;
    int f = 0;
    if (d <= y) {
      if (d == y)
        f = 1;
      B.push_back(S[i]);

      y -= d;

    } else {

      B.push_back(P(S[i].first, S[i].first + y - 1));

      S[i] = P(S[i].first + y, S[i].second);

      y = 0;
    }

    idx = i + f;

    if (!y)
      break;
  }

  for (int i = idx; i < S.size(); i++)
    C.push_back(S[i]);

  S.clear();

  for (int i = 0; i < C.size(); i++)
    S.push_back(C[i]);

  for (int i = 0; i < B.size(); i++)
    S.push_back(B[i]);

  for (int i = 0; i < A.size(); i++)
    S.push_back(A[i]);
}

int cal(int p, int q, int r) {

  int res = 0;

  q = q - p + 1;
  p--;
  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= p) {

      p -= d;

      S.erase(S.begin());

      i--;
    } else {

      S[i] = P(S[i].first + p, S[i].second);

      p = 0;
    }

    if (!p)
      break;
  }

  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= q) {

      if (S[i].first <= r)
        res += min(r, S[i].second) - S[i].first + 1;

      q -= d;
    } else {

      S[i] = P(S[i].first, S[i].first + q - 1);

      if (S[i].first <= r)
        res += min(r, S[i].second) - S[i].first + 1;

      q = 0;
    }

    if (!q)
      break;
  }

  return res;
}

int main() {

  while (1) {

    cin >> n;
    if (!n)
      break;

    cin >> m;

    int p, q, r;

    cin >> p >> q >> r;

    S.clear();

    S.push_back(P(1, n));

    for (int i = 0; i < m; i++) {

      int x, y;
      cin >> x >> y;

      shuffle(x, y - x);
    }

    cout << cal(p, q, r) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int n, m;
vector<P> S, A, B, C;

void shuffle(int x, int y) {

  A.clear();
  B.clear();
  C.clear();

  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= x) {

      A.push_back(S[i]);

      x -= d;

      S.erase(S.begin());

      i--;
    } else {

      A.push_back(P(S[i].first, S[i].first + x - 1));

      S[i] = P(S[i].first + x, S[i].second);

      x = 0;
    }
    if (!x)
      break;
  }

  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= y) {

      B.push_back(S[i]);

      y -= d;

      S.erase(S.begin());

      i--;
    } else {

      B.push_back(P(S[i].first, S[i].first + y - 1));

      S[i] = P(S[i].first + y, S[i].second);

      y = 0;
    }

    if (!y)
      break;
  }

  for (int i = 0; i < S.size(); i++)
    C.push_back(S[i]);

  S.clear();

  for (int i = 0; i < C.size(); i++)
    S.push_back(C[i]);

  for (int i = 0; i < B.size(); i++)
    S.push_back(B[i]);

  for (int i = 0; i < A.size(); i++)
    S.push_back(A[i]);
}

int cal(int p, int q, int r) {

  int res = 0;

  q = q - p + 1;
  p--;
  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= p) {

      p -= d;

      S.erase(S.begin());

      i--;
    } else {

      S[i] = P(S[i].first + p, S[i].second);

      p = 0;
    }

    if (!p)
      break;
  }

  for (int i = 0; i < S.size(); i++) {

    int d = S[i].second - S[i].first + 1;

    if (d <= q) {

      if (S[i].first <= r)
        res += min(r, S[i].second) - S[i].first + 1;

      q -= d;
    } else {

      S[i] = P(S[i].first, S[i].first + q - 1);

      if (S[i].first <= r)
        res += min(r, S[i].second) - S[i].first + 1;

      q = 0;
    }

    if (!q)
      break;
  }

  return res;
}

int main() {

  while (1) {

    cin >> n;
    if (!n)
      break;

    cin >> m;

    int p, q, r;

    cin >> p >> q >> r;

    S.clear();

    S.push_back(P(1, n));

    for (int i = 0; i < m; i++) {

      int x, y;
      cin >> x >> y;

      shuffle(x, y - x);
    }

    cout << cal(p, q, r) << endl;
  }

  return 0;
}