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

#include <bits/stdc++.h>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

double zero(double d) { return d < EPS ? 0.0 : d; }

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

// to avoid error on mingw
string toString(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}

const int MOD = 1000000007;
// a^k
ll fpow(ll a, ll k, int M) {
  ll res = 1ll;
  ll x = a;
  while (k != 0) {
    if ((k & 1) == 1)
      res = (res * x) % M;
    x = (x * x) % M;
    k >>= 1;
  }
  return res;
}

int compress(vector<pii> &p, int w) {
  vi xs;
  rep(i, p.size()) {
    REP(d, -1, 1 + 1) { // -1, 0, 1  // 蟾ヲ蜿ウ繧貞性繧√※菫晏ュ?
      int tx1 = p[i].first + d, tx2 = p[i].second + d;
      if (0 <= tx1 && tx1 <= w)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 <= w)
        xs.push_back(tx2);
    }
  }

  // 驥崎、?ｒ蜑企勁.
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  // 蝨ァ邵ョ縺励◆蛟、縺ォ譖ク縺肴鋤縺?
  rep(i, p.size()) {
    p[i].first = find(xs.begin(), xs.end(), p[i].first) - xs.begin();
    p[i].second = find(xs.begin(), xs.end(), p[i].second) - xs.begin();
  }

  return xs.size();
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(8);
  ios_base::sync_with_stdio(false);

  int w, h, n;
  while (cin >> w >> h, (w | h)) {
    cin >> n;

    vector<pii> px(n), py(n);
    rep(i, n) cin >> px[i].first >> py[i].first >> px[i].second >> py[i].second;

    w = compress(px, w);
    h = compress(py, h);

    w--, h--;

    // 蝪励ｊ縺、縺カ縺?
    vector<vector<bool>> fld(h, vector<bool>(w, false));
    rep(i, n) {
      REP(y, py[i].first, py[i].second) {
        REP(x, px[i].first, px[i].second)
        fld[y][x] = true;
      }
    }

    int res = 0;
    rep(r, h) {
      rep(c, w) {
        if (!fld[r][c]) {
          res++;

          // bfs
          queue<pii> que;
          que.push(mp(r, c));
          while (!que.empty()) {
            pii now = que.front();
            que.pop();

            rep(d, 4) {
              int my = now.first + dy[d];
              int mx = now.second + dx[d];
              if (0 <= my && my < h && 0 <= mx && mx < w && !fld[my][mx]) {
                fld[my][mx] = true;
                que.push(mp(my, mx));
              }
            }
          }
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-10;

double zero(double d) { return d < EPS ? 0.0 : d; }

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

// to avoid error on mingw
string toString(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}

const int MOD = 1000000007;
// a^k
ll fpow(ll a, ll k, int M) {
  ll res = 1ll;
  ll x = a;
  while (k != 0) {
    if ((k & 1) == 1)
      res = (res * x) % M;
    x = (x * x) % M;
    k >>= 1;
  }
  return res;
}

int compress(vector<pii> &p, int w) {
  vi xs;
  rep(i, p.size()) {
    REP(d, -1, 1 + 1) { // -1, 0, 1  // 蟾ヲ蜿ウ繧貞性繧√※菫晏ュ?
      int tx1 = p[i].first + d, tx2 = p[i].second + d;
      if (0 <= tx1 && tx1 <= w)
        xs.push_back(tx1);
      if (0 <= tx2 && tx2 <= w)
        xs.push_back(tx2);
    }
  }

  // 驥崎、?ｒ蜑企勁.
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());

  // 蝨ァ邵ョ縺励◆蛟、縺ォ譖ク縺肴鋤縺?
  rep(i, p.size()) {
    p[i].first = find(xs.begin(), xs.end(), p[i].first) - xs.begin();
    p[i].second = find(xs.begin(), xs.end(), p[i].second) - xs.begin();
  }

  return xs.size();
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(8);
  ios_base::sync_with_stdio(false);

  int w, h, n;
  while (cin >> w >> h, (w | h)) {
    cin >> n;

    vector<pii> px(n), py(n);
    rep(i, n) cin >> px[i].first >> py[i].first >> px[i].second >> py[i].second;

    w = compress(px, w);
    h = compress(py, h);

    w--, h--;

    // 蝪励ｊ縺、縺カ縺?
    vector<vector<bool>> fld(h, vector<bool>(w, false));
    rep(i, n) {
      REP(y, py[i].first, py[i].second) {
        REP(x, px[i].first, px[i].second)
        fld[y][x] = true;
      }
    }

    int res = 0;
    rep(r, h) {
      rep(c, w) {
        if (!fld[r][c]) {
          res++;

          // bfs
          queue<pii> que;
          que.push(mp(r, c));
          while (!que.empty()) {
            pii now = que.front();
            que.pop();
            fld[now.first][now.second] = true;

            rep(d, 4) {
              int my = now.first + dy[d];
              int mx = now.second + dx[d];
              if (0 <= my && my < h && 0 <= mx && mx < w && !fld[my][mx])
                que.push(mp(my, mx));
            }
          }
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}