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

#include <iostream>
#include <list>

#define T_CT 1
#define T_NC 2

using namespace std;

typedef struct {
  int count;
  int type;
} bulk;

int N, M, P, Q, R;
list<bulk> S;
bulk tb;

bulk *makebulk(int count, int type) {
  tb.count = count;
  tb.type = type;
  return &tb;
}

void shuffle(int x, int y) {
  int sum = 0;
  int countv = 0;
  auto endi = S.end();
  auto sfront = S.begin();
  while (true) {
    sfront = S.begin();
    if (sum + sfront->count <= x) {
      S.push_back(*sfront);
      sum += sfront->count;
      S.pop_front();
      countv++;
    } else {
      if (sum != x) {
        sfront->count -= x - sum;
        S.push_back(*makebulk(x - sum, sfront->type));
        countv++;
      }
      break;
    }
  }
  sum = 0;
  for (int i = 0; i < countv; i++) {
    endi--;
  }
  while (true) {
    sfront = S.begin();
    if (sum + sfront->count <= y - x) {
      S.insert(endi, *sfront);
      sum += sfront->count;
      S.pop_front();
    } else {
      if (sum != y - x) {
        sfront->count -= (y - x) - sum;
        S.insert(endi, *makebulk((y - x) - sum, sfront->type));
        endi++;
      }
      break;
    }
  }
  /*
  for(auto it = S.begin();it < S.end()-1;it++)
  {
          if(it->type == (it+1)->type)
          {
                  it->count += (it+1)->count;
                  S.erase(it+1);
                  it--;
          }
  }
  */
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      exit(0);
    cin >> M >> P >> Q >> R;
    S.clear();
    S.push_back(*makebulk(R, T_CT));
    S.push_back(*makebulk(N - R, T_NC));
    int a, b;
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      shuffle(a, b);
    }
    int sum = 0;
    auto sfront = S.begin();
    while (true) {
      sfront = S.begin();
      if (sum + sfront->count <= P - 1) {
        sum += sfront->count;
        S.pop_front();
      } else {
        if (sum != P - 1) {
          sfront->count -= (P - 1) - sum;
        }
        break;
      }
    }
    sum = 0;
    int count = (Q - P) + 1;
    int result = 0;
    while (true) {
      sfront = S.begin();
      if (sum + sfront->count <= count) {
        if (sfront->type == T_CT) {
          result += sfront->count;
        }
        sum += sfront->count;
        S.pop_front();
      } else {
        if (sum != count) {
          if (sfront->type == T_CT) {
            result += count - sum;
          }
        }
        break;
      }
    }
    cout << result << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define bgi (S.begin())
#define T_CT 1
#define T_NC 2

using namespace std;

typedef struct {
  int count;
  int type;
} bulk;

int N, M, P, Q, R;
vector<bulk> S;
bulk tb;

bulk *makebulk(int count, int type) {
  tb.count = count;
  tb.type = type;
  return &tb;
}

void shuffle(int x, int y) {
  int sum = 0;
  int countv = 0;
  while (true) {
    if (sum + bgi->count <= x) {
      S.push_back(S[0]);
      sum += S[0].count;
      S.erase(bgi);
      countv++;
    } else {
      if (sum != x) {
        S[0].count -= x - sum;
        S.push_back(*makebulk(x - sum, S[0].type));
        countv++;
      }
      break;
    }
  }
  auto endi = S.end() - countv;
  sum = 0;
  while (true) {
    endi = S.end() - countv;
    if (sum + bgi->count <= y - x) {
      S.insert(endi, S[0]);
      sum += S[0].count;
      S.erase(bgi);
    } else {
      if (sum != y - x) {
        S[0].count -= (y - x) - sum;
        S.insert(endi, *makebulk((y - x) - sum, S[0].type));
        endi++;
      }
      break;
    }
  }
  for (auto it = S.begin(); it < S.end() - 1; it++) {
    if (it->type == (it + 1)->type) {
      it->count += (it + 1)->count;
      S.erase(it + 1);
      it--;
    }
  }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      exit(0);
    cin >> M >> P >> Q >> R;
    S.clear();
    S.push_back(*makebulk(R, T_CT));
    S.push_back(*makebulk(N - R, T_NC));
    int a, b;
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      shuffle(a, b);
    }
    int sum = 0;
    while (true) {
      if (sum + S[0].count <= P - 1) {
        sum += S[0].count;
        S.erase(bgi);
      } else {
        if (sum != P - 1) {
          S[0].count -= (P - 1) - sum;
        }
        break;
      }
    }
    sum = 0;
    int count = (Q - P) + 1;
    int result = 0;
    while (true) {
      if (sum + S[0].count <= count) {
        if (S[0].type == T_CT) {
          result += S[0].count;
        }
        sum += S[0].count;
        S.erase(bgi);
      } else {
        if (sum != count) {
          if (S[0].type == T_CT) {
            result += count - sum;
          }
        }
        break;
      }
    }
    cout << result << endl;
  }
}