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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

struct Range {
  Range() {}
  Range(int top, int n) : top(top), n(n) {}
  pair<Range, Range> split(int num) {
    return make_pair(Range(top, num), Range(top + num, n - num));
  }
  int top, n;
};
queue<Range> pile;
Range a[500000], b[500000], c[500000];
int size;
int next(int &sum, int until_sum, Range *a) {
  int j = 0;
  for (; sum < until_sum;) {
    if (sum + pile.front().n <= until_sum) {
      // a.push_back(pile[i]);
      a[j++] = pile.front();
      sum += pile.front().n;
      pile.pop();
    } else {
      pair<Range, Range> t = pile.front().split(until_sum - sum);
      // a.push_back(t.first);
      a[j++] = t.first;
      sum = until_sum;

      pile.front() = t.second;
      break;
    }
  }
  return j;
}
int main() {
  int n, m, p, q, r;
  while (scanf("%d", &n), n) {
    scanf("%d%d%d%d", &m, &p, &q, &r);

    // pile.push_back(Range(1, n));
    pile = queue<Range>();
    pile.push(Range(1, n));
    while (m--) {
      int x, y;
      scanf("%d%d", &x, &y);

      int sum = 0;
      // next(pile, p, sum, x, a);
      // next(pile, p, sum, y, b);
      // for ( ; p < pile.size(); ++p)
      //	c.push_back(pile[p]);
      int a_size = next(sum, x, a);
      int b_size = next(sum, y, b);

      // pile.insert(pile.end(), all(b));
      // pile.insert(pile.end(), all(a));
      rep(i, b_size) pile.push(b[i]);
      rep(i, a_size) pile.push(a[i]);
    }

    int i = 0, sum = 0;
    // next(pile, i, sum, p - 1, a);
    // next(pile, i, sum, q, b);
    int a_size = next(sum, p - 1, a);
    int b_size = next(sum, q, b);

    int res = 0;
    rep(i, b_size) res += min(b[i].n, max(0, r - (b[i].top - 1)));

    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

struct Range {
  Range() {}
  Range(int top, int n) : top(top), n(n) {}
  pair<Range, Range> split(int num) {
    return make_pair(Range(top, num), Range(top + num, n - num));
  }
  int top, n;
};
Range pile[500000], a[500000], b[500000], c[500000];
int size;
int next(int &i, int &sum, int until_sum, Range *a) {
  int j = 0;
  for (i = 0; sum < until_sum; ++i) {
    if (sum + pile[i].n <= until_sum) {
      // a.push_back(pile[i]);
      a[j++] = pile[i];
      sum += pile[i].n;
    } else {
      pair<Range, Range> t = pile[i].split(until_sum - sum);
      // a.push_back(t.first);
      a[j++] = t.first;
      sum = until_sum;

      pile[i] = t.second;
      break;
    }
  }
  return j;
}
int main() {
  int n, m, p, q, r;
  while (scanf("%d", &n), n) {
    scanf("%d%d%d%d", &m, &p, &q, &r);

    // pile.push_back(Range(1, n));
    pile[0].top = 1, pile[0].n = n;
    size = 1;
    while (m--) {
      int x, y;
      scanf("%d%d", &x, &y);

      int p = 0, sum = 0;
      // next(pile, p, sum, x, a);
      // next(pile, p, sum, y, b);
      // for ( ; p < pile.size(); ++p)
      //	c.push_back(pile[p]);
      int a_size = next(p, sum, x, a);
      int b_size = next(p, sum, y, b);
      int c_size;
      for (c_size = 0; p < size; ++p, ++c_size)
        c[c_size] = pile[p];

      // pile.insert(pile.end(), all(b));
      // pile.insert(pile.end(), all(a));
      size = 0;
      rep(i, c_size) pile[size++] = c[i];
      rep(i, b_size) pile[size++] = b[i];
      rep(i, a_size) pile[size++] = a[i];
    }

    int i = 0, sum = 0;
    // next(pile, i, sum, p - 1, a);
    // next(pile, i, sum, q, b);
    int a_size = next(p, sum, p - 1, a);
    int b_size = next(p, sum, q, b);

    int res = 0;
    rep(i, b_size) res += min(b[i].n, max(0, r - (b[i].top - 1)));

    printf("%d\\n", res);
  }
}