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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int x[5001][3];
int z[25000][2];

queue<int> Q1;
queue<int> R1;
queue<int> S1;
queue<int> Q2;
queue<int> R2;
queue<int> S2;

int main() {
  int n, m, a, b, c, k, p, sum, sum2, sum3, t;
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cin >> m >> a >> b >> c;
    z[0][0] = 1;
    z[0][1] = n;

    k = 1;
    for (int i = 1; i <= m; i++) {
      cin >> x[i][1] >> x[i][2];
      sum2 = 1;
      sum3 = 1;
      for (int j = 0; j < k; j++) {
        sum2 += z[j][1] - z[j][0] + 1;
        sum2 -= 1;
        if (sum2 <= x[i][1]) {
          Q1.push(z[j][0]);
          Q2.push(z[j][1]);
        } else if (sum3 > x[i][2]) {
          S1.push(z[j][0]);
          S2.push(z[j][1]);
        } else if (sum2 <= x[i][2] && sum3 > x[i][1]) {
          R1.push(z[j][0]);
          R2.push(z[j][1]);
        } else if (sum2 <= x[i][2] && sum3 <= x[i][1]) {
          Q1.push(z[j][0]);
          Q2.push(z[j][0] + x[i][1] - sum3);
          R1.push(z[j][0] + x[i][1] - sum3 + 1);
          R2.push(z[j][1]);
        } else if (sum2 > x[i][2] && sum3 > x[i][1]) {
          R1.push(z[j][0]);
          R2.push(z[j][0] + x[i][2] - sum3);
          S1.push(z[j][0] + x[i][2] - sum3 + 1);
          S2.push(z[j][1]);
        } else {
          Q1.push(z[j][0]);
          Q2.push(z[j][0] + x[i][1] - sum3);
          R1.push(z[j][0] + x[i][1] - sum3 + 1);
          R2.push(z[j][0] + x[i][2] - sum3);
          S1.push(z[j][0] + x[i][2] - sum3 + 1);
          S2.push(z[j][1]);
        }
        sum2 += 1;
        sum3 = sum2;
      }
      k = 0;
      k += Q1.size();
      k += R1.size();
      k += S1.size();
      p = 0;

      while (!S1.empty()) {
        z[p][0] = S1.front();
        z[p][1] = S2.front();
        S1.pop();
        S2.pop();
        p++;
      }
      while (!R1.empty()) {
        z[p][0] = R1.front();
        z[p][1] = R2.front();
        R1.pop();
        R2.pop();
        p++;
      }
      while (!Q1.empty()) {
        z[p][0] = Q1.front();
        z[p][1] = Q2.front();
        Q1.pop();
        Q2.pop();
        p++;
      }
    }
    sum = 0;
    sum2 = 1;
    sum3 = 1;
    for (int i = 0; i < k; i++) {
      sum2 += z[i][1] - z[i][0] + 1;
      if (sum2 - 1 >= a && sum3 <= b) {
        if (z[i][1] <= c) {
          sum += min(b, sum2 - 1) - max(a, sum3) + 1;
        } else if (z[i][0] <= c) {
          t = min(b, sum2 - 1);
          t = min(t, sum3 + c - z[i][0]);
          if (t - max(a, sum3) >= 0) {
            sum += t - max(a, sum3) + 1;
          }
        }
      }
      sum3 = sum2;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int x[5001][3];
int z[25000][2];

queue<int> Q1;
queue<int> R1;
queue<int> S1;
queue<int> Q2;
queue<int> R2;
queue<int> S2;

int main() {
  int n, m, a, b, c, k, p, sum, sum2, sum3, t;
  while (true) {
    cin >> n >> m;
    cin >> a >> b >> c;
    z[0][0] = 1;
    z[0][1] = n;

    k = 1;
    for (int i = 1; i <= m; i++) {
      cin >> x[i][1] >> x[i][2];

      for (int j = 0; j < k; j++) {
        if (z[j][1] <= x[i][1]) {
          Q1.push(z[j][0]);
          Q2.push(z[j][1]);
        } else if (z[j][0] > x[i][2]) {
          S1.push(z[j][0]);
          S2.push(z[j][1]);
        } else if (z[j][1] <= x[i][2] && z[j][0] > x[i][1]) {
          R1.push(z[j][0]);
          R2.push(z[j][1]);
        } else if (z[j][1] <= x[i][2] && z[j][0] <= x[i][1]) {
          Q1.push(z[j][0]);
          Q2.push(x[i][1]);
          R1.push(x[i][1] + 1);
          R2.push(z[j][1]);
        } else if (z[j][1] > x[i][2] && z[j][0] > x[i][1]) {
          R1.push(z[j][0]);
          R2.push(x[i][2]);
          S1.push(x[i][2] + 1);
          S2.push(z[j][1]);
        } else {
          Q1.push(z[j][0]);
          Q2.push(x[i][1]);
          R1.push(x[i][1] + 1);
          R2.push(x[i][2]);
          S1.push(x[i][2] + 1);
          S2.push(z[j][1]);
        }
      }
      k = 0;
      k += Q1.size();
      k += R1.size();
      k += S1.size();
      p = 0;

      while (!S1.empty()) {
        z[p][0] = S1.front();
        z[p][1] = S2.front();
        S1.pop();
        S2.pop();
        p++;
      }
      while (!R1.empty()) {
        z[p][0] = R1.front();
        z[p][1] = R2.front();
        R1.pop();
        R2.pop();
        p++;
      }
      while (!Q1.empty()) {
        z[p][0] = Q1.front();
        z[p][1] = Q2.front();
        Q1.pop();
        Q2.pop();
        p++;
      }
    }
    sum = 0;
    sum2 = 1;
    sum3 = 1;
    for (int i = 0; i < k; i++) {
      sum2 += z[i][1] - z[i][0] + 1;
      if (sum2 - 1 >= a && sum3 <= b) {
        if (z[i][1] <= c) {
          sum += min(b, sum2 - 1) - max(a, sum3) + 1;
        } else if (z[i][0] <= c) {
          t = min(b, sum2 - 1);
          t = min(t, sum3 + c - z[i][0]);
          if (t - max(a, sum3) >= 0) {
            sum += t - max(a, sum3) + 1;
          }
        }
      }
      sum3 = sum2;
    }
    cout << sum << endl;
  }
}