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

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

int solve();
vector<pair<int, int>> shuffle(vector<pair<int, int>> vec, int i);

int N, M, P, Q, R;
int X[5010], Y[5010];

int main() {
  while (scanf("%d", &N), N) {
    scanf("%d", &M);
    scanf("%d%d%d", &P, &Q, &R);
    for (int i = 0; i < M; i++) {
      scanf("%d%d", &X[i], &Y[i]);
    }
    printf("%d\\n", solve());
  }
}

vector<pair<int, int>> shuffle(vector<pair<int, int>> vec, int i) {
  vector<pair<int, int>> new_vec;
  for (int j = 0; j < vec.size(); j++) {
    int l = vec[j].first, r = vec[j].second;
    if (l <= X[i] && r <= X[i]) {
      new_vec.push_back(make_pair(l + N - X[i], r + N - X[i]));
      continue;
    }
    if (l <= X[i] && X[i] < r && r <= Y[i]) {
      new_vec.push_back(make_pair(l + N - X[i], N));
      new_vec.push_back(make_pair(N - Y[i] + 1, r + N - X[i] - Y[i]));
      continue;
    }
    if (X[i] < l && l <= Y[i] && X[i] < r && r <= Y[i]) {
      new_vec.push_back(make_pair(l + N - X[i] - Y[i], r + N - X[i] - Y[i]));
      continue;
    }
    if (X[i] < l && l <= Y[i] && Y[i] < r) {
      new_vec.push_back(make_pair(l + N - X[i] - Y[i], N - X[i]));
      new_vec.push_back(make_pair(1, r - Y[i]));
      continue;
    }
    if (Y[i] < l && Y[i] < r) {
      new_vec.push_back(make_pair(l - Y[i], r - Y[i]));
      continue;
    }
    if (l <= X[i] && Y[i] < r) {
      new_vec.push_back(make_pair(l + N - X[i], N));
      new_vec.push_back(make_pair(N - Y[i] + 1, N - X[i]));
      new_vec.push_back(make_pair(1, r - Y[i]));
      continue;
    }
  }
  return new_vec;
}

int solve() {
  int ANS = 0;
  vector<pair<int, int>> vec;
  vec.push_back(make_pair(1, R));
  for (int i = 0; i < M; i++) {
    vec = shuffle(vec, i);
  }
  for (int i = 0; i < vec.size(); i++) {
    int l = vec[i].first, r = vec[i].second;
    int a = max(l, P), b = min(r, Q) + 1;
    ANS += max(0, b - a);
  }
  return ANS;
}

Error: MLE

Fixed submission:

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

int solve();

int N, M, P, Q, R;
int X[5010], Y[5010];

int main() {
  while (scanf("%d", &N), N) {
    scanf("%d", &M);
    scanf("%d%d%d", &P, &Q, &R);
    for (int i = 0; i < M; i++) {
      scanf("%d%d", &X[i], &Y[i]);
    }
    printf("%d\\n", solve());
  }
}

int solve() {
  int ANS = 0;
  vector<pair<int, int>> vec[5010];
  vec[0].push_back(make_pair(1, R));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      int l = vec[i][j].first, r = vec[i][j].second;
      if (l <= X[i] && r <= X[i]) {
        vec[i + 1].push_back(make_pair(l + N - X[i], r + N - X[i]));
        continue;
      }
      if (l <= X[i] && X[i] < r && r <= Y[i]) {
        vec[i + 1].push_back(make_pair(l + N - X[i], N));
        vec[i + 1].push_back(make_pair(N - Y[i] + 1, r + N - X[i] - Y[i]));
        continue;
      }
      if (X[i] < l && l <= Y[i] && X[i] < r && r <= Y[i]) {
        vec[i + 1].push_back(
            make_pair(l + N - X[i] - Y[i], r + N - X[i] - Y[i]));
        continue;
      }
      if (X[i] < l && l <= Y[i] && Y[i] < r) {
        vec[i + 1].push_back(make_pair(l + N - X[i] - Y[i], N - X[i]));
        vec[i + 1].push_back(make_pair(1, r - Y[i]));
        continue;
      }
      if (Y[i] < l && Y[i] < r) {
        vec[i + 1].push_back(make_pair(l - Y[i], r - Y[i]));
        continue;
      }
      if (l <= X[i] && Y[i] < r) {
        vec[i + 1].push_back(make_pair(l + N - X[i], N));
        vec[i + 1].push_back(make_pair(N - Y[i] + 1, N - X[i]));
        vec[i + 1].push_back(make_pair(1, r - Y[i]));
        continue;
      }
    }
    vec[i].clear();
  }
  for (int i = 0; i < vec[M].size(); i++) {
    int l = vec[M][i].first, r = vec[M][i].second;
    int a = max(l, P), b = min(r, Q) + 1;
    ANS += max(0, b - a);
  }
  return ANS;
}