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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
std::vector<int> xs;
vector<int> ys;
int X1[1002], X2[1002], Y1[1002], Y2[1002];
bool bmap[5000][5000];
class SOLVE {
public:
  int ans() {
    int w, h, n;
    scanf("%lld %lld", &w, &h);
    if (w == 0 && h == 0) {
      return (int)-1;
    }
    scanf("%lld", &n);
    if (xs.size())
      xs.clear();
    if (ys.size())
      ys.clear();
    fill(bmap[0], bmap[5000], false);
    xs.push_back(0);
    xs.push_back(w);
    ys.push_back(0);
    ys.push_back(h);

    rep(i, n) {
      int x1, x2, y1, y2;
      scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
      X1[i] = x1;
      Y1[i] = y1;
      X2[i] = x2;
      Y2[i] = y2;
      xs.push_back(x1);
      xs.push_back(x1 + 1);
      xs.push_back(x2 - 1);
      xs.push_back(x2);
      ys.push_back(y1);
      ys.push_back(y1 + 1);
      ys.push_back(y2 - 1);
      ys.push_back(y2);
    }
    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    rep(i, n) {
      X1[i] = find(xs.begin(), xs.end(), X1[i]) - xs.begin();
      X2[i] = find(xs.begin(), xs.end(), X2[i]) - xs.begin();
      Y1[i] = find(ys.begin(), ys.end(), Y1[i]) - ys.begin();
      Y2[i] = find(ys.begin(), ys.end(), Y2[i]) - ys.begin();
    }
    int ans = 0;
    rep(i, n) {
      for (int x = X1[i]; x < X2[i]; x++) {
        for (int y = Y1[i]; y < Y2[i]; y++) {
          bmap[x][y] = true;
        }
      }
    }
    int ww = find(xs.begin(), xs.end(), w) - xs.begin();
    int hh = find(ys.begin(), ys.end(), h) - ys.begin();
    /*rep(i,ww){
        rep(j,hh){
            printf("%lld",(int)bmap[i][j]);
        }
        printf("\\n");
    }*/
    rep(i, ww) {
      rep(j, hh) {
        queue<pair<int, int>> que;
        if (bmap[i][j]) {
          continue;
        } else {
          que.push(make_pair(i, j));
          ans++;
          // printf("%lld %lld/",i,j);
          while (que.size()) {
            pair<int, int> p = que.front();
            que.pop();
            int x = p.first;
            int y = p.second;
            if (bmap[x][y] == true) {
              continue;
            }
            bmap[x][y] = true;
            // printf("[%lld %lld]",x,y);
            if (x > 0 && bmap[x - 1][y] == false) {
              que.push(make_pair(x - 1, y));
            }
            if (y > 0 && bmap[x][y - 1] == false) {
              que.push(make_pair(x, y - 1));
            }
            if (x < ww - 1 && bmap[x + 1][y] == false) {
              que.push(make_pair(x + 1, y));
            }
            if (y < hh - 1 && bmap[x][y + 1] == false) {
              que.push(make_pair(x, y + 1));
            }
          }
        }
      }
    }
    return ans;
  }
};
signed main(void) {
  while (1) {
    SOLVE sol;
    int pr = sol.ans();
    if (pr == -1)
      break;
    printf("%lld\\n", pr);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 1000

struct mtape {
  int x1, x2, y1, y2;
};

int W, H, N;
vector<int> xinfo;
vector<int> yinfo;
bool map[MAX_N * 4][MAX_N * 4];
mtape mtinfo[MAX_N];

void compress() {
  rep(i, N) {
    int x1, x2, y1, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    mtinfo[i].x1 = x1;
    mtinfo[i].y1 = y1;
    mtinfo[i].x2 = x2;
    mtinfo[i].y2 = y2;
    xinfo.push_back(mtinfo[i].x1);
    xinfo.push_back(mtinfo[i].x2);
    // if(mtinfo[i].x1-1>=0)xinfo.push_back(mtinfo[i].x1-1);
    // if(mtinfo[i].x2+1<=W-1)xinfo.push_back(mtinfo[i].x2+1);
    yinfo.push_back(mtinfo[i].y1);
    yinfo.push_back(mtinfo[i].y2);
    // if(mtinfo[i].y1-1>=0)yinfo.push_back(mtinfo[i].y1-1);
    // if(mtinfo[i].y2+1<=H-1)yinfo.push_back(mtinfo[i].y2+1);
  }
  xinfo.push_back(0);
  xinfo.push_back(W);
  yinfo.push_back(0);
  yinfo.push_back(H);

  sort(xinfo.begin(), xinfo.end());
  xinfo.erase(unique(xinfo.begin(), xinfo.end()), xinfo.end());
  sort(yinfo.begin(), yinfo.end());
  yinfo.erase(unique(yinfo.begin(), yinfo.end()), yinfo.end());
  rep(i, N) {
    mtinfo[i].x1 =
        find(xinfo.begin(), xinfo.end(), mtinfo[i].x1) - xinfo.begin();
    mtinfo[i].x2 =
        find(xinfo.begin(), xinfo.end(), mtinfo[i].x2) - xinfo.begin();
    mtinfo[i].y1 =
        find(yinfo.begin(), yinfo.end(), mtinfo[i].y1) - yinfo.begin();
    mtinfo[i].y2 =
        find(yinfo.begin(), yinfo.end(), mtinfo[i].y2) - yinfo.begin();
  }
  W = xinfo.size();
  H = yinfo.size();
  return;
}

int main() {
  while (1) {
    xinfo.clear();
    yinfo.clear();

    scanf("%d %d", &W, &H);
    if (W == 0 && H == 0)
      break;
    scanf("%d", &N);

    compress();
    rep(i, W + 1) rep(j, H + 1) map[i][j] = false;
    rep(i, N) {
      int x1, x2, y1, y2;
      x1 = mtinfo[i].x1;
      y1 = mtinfo[i].y1;
      x2 = mtinfo[i].x2;
      y2 = mtinfo[i].y2;
      for (int x = x1; x <= x2; x++) {
        for (int y = y1; y <= y2; y++) {
          map[x][y] = true;
        }
      }
    }

    int ans = 0;
    rep(x, W - 1) {
      rep(y, H - 1) {
        if (!map[x][y]) {
          queue<pair<int, int>> q1;
          q1.push(make_pair(x, y));
          while (q1.size()) {
            pair<int, int> a = q1.front();
            q1.pop();
            int xx = a.first;
            int yy = a.second;
            map[xx][yy] = true;
            for (int i = -1; i < 2; i++) {
              for (int j = -1; j < 2; j++) {
                if (abs(i + j) > 1)
                  continue;
                if (xx + i < 0 || xx + i > W || yy + j < 0 || yy + j > H)
                  continue;
                if (!map[xx + i][yy + j])
                  q1.push(make_pair(xx + i, yy + j));
              }
            }
          }
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}