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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int INF = (1 << 30) - 1;
const int dh[] = {0, 0, 1, -1};
const int dw[] = {1, -1, 0, 0};

int compress(vector<int> &x1, vector<int> &x2, int W) {
  vector<int> xs;
  for (int i = 0; i < x1.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d;
      int tx2 = x2[i] + d;
      if (0 <= tx1 && tx1 < W)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 < W)
        xs.push_back(tx2);
    }
  }

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

  for (int i = 0; i < x1.size(); i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void dfs(int h, int w, vector<vector<bool>> &field) {
  const int H = field.size();
  const int W = field[0].size();

  field[h][w] = true;
  for (int i = 0; i < 4; i++) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (0 <= nh && nh < H && 0 <= nw && nw < W && !field[nh][nw])
      dfs(nh, nw, field);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int W, H;
  while (cin >> W >> H, W | H) {
    int N;
    cin >> N;
    vector<int> x1(N);
    vector<int> y1(N);
    vector<int> x2(N);
    vector<int> y2(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);

    vector<vector<bool>> field(H, vector<bool>(W));
    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[y][x] = true;
        }
      }
    }
    int ans = 0;
    for (int h = 0; h < H; h++) {
      for (int w = 0; w < W; w++) {
        if (field[h][w])
          continue;
        ans++;
        queue<pair<int, int>> q;
        q.push(make_pair(h, w));

        while (!q.empty()) {
          auto p = q.front();
          q.pop();
          int th = p.first;
          int tw = p.second;
          for (int i = 0; i < 4; i++) {
            int nh = th + dh[i];
            int nw = tw + dw[i];
            if (0 <= nh && nh < H && 0 <= nw && nw < W && !field[nh][nw]) {
              q.push(make_pair(nh, nw));
              field[nh][nw] = true;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int INF = (1 << 30) - 1;
const int dh[] = {0, 0, 1, -1};
const int dw[] = {1, -1, 0, 0};

int compress(vector<int> &x1, vector<int> &x2, int W) {
  vector<int> xs;
  for (int i = 0; i < x1.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i] + d;
      int tx2 = x2[i] + d;
      if (0 <= tx1 && tx1 < W)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 < W)
        xs.push_back(tx2);
    }
  }

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

  for (int i = 0; i < x1.size(); i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void dfs(int h, int w, vector<vector<bool>> &field) {
  const int H = field.size();
  const int W = field[0].size();

  field[h][w] = true;
  for (int i = 0; i < 4; i++) {
    int nh = h + dh[i];
    int nw = w + dw[i];
    if (0 <= nh && nh < H && 0 <= nw && nw < W && !field[nh][nw])
      dfs(nh, nw, field);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int W, H;
  while (cin >> W >> H, W | H) {
    int N;
    cin >> N;
    vector<int> x1(N);
    vector<int> y1(N);
    vector<int> x2(N);
    vector<int> y2(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);

    vector<vector<bool>> field(H, vector<bool>(W));
    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[y][x] = true;
        }
      }
    }
    int ans = 0;
    for (int h = 0; h < H; h++) {
      for (int w = 0; w < W; w++) {
        if (field[h][w])
          continue;
        ans++;
        //                dfs(h, w, field);
        queue<pair<int, int>> q;
        q.push(make_pair(h, w));

        while (!q.empty()) {
          auto p = q.front();
          q.pop();
          int th = p.first;
          int tw = p.second;
          field[th][tw] = true;
          for (int i = 0; i < 4; i++) {
            int nh = th + dh[i];
            int nw = tw + dw[i];
            if (0 <= nh && nh < H && 0 <= nw && nw < W && !field[nh][nw])
              q.push(make_pair(nh, nw));
          }
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}