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

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int imos[1010][1010];
int H, W, N;

void bfs(int y, int x) {
  queue<P> que;

  que.push(P{y, x});
  imos[y][x] = 1;

  while (Size(que)) {
    P p = que.front();
    que.pop();

    int y = p.fi, x = p.se;

    REP(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];

      if (ny < 0 || nx < 0 || W <= nx || H <= ny) {
        continue;
      }

      if (imos[ny][nx] != 0) {
        continue;
      }

      imos[ny][nx] = 1;

      que.push(P{ny, nx});
    }
  }

  return;
}

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

  while (cin >> W >> H >> N, W) {
    memset(imos, 0, sizeof(imos));

    vector<ll> sx, ex, sy, ey, X, Y;

    sx.resize(N, 0);
    sy.resize(N, 0);
    ex.resize(N, 0);
    ey.resize(N, 0);

    REP(i, N) {
      cin >> sx[i] >> sy[i] >> ex[i] >> ey[i];

      X.push_back(sx[i]);
      X.push_back(ex[i]);
      Y.push_back(sy[i]);
      Y.push_back(ey[i]);
    }

    X.push_back(0);
    X.push_back(W);
    Y.push_back(0);
    Y.push_back(H);

    sort(ALL(X));
    sort(ALL(Y));

    X.erase(unique(ALL(X)), X.end());
    Y.erase(unique(ALL(Y)), Y.end());

    REP(i, N) {
      int x1 = lower_bound(ALL(X), sx[i]) - X.begin();
      int x2 = lower_bound(ALL(X), ex[i]) - X.begin();

      int y1 = lower_bound(ALL(Y), sy[i]) - Y.begin();
      int y2 = lower_bound(ALL(Y), ey[i]) - Y.begin();

      imos[x1][y1]++;
      imos[x2][y2]++;
      imos[x1][y2]--;
      imos[x2][y1]--;
    }

    H = Size(X) - 1;
    W = Size(Y) - 1;

    REP(y, H) {
      REP(x, W) {
        if (x == 0)
          continue;

        imos[y][x] += imos[y][x - 1];
      }
    }

    REP(y, H) {
      REP(x, W) {
        if (y == 0)
          continue;

        imos[y][x] += imos[y - 1][x];
      }
    }

    int ans = 0;

    REP(y, H) {
      REP(x, W) {
        if (imos[y][x] == 0) {
          ans++;
          bfs(y, x);
        }
      }
    }

    Cout(ans);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

vector<ll> sx, ex, sy, ey, X, Y;
int imos[1010][1010];
int H, W, N;

void bfs(int y, int x) {
  queue<P> que;

  que.push(P{y, x});
  imos[y][x] = 1;

  while (Size(que)) {
    P p = que.front();
    que.pop();

    int y = p.fi, x = p.se;

    REP(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];

      if (ny < 0 || nx < 0 || W <= nx || H <= ny) {
        continue;
      }

      if (imos[ny][nx] != 0) {
        continue;
      }

      imos[ny][nx] = 1;

      que.push(P{ny, nx});
    }
  }

  return;
}

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

  while (cin >> W >> H >> N, W) {

    sx.resize(N);
    sy.resize(N);
    ex.resize(N);
    ey.resize(N);

    REP(i, N) {
      cin >> sx[i] >> sy[i] >> ex[i] >> ey[i];

      X.push_back(sx[i]);
      X.push_back(ex[i]);
      Y.push_back(sy[i]);
      Y.push_back(ey[i]);
    }

    X.push_back(0);
    X.push_back(W);
    Y.push_back(0);
    Y.push_back(H);

    sort(ALL(X));
    sort(ALL(Y));

    X.erase(unique(ALL(X)), X.end());
    Y.erase(unique(ALL(Y)), Y.end());

    REP(i, N) {
      int x1 = lower_bound(ALL(X), sx[i]) - X.begin();
      int x2 = lower_bound(ALL(X), ex[i]) - X.begin();

      int y1 = lower_bound(ALL(Y), sy[i]) - Y.begin();
      int y2 = lower_bound(ALL(Y), ey[i]) - Y.begin();

      imos[x1][y1]++;
      imos[x2][y2]++;
      imos[x1][y2]--;
      imos[x2][y1]--;
    }

    H = Size(X) - 1;
    W = Size(Y) - 1;

    REP(y, H) {
      REP(x, W) {
        if (x == 0)
          continue;

        imos[y][x] += imos[y][x - 1];
      }
    }

    REP(y, H) {
      REP(x, W) {
        if (y == 0)
          continue;

        imos[y][x] += imos[y - 1][x];
      }
    }

    int ans = 0;

    REP(y, H) {
      REP(x, W) {
        if (imos[y][x] == 0) {
          ans++;
          bfs(y, x);
        }
      }
    }

    Cout(ans);
  }

  return 0;
}