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

/*
AOJ 0531 Paint Color

?¢???????????????£???????????????????????¨????????¢??????????????????????????\\??¶??????????????????????????????????¶?????????¨????¢?????´´?\\?????????????
?¢??????????????????????????¶??????????????¢????????????????????°±?????\\?¶????5?§??¢???????
????????????????¨???????????¶??????°???????????\\??°????????????????????????????¢?????????¨??¨???????????¶?????????????????????????°´???????????´??????

?????\\????¬¬????????°??????w???1 ??? w ??? 1000000???????¬¬????????°??????h???1
??? h ??? 1000000?????? ?¬¬????????????????????°???n???1 ??? n ???
1000????????\\???n????????????????????????????§??????? (x1 , y1
)??????????§??????? (x2 , y2 )???
??¨??????????????? x1 , y1 , x2 , y2 (0 ??? x1< x2 ??? w, 0 ??? y1 < y2 ??? h
????????´??°)
??????????????????????????????????§???? (0, 0) ??????????§????(w, h) ???
 ???????????????30%???????¶?w ??? 100, h ??? 100, n ??? 100???

?????????????????´??°?????£??¨?¶??????°??????
*/

#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <algorithm>
#include <complex>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) c.begin(), c.end()
#define CLR(c, n) memset(c, n, sizeof(c))
#define TR(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define CONTAIN(it, c) (c.find(it) != c.end())
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> void checkmin(T &a, T b) {
  if (b < a)
    a = b;
}
template <class T> void checkmax(T &a, T b) {
  if (b > a)
    a = b;
}
typedef long long LL;
const int INF = 0x3F3F3F3F;

const int N = 1024;
int w, h, n, cord_x1[N], cord_y1[N], cord_x2[N], cord_y2[N];
int x[N * 4], y[N * 4];
short cnt[N * 4][N * 4];

void fill(int x0, int y0) {
  cnt[x0][y0] = -1;
  queue<PII> q;
  q.push(PII(x0, y0));
  int dx[4] = {-1, 0, 1, 0};
  while (!q.empty()) {
    int x0 = q.front().first, y0 = q.front().second;
    q.pop();
    REP(dir, 4) {
      int x1 = x0 + dx[dir], y1 = y0 + dx[dir ^ 1];
      if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h && cnt[x1][y1] == 0) {
        cnt[x1][y1] = -1;
        q.push(PII(x1, y1));
      }
    }
  }
}

int main(int argc, char *argv[]) {
  while (scanf("%d %d", &w, &h) == 2 && w + h > 0) {
    scanf("%d", &n);
    REP(i, n) {
      scanf("%d %d %d %d", &cord_x1[i], &cord_y1[i], &cord_x2[i], &cord_y2[i]);
    }
    int nx = 0, ny = 0;
    REP(i, n) {
      x[nx++] = cord_x1[i];
      if (cord_x1[i])
        x[nx++] = cord_x1[i] - 1;
      x[nx++] = cord_x2[i];
      if (cord_x2[i])
        x[nx++] = cord_x2[i] - 1;
      y[ny++] = cord_y1[i];
      if (cord_y1[i])
        y[ny++] = cord_y1[i] - 1;
      y[ny++] = cord_y2[i];
      if (cord_y2[i])
        y[ny++] = cord_y2[i] - 1;
    }
    x[nx++] = w;
    y[ny++] = h;
    sort(x, x + nx);
    sort(y, y + ny);
    w = unique(x, x + nx) - x - 1;
    h = unique(y, y + ny) - y - 1;
    REP(i, n) {
      cord_x1[i] = lower_bound(x, x + w, cord_x1[i]) - x;
      cord_y1[i] = lower_bound(y, y + h, cord_y1[i]) - y;
      cord_x2[i] = lower_bound(x, x + w, cord_x2[i]) - x;
      cord_y2[i] = lower_bound(y, y + h, cord_y2[i]) - y;
    }
    CLR(cnt, 0);
    REP(i, n) {
      ++cnt[cord_x1[i]][cord_y1[i]];
      ++cnt[cord_x2[i]][cord_y2[i]];
      --cnt[cord_x1[i]][cord_y2[i]];
      --cnt[cord_x2[i]][cord_y1[i]];
    }
    FOR(i, 0, w) FOR(j, 1, h) cnt[i][j] += cnt[i][j - 1];
    FOR(i, 1, w) FOR(j, 0, h) cnt[i][j] += cnt[i - 1][j];
    int ans = 0;
    REP(i, w) REP(j, h) if (cnt[i][j] == 0) {
      ++ans;
      fill(i, j);
    }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

/*
AOJ 0531 Paint Color

?¢???????????????£???????????????????????¨????????¢??????????????????????????\\??¶??????????????????????????????????¶?????????¨????¢?????´´?\\?????????????
?¢??????????????????????????¶??????????????¢????????????????????°±?????\\?¶????5?§??¢???????
????????????????¨???????????¶??????°???????????\\??°????????????????????????????¢?????????¨??¨???????????¶?????????????????????????°´???????????´??????

?????\\????¬¬????????°??????w???1 ??? w ??? 1000000???????¬¬????????°??????h???1
??? h ??? 1000000?????? ?¬¬????????????????????°???n???1 ??? n ???
1000????????\\???n????????????????????????????§??????? (x1 , y1
)??????????§??????? (x2 , y2 )???
??¨??????????????? x1 , y1 , x2 , y2 (0 ??? x1< x2 ??? w, 0 ??? y1 < y2 ??? h
????????´??°)
??????????????????????????????????§???? (0, 0) ??????????§????(w, h) ???
 ???????????????30%???????¶?w ??? 100, h ??? 100, n ??? 100???

?????????????????´??°?????£??¨?¶??????°??????
*/

#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <algorithm>
#include <complex>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) c.begin(), c.end()
#define CLR(c, n) memset(c, n, sizeof(c))
#define TR(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define CONTAIN(it, c) (c.find(it) != c.end())
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> void checkmin(T &a, T b) {
  if (b < a)
    a = b;
}
template <class T> void checkmax(T &a, T b) {
  if (b > a)
    a = b;
}
typedef long long LL;
const int INF = 0x3F3F3F3F;

const int N = 1024;
int w, h, n, cord_x1[N], cord_y1[N], cord_x2[N], cord_y2[N];
int x[N * 4], y[N * 4];
int cnt[N * 4][N * 4];

void fill(int x0, int y0) {
  cnt[x0][y0] = -1;
  queue<PII> q;
  q.push(PII(x0, y0));
  int dx[4] = {-1, 0, 1, 0};
  while (!q.empty()) {
    int x0 = q.front().first, y0 = q.front().second;
    q.pop();
    REP(dir, 4) {
      int x1 = x0 + dx[dir], y1 = y0 + dx[dir ^ 1];
      if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h && cnt[x1][y1] == 0) {
        cnt[x1][y1] = -1;
        q.push(PII(x1, y1));
      }
    }
  }
}

int main(int argc, char *argv[]) {
  while (scanf("%d %d", &w, &h) == 2 && w + h > 0) {
    scanf("%d", &n);
    REP(i, n) {
      scanf("%d %d %d %d", &cord_x1[i], &cord_y1[i], &cord_x2[i], &cord_y2[i]);
    }
    int nx = 0, ny = 0;
    REP(i, n) {
      x[nx++] = cord_x1[i];
      if (cord_x1[i])
        x[nx++] = cord_x1[i] - 1;
      x[nx++] = cord_x2[i];
      if (cord_x2[i])
        x[nx++] = cord_x2[i] - 1;
      y[ny++] = cord_y1[i];
      if (cord_y1[i])
        y[ny++] = cord_y1[i] - 1;
      y[ny++] = cord_y2[i];
      if (cord_y2[i])
        y[ny++] = cord_y2[i] - 1;
    }
    x[nx++] = w;
    y[ny++] = h;
    sort(x, x + nx);
    sort(y, y + ny);
    w = unique(x, x + nx) - x - 1;
    h = unique(y, y + ny) - y - 1;
    REP(i, n) {
      cord_x1[i] = lower_bound(x, x + w, cord_x1[i]) - x;
      cord_y1[i] = lower_bound(y, y + h, cord_y1[i]) - y;
      cord_x2[i] = lower_bound(x, x + w, cord_x2[i]) - x;
      cord_y2[i] = lower_bound(y, y + h, cord_y2[i]) - y;
    }
    CLR(cnt, 0);
    REP(i, n) {
      ++cnt[cord_x1[i]][cord_y1[i]];
      ++cnt[cord_x2[i]][cord_y2[i]];
      --cnt[cord_x1[i]][cord_y2[i]];
      --cnt[cord_x2[i]][cord_y1[i]];
    }
    FOR(i, 0, w) FOR(j, 1, h) cnt[i][j] += cnt[i][j - 1];
    FOR(i, 1, w) FOR(j, 0, h) cnt[i][j] += cnt[i - 1][j];
    int ans = 0;
    REP(i, w) REP(j, h) if (cnt[i][j] == 0) {
      ++ans;
      fill(i, j);
    }
    printf("%d\\n", ans);
  }
}