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

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond)                                                          \\
  if (cond)                                                                    \\
    break;                                                                     \\
  else

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template <typename T> inline void input(vector<T> &v) {
  for (auto &x : v)
    cin >> x;
}

signed main() {
  int n, m, p, q, r;
  vector<P> pre(5000), next(5000);
  while (cin >> n && n) {
    cin >> m >> p >> q >> r;
    pre.clear();
    pre.push_back(P(1, n));
    vector<P> a(m), b(m), c(m);
    rep(i, m) {
      a.clear();
      b.clear();
      c.clear();
      next.clear();
      int x, y;
      cin >> x >> y;
      int sum = 0;
      for (P p : pre) {
        int tmp = p.second - p.first + 1;
        if (sum + tmp <= x) {
          a.push_back(p);
        } else if (y <= sum) {
          c.push_back(p);
        } else if (x <= sum && sum + tmp <= y) {
          b.push_back(p);
        } else if (sum < x && y < sum + tmp) {
          P fst = P(p.first, p.first + (x - sum) - 1);
          P snd = P(fst.second + 1, fst.second + (y - x));
          P trd = P(snd.second + 1, p.second);
          a.push_back(fst);
          b.push_back(snd);
          c.push_back(trd);
        } else if (sum < x && x < sum + tmp) {
          P fst = P(p.first, p.first + (x - sum) - 1);
          P snd = P(fst.second + 1, p.second);
          a.push_back(fst);
          b.push_back(snd);
        } else if (sum < y && y < sum + tmp) {
          P fst = P(p.first, p.first + (y - sum) - 1);
          P snd = P(fst.second + 1, p.second);
          b.push_back(fst);
          c.push_back(snd);
        }
        sum += tmp;
      }
      next.insert(next.end(), all(c));
      next.insert(next.end(), all(b));
      next.insert(next.end(), all(a));
      pre = move(next);
    }

    int sum = 0;
    int ans = 0;
    p--;
    for (P item : pre) {
      int tmp = item.second - item.first + 1;
      P ra = P(1ll << 60, 1ll << 60);
      if (sum + tmp <= p) {
        // skip
      } else if (q <= sum) {
        // skip
      } else if (p <= sum && sum + tmp <= q) {
        ra = item;
      } else if (sum < p && q < sum + tmp) {
        int l = item.first + (p - sum);
        int r = l + (q - p);
        ra = P(l, r);
      } else if (sum < p && p < sum + tmp) {
        int l = item.first + (p - sum);
        int r = item.second;
        ra = P(l, r);
      } else if (sum < q && q < sum + tmp) {
        ra = P(item.first, item.first + (q - sum) - 1);
      }

      ans += max(0ll, min(r, ra.second) - ra.first + 1ll);
      sum += tmp;
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond)                                                          \\
  if (cond)                                                                    \\
    break;                                                                     \\
  else

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template <typename T> inline void input(vector<T> &v) {
  for (auto &x : v)
    cin >> x;
}

signed main() {
  int n, m, p, q, r;
  vector<vector<P>> v(5001, vector<P>(5000));
  while (cin >> n && n) {
    cin >> m >> p >> q >> r;
    v[0].clear();
    v[0].push_back(P(1, n));
    vector<P> a(m), b(m), c(m);
    rep(i, m) {
      a.clear();
      b.clear();
      c.clear();
      v[i + 1].clear();
      int x, y;
      cin >> x >> y;
      int sum = 0;
      for (P p : v[i]) {
        int tmp = p.second - p.first + 1;
        if (sum + tmp <= x) {
          a.push_back(p);
        } else if (y <= sum) {
          c.push_back(p);
        } else if (x <= sum && sum + tmp <= y) {
          b.push_back(p);
        } else if (sum < x && y < sum + tmp) {
          P fst = P(p.first, p.first + (x - sum) - 1);
          P snd = P(fst.second + 1, fst.second + (y - x));
          P trd = P(snd.second + 1, p.second);
          a.push_back(fst);
          b.push_back(snd);
          c.push_back(trd);
        } else if (sum < x && x < sum + tmp) {
          P fst = P(p.first, p.first + (x - sum) - 1);
          P snd = P(fst.second + 1, p.second);
          a.push_back(fst);
          b.push_back(snd);
        } else if (sum < y && y < sum + tmp) {
          P fst = P(p.first, p.first + (y - sum) - 1);
          P snd = P(fst.second + 1, p.second);
          b.push_back(fst);
          c.push_back(snd);
        }
        sum += tmp;
      }
      v[i + 1].insert(v[i + 1].end(), all(c));
      v[i + 1].insert(v[i + 1].end(), all(b));
      v[i + 1].insert(v[i + 1].end(), all(a));
    }

    int sum = 0;
    int ans = 0;
    p--;
    for (P item : v[m]) {
      int tmp = item.second - item.first + 1;
      P ra = P(1ll << 60, 1ll << 60);
      if (sum + tmp <= p) {
        // skip
      } else if (q <= sum) {
        // skip
      } else if (p <= sum && sum + tmp <= q) {
        ra = item;
      } else if (sum < p && q < sum + tmp) {
        int l = item.first + (p - sum);
        int r = l + (q - p);
        ra = P(l, r);
      } else if (sum < p && p < sum + tmp) {
        int l = item.first + (p - sum);
        int r = item.second;
        ra = P(l, r);
      } else if (sum < q && q < sum + tmp) {
        ra = P(item.first, item.first + (q - sum) - 1);
      }

      ans += max(0ll, min(r, ra.second) - ra.first + 1ll);
      sum += tmp;
    }
    cout << ans << endl;
  }
}