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

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m, p, q, r;

int main() {
  while (scanf("%d", &n), n) {
    scanf("%d", &m);
    scanf("%d%d%d", &p, &q, &r);
    vector<P> *z = new vector<P>;
    z->push_back(P(1, n));
    for (int i = 0; i < m; i++) {
      int x[3] = {0, 0, n};
      scanf("%d%d", &x[0], &x[1]);
      vector<P> *b = new vector<P>;
      vector<P> c[3];
      int count = 0, cx = 1;
      int j = 0;
      while (cx <= n)
        if (cx + (z->at(j).second) - (z->at(j).first) <= x[count]) {
          c[count].push_back(z->at(j));
          cx += (z->at(j).second) - (z->at(j).first) + 1;
          j++;
        } else {
          if (cx != x[count] + 1)
            c[count].push_back(
                P(z->at(j).first, z->at(j).first + x[count] - cx));
          z->at(j).first += x[count] - cx + 1;
          cx = x[count] + 1;
          count++;
        }
      for (j = 2; j >= 0; j--)
        for (int k = 0; k < c[j].size(); k++)
          b->push_back(c[j][k]);
      delete z;
      z = b;
    }
    int res = 0, count = 1;
    for (int i = 0; i < z->size(); i++) {
      P pp = z->at(i);
      int c1 = count + pp.second - pp.first;
      if (count >= p && q >= c1)
        ;
      else if (count <= p && p <= c1)
        pp.first += p - count;
      else if (count <= q && q <= c1)
        pp.second -= c1 - q;
      else {
        count = c1 + 1;
        continue;
      }
      count = c1 + 1;
      if (r >= pp.second)
        res += pp.second - pp.first + 1;
      else if (r >= pp.first)
        res += r - pp.first + 1;
    }
    printf("%d\\n", res);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m, p, q, r;

int main() {
  while (scanf("%d", &n), n) {
    scanf("%d", &m);
    scanf("%d%d%d", &p, &q, &r);
    vector<P> *z = new vector<P>;
    z->push_back(P(1, n));
    for (int i = 0; i < m; i++) {
      int x[3] = {0, 0, n};
      scanf("%d%d", &x[0], &x[1]);
      vector<P> *b = new vector<P>;
      vector<P> c[3];
      int count = 0, cx = 1;
      int j = 0;
      while (cx <= n)
        if (cx + (z->at(j).second) - (z->at(j).first) <= x[count]) {
          c[count].push_back(z->at(j));
          cx += (z->at(j).second) - (z->at(j).first) + 1;
          j++;
        } else {
          if (cx != x[count] + 1)
            c[count].push_back(
                P(z->at(j).first, z->at(j).first + x[count] - cx));
          z->at(j).first += x[count] - cx + 1;
          cx = x[count] + 1;
          count++;
        }
      for (j = 2; j >= 0; j--)
        for (int k = 0; k < c[j].size(); k++)
          b->push_back(c[j][k]);
      z = b;
    }
    int res = 0, count = 1;
    for (int i = 0; i < z->size(); i++) {
      P pp = z->at(i);
      int c1 = count + pp.second - pp.first;
      if (count >= p && q >= c1)
        ;
      else if (count <= p && p <= c1)
        pp.first += p - count;
      else if (count <= q && q <= c1)
        pp.second -= c1 - q;
      else {
        count = c1 + 1;
        continue;
      }
      count = c1 + 1;
      if (r >= pp.second)
        res += pp.second - pp.first + 1;
      else if (r >= pp.first)
        res += r - pp.first + 1;
    }
    printf("%d\\n", res);
  }
}