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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int compress(int *low, int *high, int n, int upper) {
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    v.push_back(low[i]);
    v.push_back(high[i]);
  }
  v.push_back(0);
  v.push_back(upper);

  sort(all(v));
  v.erase(unique(all(v)), v.end());

  for (int i = 0; i < n; ++i) {
    low[i] = lower_bound(all(v), low[i]) - v.begin();
    high[i] = lower_bound(all(v), high[i]) - v.begin();
  }

  return v.size() - 1;
}
int main() {
  int w, h, n;
  while (scanf("%d%d", &w, &h), w | h) {
    scanf("%d", &n);
    int x1[1024], x2[1024], y1[1024], y2[1024];
    for (int i = 0; i < n; ++i)
      scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);

    w = compress(x1, x2, n, w);
    h = compress(y1, y2, n, h);

    static bool painted[2048][2048];
    memset(painted, 0, sizeof(painted));
    for (int i = 0; i < n; ++i)
      for (int y = y1[i]; y < y2[i]; ++y)
        for (int x = x1[i]; x < x2[i]; ++x)
          painted[y][x] = true;

    // for (int y = 0; y < h; ++y)
    //{
    //	for (int x = 0; x < w; ++x)
    //		putchar(painted[y][x] ? '*' : ' ');
    //	puts("|");
    // }

    int res = 0;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (!painted[y][x]) {
          ++res;

          static const int dx[] = {0, 1, 0, -1};
          static const int dy[] = {1, 0, -1, 0};
          queue<pint> q;
          q.push(pint(x, y));
          painted[y][x] = true;
          while (!q.empty()) {
            pint v = q.front();
            q.pop();

            for (int i = 0; i < 4; ++i) {
              int tx = v.first + dx[i], ty = v.second + dy[i];
              if (0 <= tx && tx < w && 0 <= ty && ty < h && !painted[ty][tx]) {
                q.push(pint(tx, ty));
                painted[ty][tx] = true;
              }
            }
          }
        }
      }
    }
    printf("%d\\n", res);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int compress(int *low, int *high, int n, int upper) {
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    v.push_back(low[i]);
    v.push_back(high[i]);
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());

  for (int i = 0; i < n; ++i) {
    low[i] = lower_bound(all(v), low[i]) - v.begin();
    high[i] = lower_bound(all(v), high[i]) - v.begin();
  }

  return v.size() - 1;
}
int main() {
  int w, h, n;
  while (scanf("%d%d", &w, &h), w | h) {
    scanf("%d", &n);
    int x1[1024], x2[1024], y1[1024], y2[1024];
    for (int i = 0; i < n; ++i)
      scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);

    w = compress(x1, x2, n, w);
    h = compress(y1, y2, n, h);

    static bool painted[3 * 1024][3 * 1024];
    memset(painted, 0, sizeof(painted));
    for (int i = 0; i < n; ++i)
      for (int y = y1[i]; y < y2[i]; ++y)
        for (int x = x1[i]; x < x2[i]; ++x)
          painted[y][x] = true;

    // for (int y = 0; y < h; ++y)
    //{
    //	for (int x = 0; x < w; ++x)
    //		putchar(painted[y][x] ? '*' : ' ');
    //	puts("");
    // }

    int res = 0;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (!painted[y][x]) {
          ++res;

          static const int dx[] = {0, 1, 0, -1};
          static const int dy[] = {1, 0, -1, 0};
          queue<int> xq, yq;
          xq.push(x);
          yq.push(y);
          while (!xq.empty()) {
            int cx = xq.front(), cy = yq.front();
            xq.pop();
            yq.pop();

            painted[cy][cx] = true;

            for (int i = 0; i < 4; ++i) {
              int tx = cx + dx[i], ty = cy + dy[i];
              if (0 <= tx && tx < w && 0 <= ty && ty < h && !painted[ty][tx]) {
                xq.push(tx);
                yq.push(ty);
              }
            }
          }
        }
      }
    }
    printf("%d\\n", res);
  }
}