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

#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int x1[1000];
int y1[1000];
int x2[1000];
int y2[1000];
vector<int> xcmp;
vector<int> ycmp;
bool used[3002][3002];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs(int x, int y) {
  used[x][y] = true;
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx >= 0 && nx < xcmp.size() && ny >= 0 && ny < ycmp.size() &&
          !used[nx][ny]) {
        used[nx][ny] = true;
        que.push(P(nx, ny));
      }
    }
  }
}
int main() {
  int n;
  int i, j, k;
  int w, h;
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    scanf("%d", &n);
    xcmp.clear();
    ycmp.clear();
    xcmp.push_back(0);
    xcmp.push_back(w - 1);
    ycmp.push_back(0);
    ycmp.push_back(h - 1);
    for (i = 0; i < n; i++) {
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
      xcmp.push_back(x1[i]);
      xcmp.push_back(x2[i] - 1);
      if (x2[i] < w)
        xcmp.push_back(x2[i]);
      ycmp.push_back(y1[i]);
      ycmp.push_back(y2[i] - 1);
      if (y2[i] < h)
        ycmp.push_back(y2[i]);
    }
    sort(xcmp.begin(), xcmp.end());
    sort(ycmp.begin(), ycmp.end());
    xcmp.erase(unique(xcmp.begin(), xcmp.end()), xcmp.end());
    ycmp.erase(unique(ycmp.begin(), ycmp.end()), ycmp.end());
    for (i = 0; i < xcmp.size(); i++)
      for (j = 0; j < ycmp.size(); j++)
        used[i][j] = false;
    for (i = 0; i < n; i++) {
      x1[i] = find(xcmp.begin(), xcmp.end(), x1[i]) - xcmp.begin();
      x2[i] = find(xcmp.begin(), xcmp.end(), x2[i]) - xcmp.begin();
      y1[i] = find(ycmp.begin(), ycmp.end(), y1[i]) - ycmp.begin();
      y2[i] = find(ycmp.begin(), ycmp.end(), y2[i]) - ycmp.begin();
      for (j = x1[i]; j < x2[i]; j++) {
        for (k = y1[i]; k < y2[i]; k++) {
          used[j][k] = true;
        }
      }
    }
    int ans = 0;
    for (i = 0; i < xcmp.size(); i++) {
      for (j = 0; j < ycmp.size(); j++)
        if (!used[i][j]) {
          ans++;
          bfs(i, j);
        }
    }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
int x1[1000];
int x2[1000];
int y1[1000];
int y2[1000];
vector<int> nx;
vector<int> ny;
set<int> ex;
set<int> ey;
bool ib[4000][4000];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
queue<P> que;
void bfs(int a, int b) {
  int i;
  que.push(P(a, b));
  while (!que.empty()) {
    int x, y;
    x = que.front().first;
    y = que.front().second;
    que.pop();
    ib[x][y] = true;
    for (i = 0; i < 4; i++) {
      if (x + dx[i] >= 0 && x + dx[i] < nx.size() && y + dy[i] >= 0 &&
          y + dy[i] < ny.size()) {
        if (!ib[x + dx[i]][y + dy[i]])
          que.push(P(x + dx[i], y + dy[i]));
      }
    }
  }
}

int main() {
  int w, h;
  int n;
  int i, j, k;
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    scanf("%d", &n);
    ex.clear();
    ey.clear();
    nx.clear();
    ny.clear();
    for (i = 0; i < n; i++) {
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x2[i]--;
      y2[i]--;
      ex.insert(x1[i]);
      ex.insert(x2[i]);
      ey.insert(y1[i]);
      ey.insert(y2[i]);
    }
    for (i = 0; i < w; i++)
      if (ex.find(i) != ex.end() || ex.find(i + 1) != ex.end())
        nx.push_back(i);
    for (i = 0; i < h; i++)
      if (ey.find(i) != ey.end() || ey.find(i + 1) != ey.end())
        ny.push_back(i);
    for (i = 0; i < nx.size(); i++)
      for (j = 0; j < ny.size(); j++)
        ib[i][j] = false;
    for (i = 0; i < n; i++) {
      int a, b, c, d;
      a = lower_bound(nx.begin(), nx.end(), x1[i]) - nx.begin();
      b = lower_bound(ny.begin(), ny.end(), y1[i]) - ny.begin();
      c = lower_bound(nx.begin(), nx.end(), x2[i]) - nx.begin();
      d = lower_bound(ny.begin(), ny.end(), y2[i]) - ny.begin();
      for (j = a; j <= c; j++)
        for (k = b; k <= d; k++)
          ib[j][k] = true;
    }
    int ans = 0;
    for (i = 0; i < nx.size(); i++) {
      for (j = 0; j < ny.size(); j++) {
        if (!ib[i][j]) {
          ans++;
          bfs(i, j);
        }
      }
    }
    printf("%d\\n", ans);
  }
}