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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) c.begin(), c.end()

map<int, int> compress(vector<int> &from) {
  map<int, int> f;
  sort(all(from));
  from.erase(unique(all(from)), from.end());
  int cur = 0;
  for (int i = (from[0] == 0 ? 1 : 0); i < from.size(); i++) {
    if (from[i] == cur + 1)
      cur++;
    else
      cur += 2;
    f[from[i]] = cur;
  }
  return f;
}

int w, h;
bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; }

bool mask[2000][2000];

void dfs(int x, int y) {
  typedef pair<int, int> P;
  stack<P> s;
  s.push(P(x, y));
  const int dx[] = {1, -1, 0, 0};
  const int dy[] = {0, 0, 1, -1};
  while (s.size()) {
    auto p = s.top();
    s.pop();
    if (!in(p.first, p.second) || mask[p.second][p.first] == 1)
      continue;
    mask[p.second][p.first] = 1;
    rep(i, 4) {
      int xx = p.first + dx[i];
      int yy = p.second + dy[i];
      s.push(P(xx, yy));
    }
  }
  /*
      if (!in(x, y) || mask[y][x] == 1) return;
      mask[y][x] = 1;
      const int dx[] = { 0, 0, 1, -1 };
      const int dy[] = { 1, -1, 0, 0 };
      rep(i, 4){
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (in(xx, yy) && mask[yy][xx] == 0) dfs(xx, yy);
      }*/
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    int n;
    cin >> n;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    vector<int> ax(n + n + 2), ay(n + n + 2);
    rep(i, n) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      ax[i + i] = x1[i];
      ax[i + i + 1] = x2[i];
      ay[i + i] = y1[i];
      ay[i + i + 1] = y2[i];
    }
    ax.push_back(0);
    ax.push_back(w);
    ay.push_back(0);
    ay.push_back(h);
    auto fx = compress(ax);
    auto fy = compress(ay);
    w = fx[w];
    h = fy[h];
    rep(i, n) {
      x1[i] = fx[x1[i]];
      x2[i] = fx[x2[i]];
      y1[i] = fy[y1[i]];
      y2[i] = fy[y2[i]];
    }
    memset(mask, 0, sizeof(mask));
    rep(i, n) {
      for (int x = x1[i]; x < x2[i]; x++) {
        for (int y = y1[i]; y < y2[i]; y++) {
          mask[y][x] = 1;
        }
      }
    }
    int ans = 0;
    rep(x, w) {
      rep(y, h) {
        if (mask[y][x] == 0) {
          ans++;
          dfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) c.begin(), c.end()

map<int, int> compress(vector<int> &from) {
  map<int, int> f;
  sort(all(from));
  from.erase(unique(all(from)), from.end());
  int cur = 0;
  for (int i = (from[0] == 0 ? 1 : 0); i < from.size(); i++) {
    if (from[i] == cur + 1)
      cur++;
    else
      cur += 2;
    f[from[i]] = cur;
  }
  return f;
}

int w, h;
bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; }

int mask[2000][2000];

void dfs(int x, int y) {
  if (!in(x, y) || mask[y][x] == 1)
    return;
  mask[y][x] = 1;
  const int dx[] = {0, 0, 1, -1};
  const int dy[] = {1, -1, 0, 0};
  rep(i, 4) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (in(xx, yy) && mask[yy][xx] == 0)
      dfs(xx, yy);
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int n;
    cin >> n;
    vector<int> x1(n), y1(n), x2(n), y2(n);
    vector<int> ax(n + n + 2), ay(n + n + 2);
    rep(i, n) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
      ax[i + i] = x1[i];
      ax[i + i + 1] = x2[i];
      ay[i + i] = y1[i];
      ay[i + i + 1] = y2[i];
    }
    ax.push_back(0);
    ax.push_back(w);
    ay.push_back(0);
    ay.push_back(h);
    auto fx = compress(ax);
    auto fy = compress(ay);
    w = fx[w];
    h = fy[h];
    rep(i, n) {
      x1[i] = fx[x1[i]];
      x2[i] = fx[x2[i]];
      y1[i] = fy[y1[i]];
      y2[i] = fy[y2[i]];
    }
    memset(mask, 0, sizeof(mask));
    rep(i, n) {
      for (int x = x1[i]; x < x2[i]; x++) {
        for (int y = y1[i]; y < y2[i]; y++) {
          mask[y][x] = 1;
        }
      }
    }
    int ans = 0;
    rep(x, w) {
      rep(y, h) {
        if (mask[y][x] == 0) {
          ans++;
          dfs(x, y);
        }
      }
    }
    cout << ans << endl;
  }
}