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

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n, m, p, q, r;
  for (;;) {
    scanf("%d%d%d%d%d", &n, &m, &p, &q, &r);
    if (n == 0)
      break;
    pair<int, int> *cards = new pair<int, int>[1];
    cards[0] = make_pair(1, n);
    int csize = 1;
    for (int ix = 0; ix < m; ix++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int *maif = new int[csize], *mais = new int[csize];
      maif[0] = 1;
      mais[0] = cards[0].second - cards[0].first + 1;
      for (int i = 1; i < csize; i++) {
        mais[i] = mais[i - 1] + (cards[i].second - cards[i].first + 1);
        maif[i] = mais[i - 1] + 1;
      }
      vector<pair<int, int>> a, b, c;
      for (int i = 0; i < csize; i++) {
        if (mais[i] <= x)
          a.push_back(cards[i]);
        else if (y < maif[i])
          c.push_back(cards[i]);
        else if (x < maif[i] && mais[i] <= y)
          b.push_back(cards[i]);
        else if (x < maif[i] && y < mais[i]) {
          b.push_back(
              make_pair(cards[i].first, y + (cards[i].first - maif[i])));
          c.push_back(
              make_pair(y + (cards[i].first - maif[i]) + 1, cards[i].second));
        } else if (maif[i] <= x && mais[i] <= y) {
          a.push_back(
              make_pair(cards[i].first, x + (cards[i].first - maif[i])));
          b.push_back(
              make_pair(x + (cards[i].first - maif[i]) + 1, cards[i].second));
        } else if (maif[i] <= x && y < mais[i]) {
          a.push_back(
              make_pair(cards[i].first, x + (cards[i].first - maif[i])));
          b.push_back(make_pair(x + (cards[i].first - maif[i]) + 1,
                                y + (cards[i].first - maif[i])));
          c.push_back(
              make_pair(y + (cards[i].first - maif[i]) + 1, cards[i].second));
        }
      }
      delete[] cards;
      delete[] maif;
      delete[] mais;
      csize = c.size() + b.size() + a.size();
      cards = new pair<int, int>[csize];
      for (int ii = 0; ii < c.size(); ii++)
        cards[ii] = c[ii];
      for (int ii = 0; ii < b.size(); ii++)
        cards[ii + c.size()] = b[ii];
      for (int ii = 0; ii < a.size(); ii++)
        cards[ii + c.size() + b.size()] = a[ii];
    }
    int *maif = new int[csize], *mais = new int[csize];
    maif[0] = 1;
    mais[0] = cards[0].second - cards[0].first + 1;
    for (int i = 1; i < csize; i++) {
      mais[i] = mais[i - 1] + (cards[i].second - cards[i].first + 1);
      maif[i] = mais[i - 1] + 1;
    }
    int ans = 0;
    for (int i = 0; i < csize; i++) {
      if (p <= maif[i] && mais[i] <= q)
        ans += max(0, min(r, cards[i].second) - cards[i].first + 1);
      else if (maif[i] < p)
        ans += max(0, min(r, cards[i].second) -
                          (cards[i].first + (p - maif[i])) + 1);
      else if (q < mais[i])
        ans += max(0, min(r, cards[i].second - (mais[i] - q)) - cards[i].first +
                          1);
      else
        ans += max(0, min(r, cards[i].second - (mais[i] - q)) -
                          (cards[i].first + (p - maif[i])) + 1);
    }
    printf("%d\\n", ans);
    delete[] cards;
    delete[] maif;
    delete[] mais;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n, m, p, q, r;
  for (;;) {
    scanf("%d%d%d%d%d", &n, &m, &p, &q, &r);
    if (n == 0)
      break;
    pair<int, int> *cards = new pair<int, int>[1];
    cards[0] = make_pair(1, n);
    int csize = 1;
    for (int ix = 0; ix < m; ix++) {
      int x, y;
      scanf("%d%d", &x, &y);
      int *maif = new int[csize], *mais = new int[csize];
      maif[0] = 1;
      mais[0] = cards[0].second - cards[0].first + 1;
      for (int i = 1; i < csize; i++) {
        mais[i] = mais[i - 1] + (cards[i].second - cards[i].first + 1);
        maif[i] = mais[i - 1] + 1;
      }
      vector<pair<int, int>> a, b, c;
      for (int i = 0; i < csize; i++) {
        if (mais[i] <= x)
          a.push_back(cards[i]);
        else if (y < maif[i])
          c.push_back(cards[i]);
        else if (x < maif[i] && mais[i] <= y)
          b.push_back(cards[i]);
        else if (x < maif[i] && y < mais[i]) {
          b.push_back(
              make_pair(cards[i].first, y + (cards[i].first - maif[i])));
          c.push_back(
              make_pair(y + (cards[i].first - maif[i]) + 1, cards[i].second));
        } else if (maif[i] <= x && mais[i] <= y) {
          a.push_back(
              make_pair(cards[i].first, x + (cards[i].first - maif[i])));
          b.push_back(
              make_pair(x + (cards[i].first - maif[i]) + 1, cards[i].second));
        } else if (maif[i] <= x && y < mais[i]) {
          a.push_back(
              make_pair(cards[i].first, x + (cards[i].first - maif[i])));
          b.push_back(make_pair(x + (cards[i].first - maif[i]) + 1,
                                y + (cards[i].first - maif[i])));
          c.push_back(
              make_pair(y + (cards[i].first - maif[i]) + 1, cards[i].second));
        }
      }
      delete[] cards, maif, mais;
      csize = c.size() + b.size() + a.size();
      cards = new pair<int, int>[csize];
      for (int ii = 0; ii < c.size(); ii++)
        cards[ii] = c[ii];
      for (int ii = 0; ii < b.size(); ii++)
        cards[ii + c.size()] = b[ii];
      for (int ii = 0; ii < a.size(); ii++)
        cards[ii + c.size() + b.size()] = a[ii];
    }
    int *maif = new int[csize], *mais = new int[csize];
    maif[0] = 1;
    mais[0] = cards[0].second - cards[0].first + 1;
    for (int i = 1; i < csize; i++) {
      mais[i] = mais[i - 1] + (cards[i].second - cards[i].first + 1);
      maif[i] = mais[i - 1] + 1;
    }
    int ans = 0;
    for (int i = 0; i < csize; i++) {
      if (p <= maif[i] && mais[i] <= q)
        ans += max(0, min(r, cards[i].second) - cards[i].first + 1);
      else if (maif[i] < p)
        ans += max(0, min(r, cards[i].second) -
                          (cards[i].first + (p - maif[i]) + 1));
      else if (q < mais[i])
        ans += max(0, min(r, cards[i].second - (mais[i] - q)) - cards[i].first +
                          1);
      else
        ans += max(0, min(r, cards[i].second - (mais[i] - q)) -
                          (cards[i].first + (p - maif[i]) + 1));
    }
    printf("%d\\n", ans);
  }
}