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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> State;

bool compareState(const State &ls, const State &rs) {
  const P &lp = ls.first, rp = rs.first;
  if (lp.second == rp.second) {
    return lp.first < rp.first;
  }
  return lp.second < rp.second;
}

int main() {
  int N, R;
  while (std::cin >> N >> R, N) {
    int minX = INF, maxX = 0, minY = INF, maxY = 0;
    std::vector<State> S;
    REP(i, N) {
      int x1, y1, x2, y2;
      std::cin >> x1 >> y1 >> x2 >> y2;

      minX = std::min(minX, x1);
      maxX = std::max(maxX, x2);
      minY = std::min(minY, y1);
      maxY = std::max(maxY, y2);

      S.push_back(mp(mp(x1, y1), 1));
      S.push_back(mp(mp(x1, y2), -1));
      S.push_back(mp(mp(x2, y1), -1));
      S.push_back(mp(mp(x2, y2), 1));
    }

    std::sort(S.begin(), S.end(), compareState);

    int board[2][10001], pos = 0;
    REP(i, 2) {
      REP(j, 10001) { board[i][j] = 0; }
    }

    // REP(i, S.size()){
    //     printf("(%d, %d): %d\\n", S[i].first.first, S[i].first.second,
    //     S[i].second);
    // }

    // std::cout << minX << ", " << maxX << std::endl;
    // std::cout << minY << ", " << maxY << std::endl;

    int l = 0, sq = 0;
    FOR(i, minY, maxY + 1) {
      FOR(j, minX, maxX + 1) { board[1][j] = 0; }

      if (pos < S.size()) {
        while (S[pos].first.second == i) {
          board[1][S[pos].first.first] += S[pos].second;
          pos++;
        }
        // printf("%d, %d\\n", i, pos);

        FOR(j, minX + 1, maxX + 1) { board[1][j] += board[1][j - 1]; }
      }

      FOR(j, minX, maxX + 1) { board[1][j] += board[0][j]; }

      FOR(j, minX, maxX + 1) {
        // printf("%d, ", board[1][j]);

        if (board[1][j] > 0) {
          sq += 1;

          if (j == 0 || board[1][j - 1] == 0) {
            l += 1;
          }
          if (j == 10000 || board[1][j + 1] == 0) {
            l += 1;
          }
          l += (board[0][j] == 0) * 2;
        }
      }
      // puts("");
      // printf("%d\\n", l);

      std::swap(board[0], board[1]);
    }

    FOR(i, minX, maxX + 1) {
      if (board[0][i] > 0) {
        l += 1;
      }
    }

    std::cout << sq << std::endl;
    if (R == 2) {
      std::cout << l << std::endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)

const int INF = 1 << 24;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

typedef unsigned long long ull;

int N, R, min_x, max_x, min_y, max_y;
int R_2[10010][10010];

int get(int x, int y) {
  if (0 <= x && x <= 10000 && 0 <= y && y <= 10000) {
    return R_2[x][y];
  }
  return 0;
}

int solve_S() {
  int res = 0;
  FOR(i, min_y, max_y + 1) {
    FOR(j, min_x, max_x + 1) {
      if (R_2[j][i] > 0) {
        res++;
      }
    }
  }

  return res;
}

int solve_L() {
  int res = 0;
  FOR(i, min_y, max_y + 1) {
    FOR(j, min_x, max_x + 1) {
      if (R_2[j][i] > 0) {
        res +=
            !get(j - 1, i) + !get(j + 1, i) + !get(j, i - 1) + !get(j, i + 1);
      }
    }
  }

  return res;
}

int main() {
  while (std::cin >> N >> R, N) {
    min_x = 10000;
    max_x = 0;
    min_y = 10000;
    max_y = 0;

    REP(i, 10001) {
      REP(j, 10001) { R_2[j][i] = 0; }
    }

    REP(i, N) {
      int x1, y1, x2, y2;
      std::cin >> x1 >> y1 >> x2 >> y2;

      R_2[x1][y1]++;
      R_2[x1][y2]--;
      R_2[x2][y1]--;
      R_2[x2][y2]++;

      min_x = std::min(min_x, x1);
      max_x = std::max(max_x, x2);
      min_y = std::min(min_y, y1);
      max_y = std::max(max_y, y2);
    }

    // X
    FOR(i, min_y, max_y + 1) {
      FOR(j, min_x + 1, max_x + 1) { R_2[j][i] += R_2[j - 1][i]; }
    }

    // Y
    FOR(i, min_x, max_x + 1) {
      FOR(j, min_y + 1, max_y + 1) { R_2[i][j] += R_2[i][j - 1]; }
    }

    if (R == 1) {
      std::cout << solve_S() << std::endl;
    } else {
      std::cout << solve_S() << std::endl;
      std::cout << solve_L() << std::endl;
    }
  }
}