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

#include <bits/stdc++.h>
using namespace std;

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

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

typedef pair<int, int> P;
typedef pair<P, P> Beni;

int w, h, n;
vector<Beni> benis;
map<int, int> zip_x, zip_y;
int unzip_x[1000001];
int unzip_y[1000001];

pii compress() {
  vector<int> x, y;

  int benis_size = benis.size();
  for (int i = 0; i < benis_size; i++) {
    x.push_back(benis[i].first.first);
    x.push_back(benis[i].second.first);
    y.push_back(benis[i].first.second);
    y.push_back(benis[i].second.second);
  }

  x.push_back(0);
  x.push_back(w);

  y.push_back(0);
  y.push_back(h);

  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());

  int x_size = x.size();
  int y_size = y.size();

  for (int i = 0; i < x_size; i++) {
    zip_x[x[i]] = i;
    unzip_x[i] = x[i];
  }

  for (int i = 0; i < y_size; i++) {
    zip_y[y[i]] = i;
    unzip_y[i] = y[i];
  }

  return pii(x_size, y_size);
}

void dfs(int x, int y, int x_size, int y_size, vector<vector<char>> &mp) {
  mp[y][x] = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < x_size && 0 <= ny && ny < y_size && mp[ny][nx] == 0)
      dfs(nx, ny, x_size, y_size, mp);
  }
}

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

  while (cin >> w >> h, w || h) {
    cin >> n;
    benis.clear();

    for (int i = 0; i < n; i++) {
      P bl, tr;
      cin >> bl.first >> bl.second >> tr.first >> tr.second;
      benis.push_back(Beni(bl, tr));
    }

    pii res = compress();

    // from coordinate to cell
    int x_size = res.first - 1;
    int y_size = res.second - 1;

    vector<vector<char>> mp(y_size, vector<char>(x_size, 0));

    for (auto beni : benis) {
      P bl = beni.first;
      P tr = beni.second;
      for (int x = zip_x[bl.first]; x <= zip_x[tr.first] - 1; x++) {
        for (int y = zip_y[bl.second]; y <= zip_y[tr.second] - 1; y++) {
          mp[y][x] = 1;
        }
      }
    }

    int ans = 0;
    for (int x = 0; x < x_size; x++) {
      for (int y = 0; y < y_size; y++) {
        if (mp[y][x] == 1)
          continue;
        dfs(x, y, x_size, y_size, mp);
        ans++;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

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

typedef pair<int, int> P;
typedef pair<P, P> Beni;

int w, h, n;
vector<Beni> benis;
map<int, int> zip_x, zip_y;
int unzip_x[1000001];
int unzip_y[1000001];

pii compress() {
  vector<int> x, y;

  int benis_size = benis.size();
  for (int i = 0; i < benis_size; i++) {
    x.push_back(benis[i].first.first);
    x.push_back(benis[i].second.first);
    y.push_back(benis[i].first.second);
    y.push_back(benis[i].second.second);
  }

  x.push_back(0);
  x.push_back(w);

  y.push_back(0);
  y.push_back(h);

  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());

  int x_size = x.size();
  int y_size = y.size();

  for (int i = 0; i < x_size; i++) {
    zip_x[x[i]] = i;
    unzip_x[i] = x[i];
  }

  for (int i = 0; i < y_size; i++) {
    zip_y[y[i]] = i;
    unzip_y[i] = y[i];
  }

  return pii(x_size, y_size);
}

void dfs(int x, int y, int x_size, int y_size, vector<vector<bool>> &mp) {
  mp[y][x] = true;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < x_size && 0 <= ny && ny < y_size && mp[ny][nx] == false)
      dfs(nx, ny, x_size, y_size, mp);
  }
}

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

  while (cin >> w >> h, w || h) {
    cin >> n;
    benis.clear();

    for (int i = 0; i < n; i++) {
      P bl, tr;
      cin >> bl.first >> bl.second >> tr.first >> tr.second;
      benis.push_back(Beni(bl, tr));
    }

    pii res = compress();

    // from coordinate to cell
    int x_size = res.first - 1;
    int y_size = res.second - 1;

    vector<vector<bool>> mp(y_size, vector<bool>(x_size, false));

    for (auto beni : benis) {
      P bl = beni.first;
      P tr = beni.second;
      for (int x = zip_x[bl.first]; x <= zip_x[tr.first] - 1; x++) {
        for (int y = zip_y[bl.second] - 1; y <= zip_y[tr.second] - 1; y++) {
          mp[y][x] = true;
        }
      }
    }

    int ans = 0;
    for (int x = 0; x < x_size; x++) {
      for (int y = 0; y < y_size; y++) {
        if (mp[y][x] == true)
          continue;
        dfs(x, y, x_size, y_size, mp);
        ans++;
      }
    }

    cout << ans << endl;
  }
  return 0;
}