Problem p00459 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

struct tb {
  int f, t;
  tb(int f, int t) : f(f), t(t) {}
};

int n, m, pr, q, r;
int chx[5001][3];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    queue<tb> c;
    queue<tb> d[4];
    c.push(tb(1, n));
    scanf("%d", &m);
    scanf("%d%d%d", &pr, &q, &r);
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &chx[i][1], &chx[i][2]);
    }
    for (int i = 0; i < m; i++) {
      int sh = 1, size = 0;
      while (c.size()) {
        tb p = c.front();
        c.pop();
        size += p.t - p.f + 1;
        if (sh != 3) {
          if (size <= chx[i][sh]) {
            d[sh].push(tb(p.f, p.t));
            if (size == chx[i][sh])
              sh++;
          } else if (sh == 2) {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][2] - x;
            d[2].push(tb(p.f, p.f + xx - 1));
            d[3].push(tb(p.f + xx, p.t));
            sh++;

          } else if (sh == 1 && size <= chx[i][2]) {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][1] - x;
            d[1].push(tb(p.f, p.f + xx - 1));
            d[2].push(tb(p.f + xx, p.t));
            if (size == chx[i][2])
              sh++;
            sh++;
          } else {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][1] - x;
            int x2 = chx[i][2] - chx[i][1];
            d[1].push(tb(p.f, p.f + xx - 1));
            d[2].push(tb(p.f + xx, p.f + xx + x2 - 1));
            d[3].push(tb(p.f + xx + x2, p.t));
            sh = 3;
          }
        } else
          d[sh].push(tb(p.f, p.t));
      }
      for (int j = 3; j >= 1; j--) {
        while (d[j].size()) {
          tb p = d[j].front();
          d[j].pop();
          int pf = p.f, pt = p.t;
          c.push(tb(pf, pt));
        }
      }
    }
    int size = 1;
    int ans = 0;
    while (c.size()) {
      tb p = c.front();
      c.pop();
      while (p.f != p.t + 1) {
        if (size >= pr && size <= q && p.f <= r)
          ans++;
        p.f++;
        size++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

struct tb {
  int f, t;
  tb(int f, int t) : f(f), t(t) {}
};

int n, m, pr, q, r;
int chx[5001][3];

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    queue<tb> c;
    queue<tb> d[4];
    c.push(tb(1, n));
    scanf("%d", &m);
    scanf("%d%d%d", &pr, &q, &r);
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &chx[i][1], &chx[i][2]);
    }
    for (int i = 0; i < m; i++) {
      int sh = 1, size = 0;
      while (c.size()) {
        tb p = c.front();
        c.pop();
        size += p.t - p.f + 1;
        if (sh != 3) {
          if (size <= chx[i][sh]) {
            d[sh].push(tb(p.f, p.t));
            if (size == chx[i][sh])
              sh++;
          } else if (sh == 2) {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][2] - x;
            d[2].push(tb(p.f, p.f + xx - 1));
            d[3].push(tb(p.f + xx, p.t));
            sh++;

          } else if (sh == 1 && size <= chx[i][2]) {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][1] - x;
            d[1].push(tb(p.f, p.f + xx - 1));
            d[2].push(tb(p.f + xx, p.t));
            if (size == chx[i][2])
              sh++;
            sh++;
          } else {
            int x = size - p.t + p.f - 1;
            int xx = chx[i][1] - x;
            int x2 = chx[i][2] - chx[i][1];
            d[1].push(tb(p.f, p.f + xx - 1));
            d[2].push(tb(p.f + xx, p.f + xx + x2 - 1));
            d[3].push(tb(p.f + xx + x2, p.t));
            sh = 3;
          }
        } else
          d[sh].push(tb(p.f, p.t));
      }
      for (int j = 3; j >= 1; j--) {
        while (d[j].size()) {
          tb p = d[j].front();
          d[j].pop();
          int pf = p.f, pt = p.t;
          c.push(tb(pf, pt));
        }
      }
    }
    int size = 1;
    int ans = 0;
    while (c.size()) {
      tb p = c.front();
      c.pop();
      int x = p.f, y = p.t;
      int s = y - x + 1;
      if (size < pr)
        x += pr - size;
      if (size + s > q)
        y -= size + s - q - 1;
      if (y > r)
        y = r;
      if (y >= x)
        ans += y - x + 1;
      // printf("%d %d %d %d\n",p.f,p.t,x,y);
      size += s;
    }
    printf("%d\n", ans);
  }
  return 0;
}