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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};

bool v[3003][3003];

int compress(vector<int> &x1, vector<int> &x2, int maxlen) {
  vector<int> xs;
  xs.clear();
  xs.push_back(0);
  xs.push_back(maxlen);
  int n = x1.size();
  rep(i, n) {
    for (int d = -1; d <= 1; d++) {
      if (x1[i] + d >= 0 && x1[i] + d <= maxlen)
        xs.push_back(x1[i]);
      if (x2[i] + d >= 0 && x2[i] + d <= maxlen)
        xs.push_back(x2[i]);
    } // end for
  }   // end for
  sort(ALL(xs));
  xs.erase(unique(ALL(xs)), xs.end());
  rep(i, n) {
    x1[i] = lower_bound(ALL(xs), x1[i]) - xs.begin();
    x2[i] = lower_bound(ALL(xs), x2[i]) - xs.begin();
  } // end rep

  return xs.size();
}

void bfs(int row, int col, int h, int w) {
  queue<P> que;
  que.push(P(row, col));
  while (!que.empty()) {
    P curr = que.front();
    que.pop();
    int cr = curr.first;
    int cc = curr.second;
    rep(k, 4) {
      int nr = cr + dr[k];
      int nc = cc + dc[k];
      if (nr < 0 || nr >= h || nc < 0 || nc >= w)
        continue;
      if (v[nr][nc])
        continue;
      que.push(P(nr, nc));
      v[nr][nc] = true;
    } // end rep
  }   // end while
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.paint_color", "r", stdin );
  int w, h;
  while (scanf("%d %d", &w, &h), w, h) {
    memset(v, false, sizeof(v));
    int n;
    scanf("%d", &n);
    vector<int> x1(n, 0), y1(n, 0), x2(n, 0), y2(n, 0);
    rep(i, n) {
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
    } // end rep
    w = compress(x1, x2, w);
    h = compress(y1, y2, h);
    rep(i, h) v[i][w - 1] = true;
    rep(j, w) v[h - 1][j] = true;

    rep(i, n) {
      for (int y = y1[i]; y < y2[i]; y++) {
        for (int x = x1[i]; x < x2[i]; x++) {
          v[y][x] = true;
        } // end for
      }   // end for
    }     // end for

    int res = 0;
    rep(i, h) {
      rep(j, w) {
        if (!v[i][j]) {
          v[i][j] = true;
          res++;
          bfs(i, j, h, w);
        } // end if
      }   // end rep
    }     // end rep

    printf("%d\\n", res);
  } // end loop

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};

bool v[3003][3003];

int compress(vector<int> &x1, vector<int> &x2, int maxlen) {
  vector<int> xs;
  xs.clear();
  xs.push_back(0);
  xs.push_back(maxlen);
  int n = x1.size();
  rep(i, n) {
    for (int d = -1; d <= 1; d++) {
      if (x1[i] + d >= 0 && x1[i] + d <= maxlen)
        xs.push_back(x1[i]);
      if (x2[i] + d >= 0 && x2[i] + d <= maxlen)
        xs.push_back(x2[i]);
    } // end for
  }   // end for
  sort(ALL(xs));
  xs.erase(unique(ALL(xs)), xs.end());
  rep(i, n) {
    x1[i] = lower_bound(ALL(xs), x1[i]) - xs.begin();
    x2[i] = lower_bound(ALL(xs), x2[i]) - xs.begin();
  } // end rep

  return xs.size();
}

void bfs(int row, int col, int h, int w) {
  queue<P> que;
  que.push(P(row, col));
  while (!que.empty()) {
    P curr = que.front();
    que.pop();
    int cr = curr.first;
    int cc = curr.second;
    v[cr][cc] = true;
    rep(k, 4) {
      int nr = cr + dr[k];
      int nc = cc + dc[k];
      if (nr < 0 || nr >= h || nc < 0 || nc >= w)
        continue;
      if (v[nr][nc])
        continue;
      que.push(P(nr, nc));
    } // end rep
  }   // end while
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.paint_color", "r", stdin );
  int w, h;
  while (scanf("%d %d", &w, &h), w, h) {
    memset(v, false, sizeof(v));
    int n;
    scanf("%d", &n);
    vector<int> x1(n, 0), y1(n, 0), x2(n, 0), y2(n, 0);
    rep(i, n) {
      scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]);
    } // end rep
    w = compress(x1, x2, w);
    h = compress(y1, y2, h);
    rep(i, h) v[i][w - 1] = true;
    rep(j, w) v[h - 1][j] = true;

    rep(i, n) {
      for (int y = y1[i]; y < y2[i]; y++) {
        for (int x = x1[i]; x < x2[i]; x++) {
          v[y][x] = true;
        } // end for
      }   // end for
    }     // end for

    int res = 0;
    rep(i, h) {
      rep(j, w) {
        if (!v[i][j]) {
          res++;
          bfs(i, j, h, w);
        } // end if
      }   // end rep
    }     // end rep

    printf("%d\\n", res);
  } // end loop

  return 0;
}