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

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

int bit_n;
int dat[10010];
void init(int n) {
  bit_n = n;
  memset(dat, 0, sizeof(dat));
}
void add(int k, int x) {
  for (k++; k <= bit_n; k += k & -k)
    dat[k] += x;
}
int sum(int k) {
  int ret = 0;
  for (k++; k; k -= k & -k)
    ret += dat[k];
  return ret;
}

struct event {
  int y, id, in;
  event(int y, int id, int in) : y(y), id(id), in(in) {}
  bool operator<(const event &e) const { return y < e.y; }
};

int N, r;
vector<event> es;
int x1[10000], y1[10000], x2[10000], y2[10000];

int cnt[10020];
void solve1() {
  int ret = 0;
  memset(cnt, 0, sizeof(cnt));
  int cur = 0, prevy = 0;
  while (cur < es.size()) {
    int sum = 0;
    for (int i = 0; i < 10010; i++)
      if (cnt[i])
        sum++;
    ret += sum * (es[cur].y - prevy);
    prevy = es[cur].y;
    int next = cur;
    while (next < es.size() && es[next].y == es[cur].y) {
      int id = es[next].id;
      for (int i = x1[id]; i < x2[id]; i++) {
        if (es[next].in)
          cnt[i]++;
        else
          cnt[i]--;
      }
      next++;
    }
    cur = next;
  }

  cout << ret << endl;
}

void solve2() {
  int ret = 0;
  memset(cnt, 0, sizeof(cnt));
  int cur = 0, prevy = 0;
  while (cur < es.size()) {
    int sum = 0;
    for (int i = 0; i < 10010; i++) {
      if (cnt[i] != 0)
        continue;
      if (i && cnt[i - 1])
        sum++;
      if (cnt[i + 1])
        sum++;
    }
    ret += sum * (es[cur].y - prevy);
    prevy = es[cur].y;
    int next = cur;
    int memo[10020];
    for (int i = 0; i < 10020; i++)
      memo[i] = cnt[i];
    while (next < es.size() && es[next].y == es[cur].y) {
      int id = es[next].id;
      for (int i = x1[id]; i < x2[id]; i++) {
        if (es[next].in)
          cnt[i]++;
        else
          cnt[i]--;
      }
      next++;
    }
    for (int i = 0; i < 10020; i++)
      if (memo[i] != cnt[i] && (memo[i] == 0 || cnt[i] == 0))
        ret++;
    cur = next;
  }

  cout << ret << endl;
}

int paint[10][10];
int main() {
  while (cin >> N >> r, N || r) {
    es.clear();
    for (int i = 0; i < N; i++) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      x1[i]++;
      y1[i]++;
      x2[i]++;
      y2[i]++;
    }

    for (int i = 0; i < N; i++) {
      es.push_back(event(y1[i], i, 1));
      es.push_back(event(y2[i], i, 0));
    }

    sort(es.begin(), es.end());

    /*
    memset(paint,0,sizeof(memo));
    for(int k=0;k<N;k++){
        for(int i=y1[k];i<y2[k];i++){
            for(int j=x1[k];j<x2[k];j++){
                paint[i][j]=min(1,paint[i][j]+1);
            }
        }
    }
    for(int i=1;i<15;i++){
        for(int j=1;j<15;j++){
            cout<<paint[i][j]<<" ";
        }cout<<endl;
    }
    */
    solve1();
    if (r == 2)
      solve2();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define F first
#define S second

short fld[10002][10002];
short dx[] = {-1, 0, 1, 0};
short dy[] = {0, -1, 0, 1};

int main() {
  int n, r;
  while (scanf("%d%d", &n, &r), n || r) {
    memset(fld, 0, sizeof(fld));

    REP(i, n) {
      int x1, y1, x2, y2;
      scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      x1++;
      y1++;
      x2++;
      y2++;
      fld[y1][x1]++;
      fld[y1][x2]--;
      fld[y2][x1]--;
      fld[y2][x2]++;
    }
    FOR(i, 1, 10001) {
      FOR(j, 1, 10001) {
        fld[i][j] += fld[i][j - 1] + fld[i - 1][j] - fld[i - 1][j - 1];
      }
    }
    LL S = 0, L = 0;
    FOR(i, 1, 10002) {
      FOR(j, 1, 10002) {
        if (fld[i][j])
          S++;
        else
          continue;
        REP(k, 4) {
          int x = j + dx[k], y = i + dy[k];
          if (x < 1 || x > 10001 || y < 1 || y > 10001)
            L++;
          else if (fld[y][x] == 0)
            L++;
        }
      }
    }

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