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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int compress(vector<int> &x, const int w) {
  vector<int> xs;
  for (int i = 0; i < x.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int nx = x[i] + d;
      if (0 <= nx && nx < w)
        xs.push_back(nx);
    }
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < x.size(); i++) {
    x[i] = find(xs.begin(), xs.end(), x[i]) - xs.begin();
  }
  return xs.size();
}
int main() {
  int w, h, n;
  const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};
  vector<int> x, y;
  while (cin >> w >> h, w) {
    cin >> n;
    x.resize(2 * n), y.resize(2 * n);
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> x[n + i] >> y[n + i];
    }
    int neww, newh;
    neww = compress(x, w);
    newh = compress(y, h);
    bool mas[2000][2000] = {{}};
    for (int i = 0; i < n; i++) {
      for (int j = x[i]; j < x[n + i]; j++) {
        for (int k = y[i]; k < y[n + i]; k++) {
          mas[k][j] = true;
        }
      }
    }
    int ret = 0;
    queue<int> quex, quey;
    for (int i = 0; i < newh; i++) {
      for (int j = 0; j < neww; j++) {
        if (!mas[i][j]++) {
          ret++;
          quex.push(j), quey.push(i); // あえてBFS
          while (!quex.empty()) {
            int x = quex.front(), y = quey.front();
            quex.pop(), quey.pop();
            for (int i = 0; i < 4; i++) {
              int ny = y + dy[i], nx = x + dx[i];
              if (ny < 0 || nx < 0 || nx >= neww || ny >= newh)
                continue;
              if (!mas[ny][nx]) {
                quex.push(nx), quey.push(ny);
                mas[ny][nx] = true;
              }
            }
          }
        }
      }
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int compress(vector<int> &x, const int w) {
  vector<int> xs;
  for (int i = 0; i < x.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int nx = x[i] + d;
      if (0 <= nx && nx < w)
        xs.push_back(nx);
    }
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < x.size(); i++) {
    x[i] = find(xs.begin(), xs.end(), x[i]) - xs.begin();
  }
  return xs.size();
}
int main() {
  int w, h, n;
  const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};
  vector<int> x, y;
  while (cin >> w >> h, w) {
    cin >> n;
    x.resize(2 * n), y.resize(2 * n);
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> x[n + i] >> y[n + i];
    }
    int neww, newh;
    neww = compress(x, w);
    newh = compress(y, h);
    bool mas[2000][2000] = {{}};
    for (int i = 0; i < n; i++) {
      for (int j = x[i]; j < x[n + i]; j++) {
        for (int k = y[i]; k < y[n + i]; k++) {
          mas[k][j] = true;
        }
      }
    }
    int ret = 0;
    queue<int> quex, quey;
    for (int i = 0; i < newh; i++) {
      for (int j = 0; j < neww; j++) {
        if (!mas[i][j]) {
          ret++;
          quex.push(j), quey.push(i); // あえてBFS
          while (!quex.empty()) {
            int x = quex.front(), y = quey.front();
            quex.pop(), quey.pop();
            mas[y][x] = true;
            for (int i = 0; i < 4; i++) {
              int ny = y + dy[i], nx = x + dx[i];
              if (ny < 0 || nx < 0 || nx >= neww || ny >= newh)
                continue;
              if (!mas[ny][nx])
                quex.push(nx), quey.push(ny);
            }
          }
        }
      }
    }
    cout << ret << endl;
  }
}