Problem p00454 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 10000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define all(n) n.begin(), n.end()

using namespace std;

typedef long long int ll;
typedef pair<int, int> PII;

const int MAX_N = 1005;

int w, h, n;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
bool fld[6 * MAX_N][6 * MAX_N];

int compress(int *x1, int *x2, int W) {
  vector<int> xs;

  REP(i, n) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (0 <= tx1 && tx1 <= W * 2)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 <= W * 2)
        xs.push_back(tx2);
    }
  }

  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  REP(i, n) {
    x1[i] = find(all(xs), x1[i]) - xs.begin();
    x2[i] = find(all(xs), x2[i]) - xs.begin();
  }
  return xs.size();
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

int main(void) {
  while (cin >> w >> h && w && h) {
    cin >> n;
    REP(i, n) {
      cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
      X1[i] = X1[i] * 2;
      X2[i] = X2[i] * 2;
      Y1[i] = Y1[i] * 2;
      Y2[i] = Y2[i] * 2;
    }

    memset(fld, 0, sizeof(fld));

    w = compress(X1, X2, w);
    h = compress(Y1, Y2, h);

    REP(i, n) {
      for (int y = Y1[i]; y <= Y2[i]; y++) {
        for (int x = X1[i]; x <= X2[i]; x++) {
          fld[y][x] = true;
        }
      }
    }

    int res = 0;
    REP(y, h) REP(x, w) {
      if (fld[y][x])
        continue;
      res++;

      queue<PII> que;
      que.push(PII(y, x));
      while (!que.empty()) {
        PII p = que.front();
        que.pop();
        REP(i, 4) {
          int tx = p.second + dx[i], ty = p.first + dy[i];
          if (tx < 0 || w <= tx || ty < 0 || h <= ty || fld[ty][tx])
            continue;

          fld[ty][tx] = true;
          que.push(PII(ty, tx));
        }
      }
    }

    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 10000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define all(n) n.begin(), n.end()

using namespace std;

typedef long long int ll;
typedef pair<int, int> PII;

const int MAX_N = 1000;

int w, h, n;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
bool fld[6 * MAX_N][6 * MAX_N];

int compress(int *x1, int *x2, int W) {
  vector<int> xs;

  REP(i, n) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d, tx2 = x2[i] + d;
      if (0 <= tx1 && tx1 <= W * 2)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 <= W * 2)
        xs.push_back(tx2);
    }
  }

  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  REP(i, n) {
    x1[i] = find(all(xs), x1[i]) - xs.begin();
    x2[i] = find(all(xs), x2[i]) - xs.begin();
  }
  return xs.size();
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void dfs(int x, int y) {
  if (fld[x][y])
    return;

  fld[x][y] = true;

  REP(i, 4) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (0 <= tx && tx < w && 0 <= ty && ty < h && !fld[tx][ty])
      dfs(tx, ty);
  }
}

int main(void) {
  while (cin >> w >> h && w && h) {
    cin >> n;
    REP(i, n) {
      cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
      X1[i] = X1[i] * 2;
      X2[i] = X2[i] * 2;
      Y1[i] = Y1[i] * 2;
      Y2[i] = Y2[i] * 2;
    }

    REP(i, MAX_N * 6) { fill(fld[i], fld[i] + (MAX_N * 6), false); }

    w = compress(X1, X2, w);
    h = compress(Y1, Y2, h);

    REP(k, n) {
      for (int i = X1[k]; i <= X2[k]; i++) {
        for (int j = Y1[k]; j <= Y2[k]; j++) {
          fld[i][j] = true;
        }
      }
    }

    int res = 0;
    REP(i, w) {
      REP(j, h) {
        if (fld[i][j])
          continue;

        res++;
        dfs(i, j);
      }
    }

    cout << res << endl;
  }
}