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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int W, H;
int N;
std::vector<int> X1, X2, Y1, Y2;
bool board[6005][6005];

int compress(std::vector<int> &v1, std::vector<int> &v2, int w) {
  std::vector<int> v;

  REP(i, N) {
    int s = v1[i], t = v2[i];
    FOR(j, -1, 2) {
      int ns = s + j, nt = t + j;
      if (0 <= ns && ns < w) {
        v.push_back(ns);
      }
      if (0 <= nt && nt < w) {
        v.push_back(nt);
      }
    }
  }

  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());

  REP(i, N) {
    v1[i] = std::find(v.begin(), v.end(), v1[i]) - v.begin();
    v2[i] = std::find(v.begin(), v.end(), v2[i]) - v.begin();
  }

  return v.size();
}

int main() {
  while (std::cin >> W >> H, W) {
    X1.clear();
    Y1.clear();
    X2.clear();
    Y2.clear();

    std::cin >> N;

    REP(i, N) {
      int x1, y1, x2, y2;
      std::cin >> x1 >> y1 >> x2 >> y2;

      X1.push_back(x1);
      Y1.push_back(y1);
      X2.push_back(x2);
      Y2.push_back(y2);
    }

    int wx = compress(X1, X2, W), wy = compress(Y1, Y2, H);

    REP(i, wx) {
      REP(j, wy) { board[i][j] = 0; }
    }

    REP(i, N) {
      int x1 = X1[i], y1 = Y1[i], x2 = X2[i], y2 = Y2[i];
      // std::cout << x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;

      FOR(j, x1, x2) {
        FOR(k, y1, y2) { board[j][k] = 1; }
      }
    }

    // REP(i, wy){
    //     REP(j, wx){
    //         printf("%d, ", board[j][i]);
    //     }
    //     puts("");
    // }

    int res = 0;
    REP(i, wy) {
      REP(j, wx) {
        if (!board[j][i]) {
          res++;

          std::queue<P> q;
          q.push(mp(j, i));
          board[j][i] = 1;

          while (!q.empty()) {
            P p = q.front();
            q.pop();

            REP(k, 4) {
              int nx = p.first + dx[k], ny = p.second + dy[k];
              if (0 <= nx && nx < wx && 0 <= ny && ny < wy && !board[nx][ny]) {
                q.push(mp(nx, ny));
                board[nx][ny] = 1;
              }
            }
          }
        }
      }
    }

    std::cout << res << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

int W, H;
int N;
std::vector<int> X1, X2, Y1, Y2;
bool board[6005][6005];

int compress(std::vector<int> &v1, std::vector<int> &v2, int w) {
  std::vector<int> v;

  REP(i, N) {
    int s = v1[i], t = v2[i];
    FOR(j, -1, 2) {
      int ns = s + j, nt = t + j;
      if (0 <= ns && ns < w) {
        v.push_back(ns);
      }
      if (0 <= nt && nt < w) {
        v.push_back(nt);
      }
    }
  }

  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());

  REP(i, N) {
    v1[i] = std::find(v.begin(), v.end(), v1[i]) - v.begin();
    v2[i] = std::find(v.begin(), v.end(), v2[i]) - v.begin();
  }

  return v.size();
}

int main() {
  while (std::cin >> W >> H, W) {
    X1.clear();
    Y1.clear();
    X2.clear();
    Y2.clear();

    std::cin >> N;

    REP(i, N) {
      int x1, y1, x2, y2;
      std::cin >> x1 >> y1 >> x2 >> y2;

      X1.push_back(x1);
      Y1.push_back(y1);
      X2.push_back(x2);
      Y2.push_back(y2);
    }

    int wx = compress(X1, X2, W), wy = compress(Y1, Y2, H);

    REP(i, wx) {
      REP(j, wy) { board[i][j] = 0; }
    }

    REP(i, N) {
      int x1 = X1[i], y1 = Y1[i], x2 = X2[i], y2 = Y2[i];
      // std::cout << x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;

      FOR(j, x1, x2) {
        FOR(k, y1, y2) { board[j][k] = 1; }
      }
    }

    // REP(i, wy){
    //     REP(j, wx){
    //         printf("%d, ", board[j][i]);
    //     }
    //     puts("");
    // }

    int res = 0;
    REP(i, wy) {
      REP(j, wx) {
        if (!board[j][i]) {
          res++;

          std::queue<P> q;
          q.push(mp(j, i));

          while (!q.empty()) {
            P p = q.front();
            q.pop();
            board[p.first][p.second] = 1;

            REP(k, 4) {
              int nx = p.first + dx[k], ny = p.second + dy[k];
              if (0 <= nx && nx < wx && 0 <= ny && ny < wy && !board[nx][ny]) {
                q.push(mp(nx, ny));
              }
            }
          }
        }
      }
    }

    std::cout << res << std::endl;
  }
}