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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef pair<int, int> PII;
const int DX[] = {-1, 1, 0, 0};
const int DY[] = {0, 0, -1, 1};

#define dump(x)                                                                \\
  cerr << "  (L" << __LINE__ << ") " << #x << " = " << (x) << endl;
#define dumpv(x)                                                               \\
  cerr << "  (L" << __LINE__ << ") " << #x << " = ";                           \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << "," << d.second << ")";
}

int compress(VI &x1, VI &x2, int w) {
  VI 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 (tx1 >= 0 && tx1 < w)
        xs.push_back(tx1);
      if (tx2 >= 0 && 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] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void bfs(int x, int y, int w, int h, vector<vector<bool>> &used) {

  queue<PII> que;

  used[x][y] = true;
  que.push(PII(x, y));

  while (!que.empty()) {
    PII p = que.front();
    que.pop();
    for (int k = 0; k < 4; k++) {
      int nx = p.first + DX[k];
      int ny = p.second + DY[k];
      if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
        if (used[nx][ny] == false) {
          used[nx][ny] = true;
          que.push(PII(nx, ny));
        }
      }
    }
  }
}

void print(vector<vector<bool>> &used) {
  /*
  cerr << "=====================================" << endl;
  for (int i = 0; i < used.size(); i++) {
    for (int j = 0; j < used[0].size(); j++) cerr << (used[i][j] ? 'x' : '-');
    cerr << endl;
  }
  */
}

PII find_unused(vector<vector<bool>> &used) {
  for (int i = 0; i < used.size(); i++) {
    for (int j = 0; j < used[0].size(); j++) {
      if (used[i][j] == false) {
        return PII(i, j);
      }
    }
  }
  return PII(-1, -1);
}

int solve(int w, int h, VI &x1, VI &y1, VI &x2, VI &y2) {
  // compression
  w = compress(x1, x2, w);
  h = compress(y1, y2, h);

  vector<vector<bool>> used(w, vector<bool>(h, false));
  for (int k = 0; k < x1.size(); k++) {
    for (int x = x1[k]; x < x2[k]; x++) {
      for (int y = y1[k]; y < y2[k]; y++) {
        used[x][y] = true;
      }
    }
  }

  print(used);

  int count = 0;
  while (true) {
    PII unused = find_unused(used);
    if (unused.first != -1) {
      count++;
      bfs(unused.first, unused.second, w, h, used);
      print(used);
    } else {
      break;
    }
  }
  return count;
}

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

  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    int n;
    cin >> n;
    VI x1(n), y1(n), x2(n), y2(n);
    for (int i = 0; i < n; i++) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    }

    int ans = solve(w, h, x1, y1, x2, y2);
    cout << ans << endl;
  }
}

Error: 0
x--xx
xxxxx
x--xx
-----
-xxx-
xx--x
-xxx-
-----
x---x
xxxxx
x---x
-----


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef pair<int, int> PII;
const int DX[] = {-1, 1, 0, 0};
const int DY[] = {0, 0, -1, 1};

#define dump(x)                                                                \\
  cerr << "  (L" << __LINE__ << ") " << #x << " = " << (x) << endl;
#define dumpv(x)                                                               \\
  cerr << "  (L" << __LINE__ << ") " << #x << " = ";                           \\
  REP(q, (x).size()) cerr << (x)[q] << " ";                                    \\
  cerr << endl;
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << "," << d.second << ")";
}

int compress(VI &x1, VI &x2, int w) {
  VI xs;
  for (int i = 0; i < x1.size(); i++) {
    for (int d = -1; d <= 1; d++) {
      int tx1 = x1[i], tx2 = x2[i];
      if (tx1 >= 0 && tx1 < w)
        xs.push_back(tx1);
      if (tx2 >= 0 && 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] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }

  return xs.size();
}

void bfs(int x, int y, int w, int h, vector<vector<bool>> &used) {

  queue<PII> que;

  used[x][y] = true;
  que.push(PII(x, y));

  while (!que.empty()) {
    PII p = que.front();
    que.pop();
    for (int k = 0; k < 4; k++) {
      int nx = p.first + DX[k];
      int ny = p.second + DY[k];
      if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
        if (used[nx][ny] == false) {
          used[nx][ny] = true;
          que.push(PII(nx, ny));
        }
      }
    }
  }
}

int solve(int w, int h, VI &x1, VI &y1, VI &x2, VI &y2) {
  // compression
  w = compress(x1, x2, w);
  h = compress(y1, y2, h);

  vector<vector<bool>> used(w, vector<bool>(h, false));
  for (int k = 0; k < x1.size(); k++) {
    for (int x = x1[k]; x < x2[k]; x++) {
      for (int y = y1[k]; y < y2[k]; y++) {
        used[x][y] = true;
      }
    }
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++)
      cerr << (used[i][j] ? 'x' : '-');
    cerr << endl;
  }

  int count = 0;
  while (true) {
    int sx = -1, sy = -1;
    for (int x = 0; x < w; x++)
      for (int y = 0; y < h; y++) {
        if (used[x][y] == false) {
          sx = x;
          sy = y;
          break;
        }
      }
    if (sx != -1) {
      count++;
      bfs(sx, sy, w, h, used);
    } else {
      break;
    }
  }
  return count;
}

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

  int w, h;
  cin >> w >> h;
  int n;
  cin >> n;
  VI x1(n), y1(n), x2(n), y2(n);
  for (int i = 0; i < n; i++) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
  }

  int ans = solve(w, h, x1, y1, x2, y2);
  cout << ans << endl;
}