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

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    int M;
    cin >> M;
    int P, Q, R;
    cin >> P >> Q >> R;
    --P;

    vector<PII> s[2];
    int crt = 0, nxt = 1;
    s[crt].PB(MP(0, R));
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      s[nxt].clear();

      for (auto r : s[crt]) {
        int l1 = N - b;
        int l2 = N - a;

        if (r.FF < a) {
          s[nxt].EB(l2 + r.FF, min(N, l2 + r.SS));
        }
        if (b < r.SS) {
          s[nxt].EB(max(0, r.FF - b), r.SS - b);
        }
        int lft = max(a, r.FF);
        int rht = min(b, r.SS);
        if (rht - lft > 0 && a <= lft && rht <= b) {
          s[nxt].EB(l1 + lft - a, l1 + rht - a);
        }
      }
      swap(crt, nxt);
    }

    int ans = 0;
    for (auto &&ra : s[crt]) {
      if (ra.SS <= P || Q <= ra.FF)
        continue;
      int l = max(P, ra.FF);
      int r = min(Q, ra.SS);

      ans += max(0, r - l);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    int M;
    cin >> M;
    int P, Q, R;
    cin >> P >> Q >> R;
    --P;

    vector<pair<PII, int>> s[2];
    int crt = 0, nxt = 1;
    s[crt].PB(MP(MP(0, N), 0));
    REP(i, M) {
      int a, b;
      cin >> a >> b;
      --a;
      s[nxt].clear();

      for (auto x : s[crt]) {
        auto r = x.FF;
        int ix = x.SS;
        int l1 = N - b;
        int l2 = l1 + b - a;

        if (r.SS <= a) {
          s[nxt].PB(MP(MP(l2 + r.FF, l2 + r.SS), ix));
        } else if (r.FF < a) {
          if (r.SS <= b) {
            s[nxt].PB(MP(MP(l2 + r.FF, N), ix));
            if (ix + a - r.FF < R)
              s[nxt].PB(MP(MP(l1, l1 + r.SS - a), ix + a - r.FF));
          } else {
            s[nxt].PB(MP(MP(l2 + r.FF, l2 + a), ix));
            if (ix + a - r.FF < R)
              s[nxt].PB(MP(MP(l1, l2), ix + a - r.FF));
            if (ix + b - r.FF < R)
              s[nxt].PB(MP(MP(0, r.SS - b), ix + b - r.FF));
          }
        } else if (r.FF < b) {
          if (r.SS <= b) {
            s[nxt].PB(MP(MP(l1 + r.FF - a, l1 + r.SS - a), ix));
          } else {
            s[nxt].PB(MP(MP(l1 + r.FF - a, b), ix));
            if (ix + b - r.FF < R)
              s[nxt].PB(MP(MP(0, r.SS - b), ix + b - r.FF));
          }
        } else {
          s[nxt].PB(MP(MP(r.FF - b, r.SS - b), ix));
        }
      }
      swap(crt, nxt);
    }

    int ans = 0;
    for (auto &&p : s[crt]) {
      auto ra = p.FF;
      int ix = p.SS;
      if (ra.SS <= P || Q <= ra.FF)
        continue;
      int l = max(P, ra.FF);
      int r = min(Q, ra.SS);
      int bix = ix + l - ra.FF;
      if (bix < R) {
        ans += min(r - l, R - bix);
      }
    }
    cout << ans << endl;
  }

  return 0;
}