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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

int n, r;
int sum[2][10010];
bool a[2][10010];
vector<pair<int, pi>> q;

int main() {
  while (cin >> n >> r, n) {
    memset(a, 0, sizeof(a));
    memset(sum, 0, sizeof(sum));
    q.clear();

    rep(i, n) {
      int x, y, X, Y;
      cin >> x >> y >> X >> Y;
      q.pb(mp(y, mp(x, 1)));
      q.pb(mp(y, mp(X, -1)));
      q.pb(mp(Y, mp(x, -1)));
      q.pb(mp(Y, mp(X, 1)));
    }
    sort(all(q));
    int cur = 0, next = 1, ii = 0;
    int area = 0, per = 0;

    rep(i, 10005) {
      memset(a[cur], 0, sizeof(a[cur]));

      for (; ii < q.size() && q[ii].first == i; ii++) {
        sum[cur][q[ii].second.first] += q[ii].second.second;
      }
      rep(j, 10005) sum[next][j] = sum[cur][j];
      rep(j, 10005) {
        sum[cur][j + 1] += sum[cur][j];
        a[cur][j + 1] = sum[cur][j] > 0;
      }
      rep(j, 10005) {
        if (a[cur][j])
          area++;
        rep(d, 2) {
          int ny = d ? cur : next, nx = d ? j + 1 : j;
          if (a[cur][j] != a[ny][nx])
            per++;
        }
      }
      swap(cur, next);
    }
    cout << area << endl;
    if (r == 2)
      cout << per << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

int n, r;
int sum[2][10010];
bool a[10010][10010];
vector<pair<int, pi>> q;

int main() {
  while (cin >> n >> r, n) {
    memset(a, 0, sizeof(a));
    memset(sum, 0, sizeof(sum));
    q.clear();

    rep(i, n) {
      int x, y, X, Y;
      cin >> x >> y >> X >> Y;
      q.pb(mp(y, mp(x, 1)));
      q.pb(mp(y, mp(X, -1)));
      q.pb(mp(Y, mp(x, -1)));
      q.pb(mp(Y, mp(X, 1)));
    }
    sort(all(q));
    int cur = 0, next = 1, ii = 0;
    int area = 0, per = 0;

    rep(i, 10005) {
      for (; ii < q.size() && q[ii].first == i; ii++) {
        sum[cur][q[ii].second.first] += q[ii].second.second;
      }
      rep(j, 10005) sum[next][j] = sum[cur][j];
      rep(j, 10005) {
        sum[cur][j + 1] += sum[cur][j];
        a[i + 1][j + 1] = sum[cur][j] > 0;
      }
      swap(cur, next);
    }
    rep(i, 10005) rep(j, 10005) {
      if (a[i][j])
        area++;
      rep(d, 2) {
        const int dy[] = {1, 0}, dx[] = {0, 1};
        int ny = i + dy[d], nx = j + dx[d];
        if (a[i][j] != a[ny][nx])
          per++;
      }
    }
    cout << area << endl;
    if (r == 2)
      cout << per << endl;
  }
  return 0;
}