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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <vector>
using namespace std;
int sheets[3][10007], tmp[10007];

int main() {
  while (1) {
    int n, r;
    int x1, y1, x2, y2, mx = 0, my = 0, menseki = 0, gaishuu = 0;
    vector<pair<int, int>> points;
    map<pair<int, int>, int> nums;
    scanf("%d%d", &n, &r);
    memset(sheets, 0, sizeof(sheets));
    if (n == 0 && r == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      x1++, y1++, x2++, y2++;
      points.push_back(make_pair(x1, y1));
      points.push_back(make_pair(x1, y2));
      points.push_back(make_pair(x2, y1));
      points.push_back(make_pair(x2, y2));
      if (nums.count(make_pair(x1, y1)) == 0)
        nums[make_pair(x1, y1)] = 0;
      if (nums.count(make_pair(x2, y1)) == 0)
        nums[make_pair(x2, y1)] = 0;
      if (nums.count(make_pair(x1, y2)) == 0)
        nums[make_pair(x1, y2)] = 0;
      if (nums.count(make_pair(x2, y2)) == 0)
        nums[make_pair(x2, y2)] = 0;
      nums[make_pair(x1, y1)]++;
      nums[make_pair(x2, y2)]++;
      nums[make_pair(x1, y2)]--;
      nums[make_pair(x2, y1)]--;
      mx = max(mx, x1);
      mx = max(mx, x2);
      my = max(my, y1);
      my = max(my, y2);
    }
    sort(points.begin(), points.end(), greater<pair<int, int>>());
    memset(tmp, 0, sizeof(tmp));
    for (int i = 1; i <= mx; i++) {
      for (int j = 1; j <= my + 1; j++) {
        if (points.back().first == i && points.back().second == j) {
          tmp[j] += nums[points.back()];
          pair<int, int> tmpr = points.back();
          while (points.back() == tmpr)
            points.pop_back();
        }
        sheets[i % 3][j] = sheets[i % 3][j - 1] + tmp[j];
        if (sheets[i % 3][j] > 0)
          menseki++;
        if (r == 2) {
          if (sheets[i % 3][j] > 0) {
            if (sheets[(i - 1) % 3][j] == 0)
              gaishuu++;
            if (sheets[i % 3][j - 1] == 0)
              gaishuu++;
          }
          if (sheets[i % 3][j] == 0) {
            if (sheets[(i - 1) % 3][j] > 0)
              gaishuu++;
            if (sheets[i % 3][j - 1] > 0)
              gaishuu++;
          }
        }
      }
    }
    printf("%d\\n", menseki);
    if (r == 2) {
      printf("%d\\n", gaishuu);
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int g[10003][10003];
int n, r;

int main() {
  while (scanf("%d%d", &n, &r), n) {
    memset(g, 0, sizeof(g));
    int mx = 10001, Mx = 1, my = 10001, My = 1;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      a++;
      b++;
      c++;
      d++;
      g[a][b]++;
      g[a][d]--;
      g[c][b]--;
      g[c][d]++;
      mx = min(mx, a);
      Mx = max(Mx, c);
      my = min(my, b);
      My = max(My, d);
    }

    for (int i = mx; i <= Mx; i++) {
      int t = 0;
      for (int j = my; j <= My; j++) {
        t += g[i][j];
        g[i][j] = t;
      }
    }

    int s = 0;
    for (int j = my; j <= My; j++) {
      int t = 0;
      for (int i = mx; i <= Mx; i++) {
        t += g[i][j];
        if (g[i][j] = t)
          s++;
      }
    }
    printf("%d\\n", s);

    if (r == 2) {
      int l = 0;
      for (int i = mx; i <= Mx; i++) {
        for (int j = my; j <= My; j++) {
          if (g[i][j]) {
            l += !g[i - 1][j] + !g[i + 1][j] + !g[i][j - 1] + !g[i][j + 1];
          }
        }
      }
      printf("%d\\n", l);
    }
  }
  return 0;
}