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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define INF 2000000000
#define LLINF 4000000000000000000
#define SIZE 10002

int mm[2][SIZE];
int mo[5] = {0, -1, 0, 1};

void calc(int n, int r) {
  int x_1, x_2, y_1, y_2, minx = SIZE - 1, maxx = 0, miny = SIZE - 1, maxy = 0;
  int ansV = 0, ansL = 0;
  vector<int> x_p[SIZE], x_m[SIZE];

  memset(mm, 0, sizeof(mm));

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &x_1, &y_1, &x_2, &y_2);

    x_1++;
    y_1++;
    x_2++;
    y_2++;

    x_p[x_1].push_back(y_1);
    x_m[x_1].push_back(y_2);
    x_m[x_2].push_back(y_1);
    x_p[x_2].push_back(y_2);

    minx = min(minx, x_1);
    miny = min(miny, y_1);
    maxx = max(maxx, x_2);
    maxy = max(maxy, y_2);
  }

  for (int i = minx; i < maxx; i++) {

    for (int j = miny; j <= maxy; j++) {
      mm[0][j] = mm[1][j];
    }

    memset(mm[1], 0, sizeof(mm[1]));

    for (int j = 0; j < x_p[i].size(); j++) {
      mm[1][x_p[i][j]]++;
    }
    for (int j = 0; j < x_m[i].size(); j++) {
      mm[1][x_m[i][j]]--;
    }

    for (int j = miny + 1; j <= maxy; j++) {
      mm[1][j] += mm[1][j - 1];
    }

    for (int j = miny; j <= maxy; j++) {
      mm[1][j] += mm[0][j];
    }

    for (int j = miny; j <= maxy; j++) {
      if (mm[1][j] > 0) {
        ansV++;

        for (int k = 0; k < 3; k++) {
          if (mm[1 + mo[k]][j + mo[k + 1]] == 0) {
            ansL++;
          }
        }
      } else {
        if (mm[0][j] > 0) {
          ansL++;
        }
      }
    }
  }

  for (int j = minx; j <= maxx; j++) {
    if (mm[1][j] == 1)
      ansL++;
  }

  printf("%d\\n", ansV);

  if (r == 2)
    printf("%d\\n", ansL);

  return;
}

int main() {
  int n, r;

  while (1) {
    scanf("%d%d", &n, &r);
    if (n == 0)
      break;
    calc(n, r);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define INF 2000000000
#define LLINF 4000000000000000000
#define SIZE 10002

short mm[SIZE][SIZE];
int mo[5] = {0, 1, 0, -1, 0};
void calc(int n, int r) {
  int x_1, x_2, y_1, y_2, minx = 0, maxx = SIZE - 1, miny = 0, maxy = SIZE - 1;
  int ansV, ansL;

  memset(mm, 0, sizeof(mm));

  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &x_1, &y_1, &x_2, &y_2);

    x_1++;
    y_1++;
    x_2++;
    y_2++;

    mm[x_1][y_1]++;
    mm[x_2][y_1]--;
    mm[x_1][y_2]--;
    mm[x_2][y_2]++;

    minx = min(minx, x_1);
    miny = min(miny, y_1);
    maxx = max(maxx, x_2);
    maxy = max(maxy, y_2);
  }

  for (int i = miny; i <= maxy; i++) {
    for (int j = minx + 1; j <= maxx; j++) {
      mm[j][i] += mm[j - 1][i];
    }
  }

  for (int i = miny; i <= maxy; i++) {
    for (int j = minx + 1; j <= maxx; j++) {
      mm[i][j] += mm[i][j - 1];
    }
  }

  for (int i = miny; i < maxy; i++) {
    for (int j = minx; j < maxx; j++) {
      if (mm[i][j] > 0) {
        ansV++;

        for (int k = 0; k < 4; k++) {
          if (mm[i + mo[k]][j + mo[k + 1]] == 0) {
            ansL++;
          }
        }
      }
    }
  }

  printf("%d\\n", ansV);

  if (r == 2)
    printf("%d\\n", ansL);

  return;
}

int main() {
  int n, r;

  while (1) {
    scanf("%d%d", &n, &r);
    if (n == 0)
      break;
    calc(n, r);
  }

  return 0;
}