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

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int a[10005], b[10005], c[10005], d[10005];
int main() {
  int n, m;
  while (1) {
    vector<P> y;
    vector<P> z;
    scanf("%d %d", &n, &m);
    if (!n)
      return 0;
    int lim = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
      lim = max(lim, d[i] - 1);
    }
    int ret = 0, ret2 = 0;
    int prev = INF;
    vector<P> prev2(0);
    for (int i = 0; i <= lim; i++) {
      y.clear();
      for (int j = 0; j < n; j++) {
        if (b[j] <= i && i < d[j])
          y.pb(mp(a[j], c[j]));
      }
      if (y.empty())
        continue;
      sort(y.begin(), y.end());
      int beg = y[0].first;
      int en = y[0].second;
      y.pb(mp(INF, INF));
      z.clear();
      for (int j = 1; j < y.size(); j++) {

        if (en < y[j].first) { // cout << beg << " " << en << endl;

          z.pb(mp(beg, en));
          ret += (en - beg);
          ret2 += 2; // cout << 2 << "HB" <<endl;
          if (i == lim)
            ret2 += (en - beg);
          if (prev != i - 1) {
            ret2 += (en - beg); // cout << en-beg << "HB" <<endl;
          } else {
            ret2 += (en - beg);
            for (int cur = 0; cur < prev2.size(); cur++) {
              // cout << prev2[cur].first << " " << prev2[cur].second << endl;
              ret2 -= (max(0, min(en, prev2[cur].second) -
                                  max(beg, prev2[cur].first)) *
                       2);
              // cout <<
              // -(max(0,min(en,prev2[cur].second)-max(beg,prev2[cur].first))*2)
              // << "HB" <<endl;
            }
          }
          beg = y[j].first;
          en = y[j].second;

        } else {
          if (y[j].second <= en) {
            en = max(en, y[j].second);
          } else {
            en = max(en, y[j].second);
          }
        }
      }
      for (int cur = 0; cur < prev2.size();
           cur++) { // if(i==lim) cout << (prev2[cur].second-prev2[cur].first)
                    // << "HB" <<endl;
        ret2 += (prev2[cur].second - prev2[cur].first);
      }
      // printf("%d\\n",ret2);
      prev = i;
      prev2 = z;
    }
    printf("%d\\n", ret);
    if (m == 2)
      printf("%d\\n", ret2);
  }
}

Error: MLE

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  int n, m;
  while (1) {
    vector<P> y[10005];
    vector<P> z[10005];
    scanf("%d %d", &n, &m);
    if (!n)
      return 0;
    int lim = 0;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      scanf("%d %d %d %d", &a, &b, &c, &d);
      for (int j = b; j < d; j++) {
        y[j].pb(mp(a, c));
      }
      lim = max(lim, d - 1);
    }
    for (int i = 0; i < 10005; i++)
      sort(y[i].begin(), y[i].end());
    int ret = 0, ret2 = 0;
    int prev = INF;
    vector<P> prev2(0);
    for (int i = 0; i <= lim; i++) {
      if (y[i].empty())
        continue;
      int beg = y[i][0].first;
      int en = y[i][0].second;
      y[i].pb(mp(INF, INF));
      for (int j = 1; j < y[i].size(); j++) {

        if (en < y[i][j].first) { // cout << beg << " " << en << endl;

          z[i].pb(mp(beg, en));
          ret += (en - beg);
          ret2 += 2; // cout << 2 << "HB" <<endl;
          if (i == lim)
            ret2 += (en - beg);
          if (prev != i - 1) {
            ret2 += (en - beg); // cout << en-beg << "HB" <<endl;
          } else {
            ret2 += (en - beg);
            for (int cur = 0; cur < prev2.size(); cur++) {
              // cout << prev2[cur].first << " " << prev2[cur].second << endl;
              ret2 -= (max(0, min(en, prev2[cur].second) -
                                  max(beg, prev2[cur].first)) *
                       2);
              // cout <<
              // -(max(0,min(en,prev2[cur].second)-max(beg,prev2[cur].first))*2)
              // << "HB" <<endl;
            }
          }
          beg = y[i][j].first;
          en = y[i][j].second;

        } else {
          if (y[i][j].second <= en) {
            en = max(en, y[i][j].second);
          } else {
            en = max(en, y[i][j].second);
          }
        }
      }
      for (int cur = 0; cur < prev2.size();
           cur++) { // if(i==lim) cout << (prev2[cur].second-prev2[cur].first)
                    // << "HB" <<endl;
        ret2 += (prev2[cur].second - prev2[cur].first);
      }
      // printf("%d\\n",ret2);
      y[i].clear();
      prev = i;
      prev2 = z[i];
      z[i].clear();
    }
    printf("%d\\n", ret);
    if (m == 2)
      printf("%d\\n", ret2);
  }
}