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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef vector<int> vi;

#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define all(c) c.begin(), c.end()

int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};

int w, h, n;
int x1[1000], x2[1000], y1[1000], y2[1000], X, Y;
bool v[2006][2006];
vi x, y;

int main() {
  while (cin >> w >> h, w) {
    cin >> n;
    x.clear();
    y.clear();

    rep(i, n) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      x.pb(x1[i]), y.pb(y1[i]);
      x.pb(x2[i]), y.pb(y2[i]);
    }

    x.pb(0), y.pb(0);
    x.pb(w), y.pb(h);
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());

    X = x.size(), Y = y.size();
    rep(i, n) {
      x1[i] = lower_bound(all(x), x1[i]) - x.begin();
      y1[i] = lower_bound(all(y), y1[i]) - y.begin();
      x2[i] = lower_bound(all(x), x2[i]) - x.begin();
      y2[i] = lower_bound(all(y), y2[i]) - y.begin();
    }

    rep(i, Y) rep(j, X) v[i][j] = i == Y - 1 || j == X - 1;
    rep(i, n) for (int a = x1[i]; a < x2[i]; a++) for (int b = y1[i]; b < y2[i];
                                                       b++) v[b][a] = 1;

    int ans = 0;
    rep(i, Y) rep(j, X) if (!v[i][j]) {
      ans++;
      v[i][j] = 1;
      queue<pair<int, int>> Q;
      Q.push(make_pair(i, j));
      while (!Q.empty()) {
        int a = Q.front().first, b = Q.front().second;
        Q.pop();
        rep(d, 4) {
          int ny = a + dy[d], nx = b + dx[d];
          if (ny < 0 || nx < 0 || ny >= Y - 1 || nx >= X - 1 || v[ny][nx])
            continue;
          v[ny][nx] = 1;
          Q.push(make_pair(ny, nx));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef vector<int> vi;

#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define all(c) c.begin(), c.end()

int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};

int w, h, n;
int x1[1000], x2[1000], y1[1000], y2[1000], X, Y;
bool v[2006][2006];
vi x, y;

int main() {
  while (cin >> w >> h, w) {
    cin >> n;
    x.clear();
    y.clear();

    rep(i, n) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      x.pb(x1[i]), y.pb(y1[i]);
      x.pb(x2[i]), y.pb(y2[i]);
    }

    x.pb(0), y.pb(0);
    x.pb(w), y.pb(h);
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());

    X = x.size(), Y = y.size();
    rep(i, n) {
      x1[i] = lower_bound(all(x), x1[i]) - x.begin();
      y1[i] = lower_bound(all(y), y1[i]) - y.begin();
      x2[i] = lower_bound(all(x), x2[i]) - x.begin();
      y2[i] = lower_bound(all(y), y2[i]) - y.begin();
    }

    rep(i, Y) rep(j, X) v[i][j] = i == Y - 1 || j == X - 1;
    rep(i, n) for (int a = x1[i]; a < x2[i]; a++) for (int b = y1[i]; b < y2[i];
                                                       b++) v[b][a] = 1;

    int ans = 0;
    rep(i, Y) rep(j, X) if (!v[i][j]) {
      ans++;
      queue<pair<int, int>> Q;
      Q.push(make_pair(i, j));
      while (!Q.empty()) {
        int a = Q.front().first, b = Q.front().second;
        Q.pop();
        v[a][b] = 1;
        rep(d, 4) {
          int ny = a + dy[d], nx = b + dx[d];
          if (ny < 0 || nx < 0 || ny >= Y - 1 || nx >= X - 1 || v[ny][nx])
            continue;
          Q.push(make_pair(ny, nx));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}