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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define WMAX 2010
#define HMAX 2010
#define NMAX 1002
#define SORT(a) sort((a).begin(), (a).end())
#define ALL(a) (a).begin(), (a).end()

//??????????????¢??????????????¨debug()??????????????????
#define DEBUG

using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef pair<ll, ll> P;

struct edge {
  int to, cost;
};

int N, W, H, x1[NMAX], x2[NMAX], y1[NMAX], y2[NMAX], dp[HMAX][WMAX] = {}, h, w,
                                                     ans = 0;
static const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void compress(vector<ll> &xs, map<ll, ll> &mp) {
  SORT(xs);
  xs.erase(unique(ALL(xs)), xs.end());
  for (int i = 0; i < xs.size(); ++i) {
    mp[xs[i]] = i;
  }
}

void dfs(int a, int b) {
  dp[a][b] = 1;
  for (int i = 0; i < 4; ++i) {
    if (a + dx[i] < h && a + dx[i] >= 0 && b + dy[i] < w && b + dy[i] >= 0 &&
        !dp[a + dx[i]][b + dy[i]]) {
      dfs(a + dx[i], b + dy[i]);
    }
  }
}

void bfs(ll a, ll b) {
  queue<P> que;
  que.push(P(a, b));
  dp[a][b] = 1;
  while (!que.empty()) {
    ll j = que.front().first, k = que.front().second;
    que.pop();
    for (int i = 0; i < 4; ++i) {
      if (j + dx[i] < h && j + dx[i] >= 0 && k + dy[i] < w && k + dy[i] >= 0 &&
          !dp[j + dx[i]][k + dy[i]]) {
        que.push(P(j + dx[i], k + dy[i]));
        dp[j + dx[i]][k + dy[i]] = 1;
      }
    }
  }
}

void dye(int X1, int X2, int Y1, int Y2) {
  for (int i = Y1; i < Y2; ++i) {
    for (int j = X1; j < X2; ++j) {
      dp[j][i] = 1;
    }
  }
}

void solve() {
  vector<ll> xs, ys;
  map<ll, ll> xmp, ymp;
  for (int i = 0; i < N; ++i) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
    ys.push_back(y1[i]);
    ys.push_back(y2[i]);
  }
  xs.push_back(0);
  ys.push_back(0);
  xs.push_back(H);
  ys.push_back(W);
  compress(xs, xmp);
  compress(ys, ymp);
  for (int i = 0; i < N; ++i) {
    x1[i] = xmp[x1[i]];
    x2[i] = xmp[x2[i]];
    y1[i] = ymp[y1[i]];
    y2[i] = ymp[y2[i]];
    dye(x1[i], x2[i], y1[i], y2[i]);
  }
  h = xmp[H];
  w = ymp[W];
  // for (int i = 0; i < h; ++i)
  // {
  // 	for (int j = 0; j < w; ++j)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (!dp[i][j]) {
        ans++;
        // printf("i: %d, j: %d\\n",i,j);
        bfs(i, j);
      }
    }
  }

  Fill(dp, 0);
  Fill(x1, 0);
  Fill(x2, 0);
  Fill(y1, 0);
  Fill(y2, 0);
}

void debug() {}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    }
    solve();
#ifdef DEBUG
    debug();
#endif
    cout << ans << "\\n";
    ans = 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define WMAX 2010
#define HMAX 2010
#define NMAX 102
#define SORT(a) sort((a).begin(), (a).end())
#define ALL(a) (a).begin(), (a).end()

//??????????????¢??????????????¨debug()??????????????????
#define DEBUG

using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int to, cost;
};

int N, W = 1, H = 1, x1[NMAX], x2[NMAX], y1[NMAX], y2[NMAX],
       dp[HMAX][WMAX] = {}, h, w, ans = 0;
static const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void compress(vector<ll> &xs, map<ll, ll> &mp) {
  SORT(xs);
  xs.erase(unique(ALL(xs)), xs.end());
  for (int i = 0; i < xs.size(); ++i) {
    mp[xs[i]] = i;
  }
}

void bfs(int a, int b) {
  dp[a][b] = 1;
  for (int i = 0; i < 4; ++i) {
    if (a + dx[i] < h && a + dx[i] >= 0 && b + dy[i] < w && b + dy[i] >= 0 &&
        !dp[a + dx[i]][b + dy[i]]) {
      bfs(a + dx[i], b + dy[i]);
    }
  }
}

void dye(int X1, int X2, int Y1, int Y2) {
  for (int i = Y1; i < Y2; ++i) {
    for (int j = X1; j < X2; ++j) {
      dp[j][i] = 1;
    }
  }
}

void solve() {
  vector<ll> xs, ys;
  map<ll, ll> xmp, ymp;
  for (int i = 0; i < N; ++i) {
    xs.push_back(x1[i]);
    xs.push_back(x2[i]);
    ys.push_back(y1[i]);
    ys.push_back(y2[i]);
  }
  xs.push_back(0);
  ys.push_back(0);
  xs.push_back(H);
  ys.push_back(W);
  compress(xs, xmp);
  compress(ys, ymp);
  for (int i = 0; i < N; ++i) {
    x1[i] = xmp[x1[i]];
    x2[i] = xmp[x2[i]];
    y1[i] = ymp[y1[i]];
    y2[i] = ymp[y2[i]];
    dye(x1[i], x2[i], y1[i], y2[i]);
  }
  h = xmp[H];
  w = ymp[W];
  // for (int i = 0; i < h; ++i)
  // {
  // 	for (int j = 0; j < w; ++j)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (!dp[i][j]) {
        ans++;
        bfs(i, j);
      }
    }
  }

  Fill(dp, 0);
  Fill(x1, 0);
  Fill(x2, 0);
  Fill(y1, 0);
  Fill(y2, 0);
}

void debug() {}

int main() {
  while (true) {
    cin >> H >> W;
    if (H == 0 && W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; ++i) {
      cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    }
    solve();
#ifdef DEBUG
    debug();
#endif
    cout << ans << "\\n";
    ans = 0;
  }
  return 0;
}