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

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

using namespace std;
typedef pair<int, int> P;

int h, w, n;
int x1[1005], x2[1005], y1[1005], y2[1005];
vector<int> X, Y;
bool map[2005][2005];
int xsize, ysize;

int compress(int val, vector<int> *vec) {
  return lower_bound(vec->begin(), vec->end(), val) - vec->begin();
}

void bfs(int x, int y) {
  int vec_x[] = {-1, 1, 0, 0}, vec_y[] = {0, 0, -1, 1};

  queue<P> Q;
  Q.push(make_pair(x, y));
  map[x][y] = true;

  int cx, cy, nx, ny;
  while ((int)Q.size() > 0) {
    cx = Q.front().first;
    cy = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      nx = cx + vec_x[i], ny = cy + vec_y[i];
      if (nx < 0 || nx >= xsize || ny < 0 || ny >= ysize)
        continue;
      if (!map[nx][ny]) {
        Q.push(make_pair(nx, ny));
        map[nx][ny] = true;
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> n;
    X.clear(), Y.clear();

    for (int i = 0; i < n; i++) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      X.push_back(x1[i]);
      if (x2[i] < w)
        X.push_back(x2[i]);
      Y.push_back(y1[i]);
      if (y2[i] < h)
        Y.push_back(y2[i]);
    }
    X.push_back(0), Y.push_back(0);

    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());

    xsize = (int)X.size(), ysize = (int)Y.size();
    for (int x = 0; x < xsize; x++) {
      for (int y = 0; y < ysize; y++) {
        map[x][y] = false;
      }
    }

    for (int i = 0; i < n; i++) {
      int sx = compress(x1[i], &X), sy = compress(y1[i], &Y);
      int tx = compress(x2[i], &X), ty = compress(y2[i], &Y);
      for (int x = sx; x < tx; x++) {
        for (int y = sy; y < ty; y++) {
          map[x][y] = true;
        }
      }
    }

    int ans = 0;
    for (int x = 0; x < xsize; x++) {
      for (int y = 0; y < ysize; y++) {
        if (!map[x][y]) {
          bfs(x, y);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

int h, w, n;
int x1[1005], x2[1005], y1[1005], y2[1005];
vector<int> X, Y;
bool map[2005][2005];
int xsize, ysize;

int compress(int val, vector<int> *vec) {
  return lower_bound(vec->begin(), vec->end(), val) - vec->begin();
}

void bfs(int x, int y) {
  int vec_x[] = {-1, 1, 0, 0}, vec_y[] = {0, 0, -1, 1};

  queue<int> Q;
  Q.push(x * 10000 + y);

  int cx, cy, nx, ny;
  while ((int)Q.size()) {
    cx = Q.front() / 10000;
    cy = Q.front() % 10000;
    Q.pop();
    map[cx][cy] = true;
    for (int i = 0; i < 4; i++) {
      nx = cx + vec_x[i], ny = cy + vec_y[i];
      if (nx < 0 || nx >= xsize || ny < 0 || ny >= ysize)
        continue;
      if (!map[nx][ny])
        Q.push(nx * 10000 + ny);
    }
  }
}

int main(void) {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> n;
    X.clear(), Y.clear();

    for (int i = 0; i < n; i++) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      X.push_back(x1[i]);
      if (x2[i] < w)
        X.push_back(x2[i]);
      Y.push_back(y1[i]);
      if (y2[i] < h)
        Y.push_back(y2[i]);
    }
    X.push_back(0), Y.push_back(0);

    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());

    xsize = (int)X.size(), ysize = (int)Y.size();
    for (int x = 0; x < xsize; x++) {
      for (int y = 0; y < ysize; y++) {
        map[x][y] = false;
      }
    }

    for (int i = 0; i < n; i++) {
      int sx = compress(x1[i], &X), sy = compress(y1[i], &Y);
      int tx = compress(x2[i], &X), ty = compress(y2[i], &Y);
      for (int x = sx; x < tx; x++) {
        for (int y = sy; y < ty; y++) {
          map[x][y] = true;
        }
      }
    }

    int ans = 0;
    for (int x = 0; x < xsize; x++) {
      for (int y = 0; y < ysize; y++) {
        if (!map[x][y]) {
          bfs(x, y);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}