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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
int w, h;

const int INF = 1 << 29;
typedef pair<int, int> P;

int dp[102][102];
int n;
int compress(vector<int> &vec1, vector<int> &vec2, int size) {
  vector<int> compressed;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int n1 = vec1[i] + j, n2 = vec2[i] + j;
      if (0 <= n1 && n1 < size)
        compressed.push_back(n1);
      if (0 <= n2 && n2 < size)
        compressed.push_back(n2);
    }
  }

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

  for (int i = 0; i < n; i++) {
    vec1[i] = find(compressed.begin(), compressed.end(), vec1[i]) -
              compressed.begin();
    vec2[i] = find(compressed.begin(), compressed.end(), vec2[i]) -
              compressed.begin();
  }

  return compressed.size();
}

int main(void) {
  while (cin >> w >> h && w && h) {
    vector<int> x1, x2, y1, y2;
    cin >> n;
    x1.resize(n);
    x2.resize(n);
    y1.resize(n);
    y2.resize(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>> fid;
    fid.resize(h);
    for (int i = 0; i < h; i++) {
      fid[i].resize(w);
    }

    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++) {
          fid[y][x] = true;
        }
      }
    }

    int ans = 0;
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, -1, 0, 1};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (fid[i][j])
          continue;
        ans++;

        queue<P> que;
        que.push(P(j, i));
        while (!que.empty()) {
          int sx = que.front().first, sy = que.front().second;
          que.pop();
          for (int i = 0; i < 4; i++) {
            int nx = sx + dx[i], ny = sy + dy[i];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h)
              continue;
            if (fid[ny][nx])
              continue;
            que.push(P(nx, ny));
            fid[ny][nx] = true;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
int w, h;

const int INF = 1 << 29;
typedef pair<int, int> P;

int dp[102][102];
int n;
int compress(vector<int> &vec1, vector<int> &vec2, int size) {
  vector<int> compressed;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      int n1 = vec1[i] + j, n2 = vec2[i] + j;
      if (0 <= n1 && n1 < size)
        compressed.push_back(n1);
      if (0 <= n2 && n2 < size)
        compressed.push_back(n2);
    }
  }

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

  for (int i = 0; i < size; i++) {
    vec1[i] = find(compressed.begin(), compressed.end(), vec1[i]) -
              compressed.begin();
    vec2[i] = find(compressed.begin(), compressed.end(), vec2[i]) -
              compressed.begin();
  }

  return compressed.size();
}

int main(void) {
  while (cin >> w >> h && w && h) {
    w++;
    h++;
    vector<int> x1, x2, y1, y2;
    cin >> n;
    x1.resize(n);
    x2.resize(n);
    y1.resize(n);
    y2.resize(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>> fid;
    fid.resize(h + 1);
    for (int i = 0; i < h; i++) {
      fid[h].resize(w);
    }

    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++) {
          fid[y][x] = true;
        }
      }
    }

    int ans;
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, -1, 0, 1};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (fid[i][j])
          continue;
        ans++;

        queue<P> que;
        que.push(P(j, i));
        while (!que.empty()) {
          int sx = que.front().first, sy = que.front().second;
          que.pop();
          for (int i = 0; i < 4; i++) {
            int nx = sx + dx[i], ny = sy + dy[i];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h)
              continue;
            if (fid[ny][nx])
              continue;
            que.push(P(nx, ny));
            fid[ny][nx] = true;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}