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

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define P pair<int, int>
using namespace std;

queue<P> operator+(queue<P> a, queue<P> b) {
  while (b.size()) {
    a.push(b.front());
    b.pop();
  }
  return a;
}
queue<P> L;
int main() {
  int a;
  while (cin >> a, a) {
    int b, c, d, e;
    scanf("%d%d%d%d", &b, &c, &d, &e);
    queue<P> tmp;
    swap(L, tmp);
    L.push(P(1, a));
    for (int f = 0; f < b; f++) {
      int g, h;
      scanf("%d%d", &g, &h);
      queue<P> A, B, C;
      int sum = 0, j = 0;
      while (L.size()) {
        P i = L.front();
        L.pop();
        sum += i.second - i.first + 1;
        if (j < g && sum > g) {
          if (j < h && sum > h) {
            A.push(P(i.first, i.first + (g - j) - 1));
            B.push(P(i.first + (g - j), i.first + (h - j) - 1));
            C.push(P(i.first + (h - j), i.second));
          } else {
            A.push(P(i.first, i.first + (g - j) - 1));
            B.push(P(i.first + (g - j), i.second));
          }
        } else {
          if (j < h && sum > h) {
            B.push(P(i.first, i.first + (h - j) - 1));
            C.push(P(i.first + (h - j), i.second));
          } else {
            if (sum > h) {
              C.push(i);
            } else if (sum > g) {
              B.push(i);
            } else {
              A.push(i);
            }
          }
        }
        j += i.second - i.first + 1;
      }
      L = C + B + A;
    }
    int x = 0, y = 0;
    vector<P> W;
    while (L.size()) {
      P t = L.front();
      L.pop();
      x += t.second - t.first + 1;
      if (x <= d && x >= c) {
        if (y <= d && y >= c) {
          W.push_back(P(t.first, t.second));
        } else {
          W.push_back(P(t.second - (x - c), t.second));
        }
      } else {
        if (y <= d && y >= c) {
          W.push_back(P(t.first, t.first + d - y - 1));
        }
      }
      y += t.second - t.first + 1;
    }
    int ans = 0;
    for (P i : W) {
      ans += max(0, min(e, i.second) - i.first + 1);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

list<P> operator+(list<P> a, list<P> b) {
  while (b.size()) {
    a.push_back(b.front());
    b.pop_front();
  }
  return a;
}
list<P> L;
int main() {
  int a;
  while (cin >> a, a) {
    int b, c, d, e;
    scanf("%d%d%d%d", &b, &c, &d, &e);
    L.clear();
    L.push_back(P(1, a));
    for (int f = 0; f < b; f++) {
      int g, h;
      scanf("%d%d", &g, &h);
      list<P> A, B, C;
      int sum = 0, j = 0;
      while (L.size()) {
        P i = L.front();
        L.pop_front();
        sum += i.second - i.first + 1;
        if (j < g && sum > g) {
          if (j < h && sum > h) {
            A.push_back(P(i.first, i.first + (g - j) - 1));
            B.push_back(P(i.first + (g - j), i.first + (h - j) - 1));
            C.push_back(P(i.first + (h - j), i.second));
          } else {
            A.push_back(P(i.first, i.first + (g - j) - 1));
            B.push_back(P(i.first + (g - j), i.second));
          }
        } else {
          if (j < h && sum > h) {
            B.push_back(P(i.first, i.first + (h - j) - 1));
            C.push_back(P(i.first + (h - j), i.second));
          } else {
            if (sum > h) {
              C.push_back(i);
            } else if (sum > g) {
              B.push_back(i);
            } else {
              A.push_back(i);
            }
          }
        }
        j += i.second - i.first + 1;
      }
      L = C + B + A;
    }
    int x = 0, y = 0;
    vector<P> W;
    while (L.size()) {
      P t = L.front();
      L.pop_front();
      x += t.second - t.first + 1;
      if (x <= d && x >= c) {
        if (y <= d && y >= c) {
          W.push_back(P(t.first, t.second));
        } else {
          W.push_back(P(t.second - (x - c), t.second));
        }
      } else {
        if (y <= d && y >= c) {
          W.push_back(P(t.first, t.first + d - y - 1));
        }
      }
      y += t.second - t.first + 1;
    }
    int ans = 0;
    for (P i : W) {
      ans += max(0, min(e, i.second) - i.first + 1);
    }
    cout << ans << endl;
  }
}