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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;
int X1[1005], X2[1005], Y1[1005], Y2[1005], w, h, n, dx[] = {-1, 0, 1, 0},
                                                     dy[] = {0, 1, 0, -1};
bool field[1001 * 6][1001 * 6];
vector<int> tv;
int compress(int *p1, int *p2, int w) {
  tv.clear();
  for (int i = 0; i < n; ++i) {
    for (int j = -1; j <= 1; ++j) {
      int tp1 = p1[i] + j, tp2 = p2[i] + j;
      if (0 <= tp1 && tp1 <= w)
        tv.PB(tp1);
      if (0 <= tp2 && tp2 <= w)
        tv.PB(tp2);
    }
  }
  sort(tv.begin(), tv.end());
  tv.erase(unique(tv.begin(), tv.end()), tv.end());
  for (int i = 0; i < n; ++i) {
    p1[i] = find(tv.begin(), tv.end(), p1[i]) - tv.begin();
    p2[i] = find(tv.begin(), tv.end(), p2[i]) - tv.begin();
  }
  return tv.size() - 1;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> w >> h, w) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
    }
    w = compress(X1, X2, w);
    h = compress(Y1, Y2, h);
    memset(field, 0, sizeof(field));
    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) {
          field[x][y] = true;
        }
      }
    }
    int ans = 0;
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        if (field[x][y])
          continue;
        ans++;
        queue<pii> q;
        q.push(pii(x, y));
        while (q.size()) {
          pii p = q.front();
          q.pop();
          int tx = p.first, ty = p.second;
          if (field[tx][ty])
            continue;
          field[tx][ty] = true;
          for (int i = 0; i < 4; ++i) {
            int nx = tx + dx[i], ny = ty + dy[i];
            if (nx < 0 || w <= nx || ny < 0 || h <= ny)
              continue;
            q.push(pii(nx, ny));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int solve();
int compress(int *m1, int *m2, int w);

int W, H, N;
int x1[1010], x2[1010], y1[1010], y2[1010];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
bool field[6010][6010] = {0};

int main() {
  while (scanf("%d%d", &W, &H), H + W) {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
      x2[i] = x2[i] - 1;
      y2[i] = y2[i] - 1;
    }
    printf("%d\\n", solve());
  }
  return 0;
}

int solve() {
  W = compress(x1, x2, W);
  H = compress(y1, y2, H);
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      field[i][j] = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int x = x1[i]; x <= x2[i]; x++) {
      for (int y = y1[i]; y <= y2[i]; y++) {
        field[x][y] = 1;
      }
    }
  }
  /*for(int i = 0; i < W; i++)
  {
      for(int j = 0; j < H; j++)
      {
          printf("%d ",field[i][j]);
      }
      printf("\\n");
  }
  for(int i = 0 ; i < N; i++)
  {
      printf("%d %d %d %d\\n",x1[i],x2[i],y1[i],y2[i]);
  }*/
  int ans = 0;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      if (field[i][j] == 0) {
        ans++;
        queue<pii> que;
        que.push(make_pair(i, j));
        while (que.size()) {
          int tx = que.front().first, ty = que.front().second;
          que.pop();
          field[tx][ty] = 1;
          for (int i = 0; i < 4; i++) {
            if (tx + dx[i] >= W || tx + dx[i] < 0 || ty + dy[i] >= H ||
                ty + dy[i] < 0)
              continue;
            if (field[tx + dx[i]][ty + dy[i]] == 0) {
              que.push(make_pair(tx + dx[i], ty + dy[i]));
            }
          }
        }
      }
    }
  }
  // printf("%d %d\\n",W,H);
  return ans;
}

int compress(int *m1, int *m2, int w) {
  vector<int> vec;
  for (int i = 0; i < N; i++) {
    for (int j = -1; j < 2; j++) {
      if (m1[i] + j >= 0 && m1[i] + j < w)
        vec.push_back(m1[i] + j);
      if (m2[i] + j >= 0 && m2[i] + j < w)
        vec.push_back(m2[i] + j);
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  // for(int i = 0; i < vec.size(); i++)printf("%d ",vec[i]);printf("\\n");
  for (int i = 0; i < N; i++) {
    m1[i] = find(vec.begin(), vec.end(), m1[i]) - vec.begin();
    m2[i] = find(vec.begin(), vec.end(), m2[i]) - vec.begin();
  }
  int res = vec.size();
  vec.clear();
  return res;
}