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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> P;
#define PB push_back
#define all(v) (v).begin(), (v).end()
#define uni(v)                                                                 \\
  sort(all(v));                                                                \\
  (v).erase(unique(all(v)), (v).end())
const int INF = 100000;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int sx[3000], sy[3000], ex[3000], ey[3000];
int grid[3000][3000];

int find(VI &v, int &key) {
  int l = 0, r = v.size() - 1;
  while (true) {
    int m = (l + r) / 2;
    if (v[m] == key)
      return m;
    if (v[m] < key)
      l = m + 1;
    else
      r = m - 1;
  }
}

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    VI zip_x;
    zip_x.PB(0);
    zip_x.PB(w);
    VI zip_y;
    zip_y.PB(0);
    zip_y.PB(h);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &sx[i], &sy[i], &ex[i], &ey[i]);
      zip_x.PB(sx[i]), zip_y.PB(sy[i]);
      zip_x.PB(ex[i]), zip_y.PB(ey[i]);
    }
    uni(zip_x);
    uni(zip_y);

    for (int i = 0; i < n; i++) {
      sx[i] = find(zip_x, sx[i]);
      sy[i] = find(zip_y, sy[i]);
      ex[i] = find(zip_x, ex[i]);
      ey[i] = find(zip_y, ey[i]);
    }

    memset(grid, 0, sizeof(grid));
    /*
    for (int i = 0; i < n; i++){
            grid[sx[i]][sy[i]]++;
            grid[sx[i]][ey[i]]--;
            grid[ex[i]][sy[i]]--;
            grid[ex[i]][ey[i]]++;
    }

    for (int i = 0; i < zip_x.size(); i++){
            for (int j = 0; j < zip_y.size(); j++){
                    int a = 0, b = 0;
                    if (i) a = grid[i - 1][j];
                    if (j) b = grid[i][j - 1];
                    grid[i][j] += max(a, b);
            }
    }
    */

    for (int i = 0; i < n; i++) {
      for (int j = sx[i]; j < ex[i]; j++) {
        grid[j][sy[i]]++;
        grid[j][ey[i]]--;
      }
    }

    for (int i = 0; i < zip_x.size(); i++) {
      for (int j = 1; j < zip_y.size(); j++) {
        grid[i][j] += grid[i][j - 1];
      }
    }

    for (int i = 0; i < zip_x.size(); i++) {
      for (int j = 0; j < zip_y.size(); j++) {
        grid[i][j] = !!grid[i][j];
      }
    }

    /*
    for (int i = 0; i < zip_x.size(); i++){
            for (int j = 0; j < zip_y.size(); j++){
                    printf("%d ", grid[i][j]);
            }
            puts("");
    }
    */

    int cnt = 0;

    for (int i = 0; i < zip_x.size() - 1; i++) {
      for (int j = 0; j < zip_y.size() - 1; j++) {
        if (grid[i][j] == 0) {
          cnt++;

          queue<P> que;
          que.push(P(i, j));
          while (!que.empty()) {
            P p = que.front();
            que.pop();
            if (grid[p.first][p.second] == 1)
              continue;
            for (int dir = 0; dir < 4; dir++) {
              int tx = p.first + dx[dir];
              int ty = p.second + dy[dir];
              if (tx >= 0 && tx < zip_x.size() - 1 && ty >= 0 &&
                  ty < zip_y.size() - 1 && grid[tx][ty] == 0) {
                que.push(P(tx, ty));
              }
            }
            grid[p.first][p.second] = 1;
          }
        }
      }
    }

    printf("%d\\n", cnt);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> P;
#define PB push_back
#define all(v) (v).begin(), (v).end()
#define uni(v)                                                                 \\
  sort(all(v));                                                                \\
  (v).erase(unique(all(v)), (v).end())
const int INF = 100000;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int sx[3000], sy[3000], ex[3000], ey[3000];
int grid[3000][3000];

int find(VI &v, int &key) {
  int l = 0, r = v.size() - 1;
  while (true) {
    int m = (l + r) / 2;
    if (v[m] == key)
      return m;
    if (v[m] < key)
      l = m + 1;
    else
      r = m - 1;
  }
}

int main() {
  int w, h;
  while (scanf("%d %d", &w, &h), w + h) {
    VI zip_x;
    zip_x.PB(0);
    zip_x.PB(w);
    VI zip_y;
    zip_y.PB(0);
    zip_y.PB(h);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &sx[i], &sy[i], &ex[i], &ey[i]);
      zip_x.PB(sx[i]), zip_y.PB(sy[i]);
      zip_x.PB(ex[i]), zip_y.PB(ey[i]);
    }
    uni(zip_x);
    uni(zip_y);

    for (int i = 0; i < n; i++) {
      sx[i] = find(zip_x, sx[i]);
      sy[i] = find(zip_y, sy[i]);
      ex[i] = find(zip_x, ex[i]);
      ey[i] = find(zip_y, ey[i]);
    }

    memset(grid, 0, sizeof(grid));
    /*
    for (int i = 0; i < n; i++){
            grid[sx[i]][sy[i]]++;
            grid[sx[i]][ey[i]]--;
            grid[ex[i]][sy[i]]--;
            grid[ex[i]][ey[i]]++;
    }

    for (int i = 0; i < zip_x.size(); i++){
            for (int j = 0; j < zip_y.size(); j++){
                    int a = 0, b = 0;
                    if (i) a = grid[i - 1][j];
                    if (j) b = grid[i][j - 1];
                    grid[i][j] += max(a, b);
            }
    }
    */

    for (int i = 0; i < n; i++) {
      for (int j = sx[i]; j < ex[i]; j++) {
        grid[j][sy[i]]++;
        grid[j][ey[i]]--;
      }
    }

    for (int i = 0; i < zip_x.size(); i++) {
      for (int j = 1; j < zip_y.size(); j++) {
        grid[i][j] += grid[i][j - 1];
      }
    }

    for (int i = 0; i < zip_x.size(); i++) {
      for (int j = 0; j < zip_y.size(); j++) {
        grid[i][j] = !!grid[i][j];
      }
    }

    /*
    for (int i = 0; i < zip_x.size(); i++){
            for (int j = 0; j < zip_y.size(); j++){
                    printf("%d ", grid[i][j]);
            }
            puts("");
    }
    */

    int cnt = 1;
    for (int i = 0; i < zip_x.size() - 1; i++) {
      for (int j = 0; j < zip_y.size() - 1; j++) {
        if (grid[i][j] == 1) {
          cnt++;

          queue<P> que;
          que.push({i, j});
          while (!que.empty()) {
            P p = que.front();
            que.pop();
            if (grid[p.first][p.second] == INF)
              continue;
            for (int dir = 0; dir < 4; dir++) {
              int tx = p.first + dx[dir];
              int ty = p.second + dy[dir];
              if (tx >= 0 && tx < zip_x.size() - 1 && ty >= 0 &&
                  ty < zip_y.size() - 1 && grid[tx][ty] == 1) {
                que.push({tx, ty});
              }
            }
            grid[p.first][p.second] = 0;
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }

  return 0;
}