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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int board[4010][4010];

int main() {
  while (true) {
    int w, h, n;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> n;
    vector<pair<pair<int, int>, pair<int, int>>> v;
    vector<int> x, y;
    x.push_back(0);
    x.push_back(w);
    y.push_back(0);
    y.push_back(h);
    rep(i, n) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      v.push_back(make_pair(make_pair(x1, y1), make_pair(x2, y2)));
      x.push_back(x1 - 1);
      x.push_back(x1);
      x.push_back(x2 - 1);
      x.push_back(x2);
      y.push_back(y1 - 1);
      y.push_back(y1);
      y.push_back(y2 - 1);
      y.push_back(y2);
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    y.erase(unique(y.begin(), y.end()), y.end());
    memset(board, 0, sizeof(board));
    int w0 = lower_bound(x.begin(), x.end(), 0) - x.begin();
    int ww = lower_bound(x.begin(), x.end(), w) - x.begin();
    int h0 = lower_bound(y.begin(), y.end(), 0) - y.begin();
    int hh = lower_bound(y.begin(), y.end(), h) - y.begin();
    rep(i, n) {
      int x1 = lower_bound(x.begin(), x.end(), v[i].first.first) - x.begin();
      int y1 = lower_bound(y.begin(), y.end(), v[i].first.second) - y.begin();
      int x2 = lower_bound(x.begin(), x.end(), v[i].second.first) - x.begin();
      int y2 = lower_bound(y.begin(), y.end(), v[i].second.second) - y.begin();
      board[y1][x1]++;
      board[y2][x1]--;
      board[y1][x2]--;
      board[y2][x2]++;
    }
    rep(i, y.size()) rep(j, x.size()) { board[i + 1][j] += board[i][j]; }
    rep(i, y.size()) rep(j, x.size()) { board[i][j + 1] += board[i][j]; }
    int res = 0;
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    for (int i = h0; i < hh; ++i)
      for (int j = w0; j < ww; ++j) {
        if (board[i][j] == 0) {
          res++;
          queue<pair<int, int>> que;
          que.push(make_pair(i, j));
          while (!que.empty()) {
            pair<int, int> now = que.front();
            que.pop();
            if (now.first < h0 || hh <= now.first)
              continue;
            if (now.second < w0 || ww <= now.second)
              continue;
            if (board[now.first][now.second] != 0)
              continue;
            board[now.first][now.second] = 9;
            rep(k, 4) {
              que.push(make_pair(now.first + dy[k], now.second + dx[k]));
            }
          }
        }
      }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int board[4010][4010];

int main() {
  while (true) {
    int w, h, n;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> n;
    vector<pair<pair<int, int>, pair<int, int>>> v;
    vector<int> x, y;
    x.push_back(0);
    x.push_back(w);
    y.push_back(0);
    y.push_back(h);
    rep(i, n) {
      int x1, y1, x2, y2;
      cin >> x1 >> y1 >> x2 >> y2;
      v.push_back(make_pair(make_pair(x1, y1), make_pair(x2, y2)));
      x.push_back(x1 - 1);
      x.push_back(x1);
      x.push_back(x2 - 1);
      x.push_back(x2);
      y.push_back(y1 - 1);
      y.push_back(y1);
      y.push_back(y2 - 1);
      y.push_back(y2);
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    y.erase(unique(y.begin(), y.end()), y.end());
    memset(board, 0, sizeof(board));
    int w0 = lower_bound(x.begin(), x.end(), 0) - x.begin();
    int ww = lower_bound(x.begin(), x.end(), w) - x.begin();
    int h0 = lower_bound(y.begin(), y.end(), 0) - y.begin();
    int hh = lower_bound(y.begin(), y.end(), h) - y.begin();
    rep(i, n) {
      int x1 = lower_bound(x.begin(), x.end(), v[i].first.first) - x.begin();
      int y1 = lower_bound(y.begin(), y.end(), v[i].first.second) - y.begin();
      int x2 = lower_bound(x.begin(), x.end(), v[i].second.first) - x.begin();
      int y2 = lower_bound(y.begin(), y.end(), v[i].second.second) - y.begin();
      board[y1][x1]++;
      board[y2][x1]--;
      board[y1][x2]--;
      board[y2][x2]++;
    }
    rep(i, y.size()) rep(j, x.size()) { board[i + 1][j] += board[i][j]; }
    rep(i, y.size()) rep(j, x.size()) { board[i][j + 1] += board[i][j]; }
    int res = 0;
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    for (int i = h0; i < hh; ++i)
      for (int j = w0; j < ww; ++j) {
        if (board[i][j] == 0) {
          res++;
          queue<pair<int, int>> que;
          que.push(make_pair(i, j));
          while (!que.empty()) {
            pair<int, int> now = que.front();
            que.pop();
            if (board[now.first][now.second] != 0)
              continue;
            if (now.first < h0 || hh <= now.first)
              continue;
            if (now.second < w0 || ww <= now.second)
              continue;
            board[now.first][now.second] = 9;
            rep(k, 4) {
              que.push(make_pair(now.first + dy[k], now.second + dx[k]));
            }
          }
        }
      }
    cout << res << endl;
  }
  return 0;
}