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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << ((x)) << endl
#define fi first
#define se second

#define INF 2147483600
#define long long long

int main() {
  int h, w;
  while (scanf("%d %d", &w, &h), w | h) {
    int n;
    cin >> n;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    rep(i, n) scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);

    // ??§??§
    vector<int> sx, sy;
    rep(i, n) {
      for (int j = 0; j <= 1; j++) {
        sx.pb(x1[i] + j);
        sy.pb(y1[i] + j);
        sx.pb(x2[i] + j - 1);
        sy.pb(y2[i] + j - 1);
      }
    }
    sx.pb(0);
    sx.pb(w - 1);
    sx.pb(w);
    sy.pb(0);
    sy.pb(h - 1);
    sy.pb(h);
    sort(all(sx));
    sx.erase(unique(all(sx)), sx.end());
    sort(all(sy));
    sy.erase(unique(all(sy)), sy.end());

    int x = sx.size();
    int y = sy.size();

    vector<vector<int>> vec(x, vector<int>(y, 0));

    // imos ??????
    rep(i, n) {
      int nx1 = lower_bound(all(sx), x1[i]) - sx.begin();
      int ny1 = lower_bound(all(sy), y1[i]) - sy.begin();
      int nx2 = lower_bound(all(sx), x2[i]) - sx.begin();
      int ny2 = lower_bound(all(sy), y2[i]) - sy.begin();
      vec[nx1][ny1]++;
      vec[nx2][ny1]--;
      vec[nx1][ny2]--;
      vec[nx2][ny2]++;
    }

    // imos??????
    rep(i, x) repl(j, 1, y) vec[i][j] += vec[i][j - 1];
    rep(j, y) repl(i, 1, x) vec[i][j] += vec[i - 1][j];

    int res = 0;
    rep(i, x - 1) rep(j, y - 1) if (vec[i][j] == 0) {
      res++;
      stack<pair<int, int>> s;
      s.push(mp(i, j));
      while (!s.empty()) {
        pair<int, int> p = s.top();
        s.pop();
        vec[p.fi][p.se] = 1;
        const int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
        rep(k, 4) {
          int nx = p.fi + dx[k], ny = p.se + dy[k];
          if (nx < 0 || nx >= x - 1 || ny < 0 || ny >= y - 1)
            continue;
          if (vec[nx][ny] == 0)
            s.push(mp(nx, ny));
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << ((x)) << endl
#define fi first
#define se second

#define INF 2147483600
#define long long long

int x, y;
void dfs(int i, int j, vector<vector<int>> &vec) {
  vec[i][j] = 1;
  const int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
  rep(k, 4) {
    int nx = i + dx[k], ny = j + dy[k];
    if (nx < 0 || nx >= x - 1 || ny < 0 || ny >= y - 1)
      continue;
    if (vec[nx][ny] == 0)
      dfs(nx, ny, vec);
  }
}

int main() {
  int h, w;
  while (scanf("%d %d", &w, &h), w | h) {
    int n;
    cin >> n;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    rep(i, n) scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);

    // ??§??§
    vector<int> sx, sy;
    rep(i, n) {
      for (int j = 0; j <= 1; j++) {
        sx.pb(x1[i] + j);
        sy.pb(y1[i] + j);
        sx.pb(x2[i] + j - 1);
        sy.pb(y2[i] + j - 1);
      }
    }
    sx.pb(0);
    sx.pb(w - 1);
    sx.pb(w);
    sy.pb(0);
    sy.pb(h - 1);
    sy.pb(h);
    sort(all(sx));
    sx.erase(unique(all(sx)), sx.end());
    sort(all(sy));
    sy.erase(unique(all(sy)), sy.end());

    x = sx.size();
    y = sy.size();

    vector<vector<int>> vec(x, vector<int>(y, 0));

    // imos ??????
    rep(i, n) {
      int nx1 = lower_bound(all(sx), x1[i]) - sx.begin();
      int ny1 = lower_bound(all(sy), y1[i]) - sy.begin();
      int nx2 = lower_bound(all(sx), x2[i]) - sx.begin();
      int ny2 = lower_bound(all(sy), y2[i]) - sy.begin();
      vec[nx1][ny1]++;
      vec[nx2][ny1]--;
      vec[nx1][ny2]--;
      vec[nx2][ny2]++;
    }

    // imos??????
    rep(i, x) repl(j, 1, y) vec[i][j] += vec[i][j - 1];
    rep(j, y) repl(i, 1, x) vec[i][j] += vec[i - 1][j];

    int res = 0;
    rep(i, x - 1) rep(j, y - 1) if (vec[i][j] == 0) {
      res++;
      dfs(i, j, vec);
    }
    cout << res << endl;
  }
  return 0;
}