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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll W, H, N;
ll X1[1000], X2[5000], Y1[5000], Y2[5000];

ll compress(ll *x1, ll *x2, ll w) {
  vector<ll> xs;
  rep(i, N) {
    for (ll k = -1; k <= 1; k++) {
      if (0 <= x1[i] + k && x1[i] + k < w)
        xs.pb(x1[i] + k);
      if (0 <= x2[i] + k && x2[i] + k < w)
        xs.pb(x2[i] + k);
    }
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  rep(i, N) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }
  return xs.size();
}
bool grid[6000][6000];

ll vx[4] = {1, 0, -1, 0};
ll vy[4] = {0, 1, 0, -1};

bool ok(ll y, ll x) { return 0 <= y && y < H && 0 <= x && x < W; }
bool dfs(ll y, ll x) {
  if (!grid[y][x]) {
    grid[y][x] = 1;
    rep(i, 4) {
      if (ok(y + vy[i], x + vx[i]))
        dfs(y + vy[i], x + vx[i]);
    }
    return 1;
  } else
    return 0;
}

int main() {
  while (1) {
    cin >> W >> H;
    if (!W)
      return 0;
    cin >> N;
    rep(i, N) {
      cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
      X2[i]--;
      Y2[i]--;
    }
    W = compress(X1, X2, W);
    H = compress(Y1, Y2, H);
    rep(i, H) rep(j, W) grid[i][j] = 0;
    rep(i, N) {
      for (ll y = Y1[i]; y <= Y2[i]; y++) {
        for (ll x = X1[i]; x <= X2[i]; x++) {
          grid[y][x] = 1;
        }
      }
    }
    ll ans = 0;
    /// rep(i,H){ rep(j,W){
    //	cout<<grid[i][j];
    //} cout<<endl;}
    rep(i, H) rep(j, W) {
      if (dfs(i, j))
        ans++;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int lnt;
typedef pair<lnt, lnt> P;
lnt W, H, n;
lnt X1[1000], X2[1000];
lnt Y1[1000], Y2[1000];
queue<P> q;
lnt compress(lnt w, lnt *x1, lnt *x2) {
  vector<lnt> xs;
  xs.push_back(0);
  xs.push_back(w - 1);
  for (lnt i = 0; i < n; i++) {
    for (lnt d = -1; d <= 1; d++) {
      if (0 <= x1[i] + d && x1[i] + d < w)
        xs.push_back(x1[i] + d);
      if (0 <= x2[i] + d && x2[i] + d < w)
        xs.push_back(x2[i] + d);
    }
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (lnt i = 0; i < n; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  }
  return xs.size();
}

bool ita[6100][6100];

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

void dfs(lnt i, lnt j) {
  if (ita[i][j])
    return;
  ita[i][j] = 1;
  for (lnt d = 0; d < 4; d++) {
    lnt nx = i + dx[d], ny = j + dy[d];
    if (0 <= nx && nx < W && 0 <= ny && ny < H)
      dfs(nx, ny);
  }
}

int main() {
  while (1) {
    memset(ita, 0, sizeof(ita));
    scanf("%lld%lld", &W, &H);
    if (!W && !H)
      return 0;
    scanf("%lld", &n);
    for (lnt i = 0; i < n; i++) {
      scanf("%lld%lld%lld%lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
      X2[i]--;
      Y2[i]--;
    }
    W = compress(W, X1, X2);
    H = compress(H, Y1, Y2);
    for (lnt i = 0; i < n; i++) {
      for (lnt x = X1[i]; x <= X2[i]; x++) {
        for (lnt y = Y1[i]; y <= Y2[i]; y++) {
          ita[x][y] = 1;
        }
      }
    }
    lnt ans = 0;
    for (lnt i = 0; i < W; i++) {
      for (lnt j = 0; j < H; j++) {
        if (ita[i][j])
          continue;
        ans++;
        dfs(i, j);
      }
    }
    cout << ans << endl;
  }
}