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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <tuple>
#include <vector>

using namespace std;

short map[10001];
short state_before[10001];
short map_before[10001];

std::vector<tuple<int, int, int>> points;

void read_points(int tate) {
  memcpy(map_before, map, sizeof map);
  memset(map, 0, sizeof(map));
  for (int i = 0; i < points.size(); i++) {
    auto tmp = points.at(i);
    if (get<0>(tmp) == tate) {
      map[get<1>(tmp)] += get<2>(tmp);
      // printf("%d,%d,%d\\n",get<0>(tmp),get<1>(tmp),get<2>(tmp));
    }
  }
}
int main() {

  int n, r;

  int S = 0;
  int L = 0;

  while (1) {
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;

    points.clear();
    int x1, y1, x2, y2;
    int max_x = 0, min_x = 10000, max_y = 0, min_y = 10000;
    for (int i = 0; i < n; i++) {
      cin >> x1 >> y1 >> x2 >> y2;
      tuple<int, int, int> tl = make_tuple(y1, x1, 1);
      tuple<int, int, int> tr = make_tuple(y1, x2, -1);
      tuple<int, int, int> bl = make_tuple(y2, x1, -1);
      tuple<int, int, int> br = make_tuple(y2, x2, 1);
      points.push_back(tl);
      points.push_back(tr);
      points.push_back(bl);
      points.push_back(br);
      if (max_x < x2)
        max_x = x2;
      if (max_y < y2)
        max_y = y2;
      if (min_x > x1)
        min_x = x1;
      if (min_y > y1)
        min_y = y1;
    }

    S = 0;
    L = 0;
    for (int h = min_y; h <= max_y; h++) {

      read_points(h);
      int yoko_state = 0;
      for (int w = min_x; w <= max_x; w++) {

        yoko_state += map[w];
        map[w] = yoko_state;
        if (h != 0) {
          map[w] += state_before[w];
        }
        state_before[w] = map[w];
        // printf("%2d ",map[w]);
        if (map[w] > 0) {
          S++;
          if (w == 0)
            L += 2;
          else if (map[w - 1] == 0)
            L += 2;
          if (h == 0)
            L += 2;
          else if (map_before[w] == 0)
            L += 2;
        }
      }
      // cout<<endl;
    }

    cout << S << endl;
    if (r == 2) {
      cout << L << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <stdio.h>
using namespace std;

short map[10001][10001];

int main() {

  int n, r;

  int S = 0;
  int L = 0;
  int state;

  while (1) {
    cin >> n >> r;
    if (n == 0 && r == 0)
      break;

    memset(map, 0, sizeof(map));

    int x_min = 10001, x_max = 0, y_min = 10001, y_max = 0;

    int x1, y1, x2, y2;
    for (int i = 0; i < n; i++) {

      cin >> x1 >> y1 >> x2 >> y2;

      map[x1][y1] += 1;
      map[x1][y2] -= 1;
      map[x2][y1] -= 1;
      map[x2][y2] += 1;

      if (x_min > x1)
        x_min = x1;
      if (y_min > y1)
        y_min = y1;
      if (x_max < x2)
        x_max = x2;
      if (y_max < y2)
        y_max = y2;
    }

    S = 0;
    L = 0;

    for (int h = y_min; h <= y_max; h++) {
      state = 0;
      for (int w = x_min; w <= x_max; w++) {
        state += map[w][h];
        map[w][h] = state;
      }
    }
    for (int w = x_min; w <= x_max; w++) {
      state = 0;
      for (int h = y_min; h <= y_max; h++) {
        state += map[w][h];
        map[w][h] = state;
        if (map[w][h] > 0) {
          S++;
          if (w == 0)
            L += 2;
          else if (map[w - 1][h] == 0)
            L += 2;
          if (h == 0)
            L += 2;
          else if (map[w][h - 1] == 0)
            L += 2;
        }
      }
    }
    cout << S << endl;
    if (r == 2) {
      cout << L << endl;
    }
  }
}