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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX_N 1010

using namespace std;

static int X1[1010], Y1[1010], X2[1010], Y2[1010], fld[MAX_N * 2][MAX_N * 2],
    dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};

static int compress(int *const, int *const, const int, const int);
static int bfs(const int, const int);
void imos(const int, const int, const int);

int main(void) {
  int W, H, N;

  // freopen("D:\\\\input.txt", "r", stdin);
  while (1) {
    scanf("%d%d", &W, &H);
    if (W == 0 && H == 0)
      break;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
    }
    W = compress(X1, X2, W, N);
    H = compress(Y1, Y2, H, N);

    imos(W, H, N);
    cout << bfs(W, H) << endl;
  }
  return EXIT_SUCCESS;
}

static int compress(int *const s1, int *const s2, const int W, const int N) {
  //???????????£???
  vector<int> xs;

  for (int i = 0; i < N; i++) {
    if (0 < s1[i] && s1[i] < W)
      xs.push_back(s1[i]);
    if (0 < s2[i] && s2[i] < W)
      xs.push_back(s2[i]);
  }
  xs.push_back(0);
  xs.push_back(W); //?????????????????¶
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());

  for (int i = 0; i < N; i++) {
    s1[i] = find(xs.begin(), xs.end(), s1[i]) - xs.begin();
    s2[i] = find(xs.begin(), xs.end(), s2[i]) - xs.begin();
  }
  return xs.size() -
         1; //??¨????????????????????????????????¶??????size??????2?????????1??????????\\????????????£?????°?????????
}

static int bfs(const int W, const int H) {
  int ans = 0;
  for (int i = 0; i < W; i++)
    for (int j = 0; j < H; j++) {
      if (fld[i][j])
        continue; //????´¢?????????????????????
      ans++;

      queue<pair<int, int>> que;
      que.push(make_pair(i, j));
      while (!que.empty()) {
        int tx = que.front().first, ty = que.front().second;
        que.pop();
        for (int i = 0; i < 4; i++) {
          int ttx = tx + dx[i], tty = ty + dy[i];
          if (0 <= ttx && ttx <= W && 0 <= tty && tty <= H && !fld[ttx][tty]) {
            que.push(make_pair(ttx, tty));
            fld[ttx][tty] = 1;
          }
        }
      }
    }
  return ans;
}

void imos(const int W, const int H, const int N) {
  // imos??????????????´
  memset(fld, 0, sizeof(fld));

  for (int i = 0; i < N; i++) //????????±??????
  {
    fld[X1[i]][Y1[i]]++;
    fld[X1[i]][Y2[i]]--;
    fld[X2[i]][Y1[i]]--;
    fld[X2[i]][Y2[i]]++;
  }
  for (int i = 0; i < W; i++) //?´?????¨????
    for (int j = 1; j < H; j++)
      fld[i][j] += fld[i][j - 1];

  for (int j = 0; j < H; j++) //?´??????????
    for (int i = 1; i < W; i++)
      fld[i][j] += fld[i - 1][j];
  //?????¶??¨????°±????????????????????????
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int w, h, n, x1[1005], x2[1005], y1[1005], y2[1005],
    fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int map[6005][6005];
struct node {
  int x, y;
  node(int xx, int yy) { x = xx, y = yy; }
};
queue<node> q;
void imos() {
  memset(map, 0, sizeof(map));
  for (int i = 1; i <= n; i++) {
    map[x1[i]][y1[i]]++;
    map[x1[i]][y2[i]]--;
    map[x2[i]][y1[i]]--;
    map[x2[i]][y2[i]]++;
  }
  for (int i = 0; i < w; i++) {
    for (int j = 1; j < h; j++) {
      map[i][j] = map[i][j] + map[i][j - 1];
      // printf("%2d",map[i][j]);
    }
    // printf("\\n");
  }
  for (int j = 0; j < h; j++) {
    // printf("%2d",map[0][j]);
    for (int i = 1; i < w; i++) {
      map[i][j] = map[i][j] + map[i - 1][j];
      // printf("%2d",map[i][j]);
    }
    // printf("\\n");
  }
}
int compress(int *x, int *xx, int w) {
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    for (int j = -1; j <= 1; j++) {
      int jx1 = x[i] + j, jx2 = xx[i] + j;
      if (jx1 >= 0 && jx1 < w)
        v.push_back(jx1);
      if (jx2 >= 0 && jx2 < w)
        v.push_back(jx2);
    }
  }
  /*
  for(int i=0;i<int(v.size());i++)
          printf("*%d",v[i]);
  printf("#######\\n");*/
  sort(v.begin(), v.end());
  /*for(int i=0;i<int(v.size());i++)
          printf("*%d",v[i]);
  printf("#######\\n");*/
  v.erase(unique(v.begin(), v.end()), v.end());
  /*for(int i=0;i<int(v.size());i++)
          printf("*%d",v[i]);
  printf("#######\\n");*/
  for (int i = 1; i <= n; i++) {
    x[i] = find(v.begin(), v.end(), x[i]) - v.begin();
    xx[i] = find(v.begin(), v.end(), xx[i]) - v.begin();
  }
  return v.size();
}
int main() {
  while (~scanf("%d %d", &w, &h) && w && h) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
    w = compress(x1, x2, w);
    h = compress(y1, y2, h);
    imos();
    int ans = 0;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        if (map[i][j] == 0) {
          ans++;
          while (!q.empty())
            q.pop();
          q.push(node(i, j));
          while (!q.empty()) {
            node k = q.front();
            q.pop();
            if (map[k.x][k.y] != 0)
              continue;
            map[k.x][k.y] = 1;
            for (int o = 0; o < 4; o++) {
              int xx = fx[o][0] + k.x, yy = fx[o][1] + k.y;
              if (xx >= 0 && yy >= 0 && xx < w && yy < h && map[xx][yy] == 0)
                q.push(node(xx, yy));
            }
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
}