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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
bool ary[6010][6010];
int x1[1010], x2[1010], yy1[1010], y2[1010];
vector<int> X, Y;
set<int> sx, sy;
stack<pair<int, int>> st;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int xmx, ymx;

int main() {
  int W, H, N, stx, sty, gox, goy, cnt;
  pair<int, int> pa;
  set<int>::iterator it;
  while (true) {
    X.clear();
    Y.clear();
    sx.clear();
    sy.clear();
    for (int i = 0; i < 6010; i++) {
      memset(ary[i], 0, sizeof(ary[i]));
    }
    cin >> W >> H;
    if (H == 0 && W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x1[i] >> yy1[i] >> x2[i] >> y2[i];
      sx.insert(x1[i]);
      if (x1[i] > 0)
        sx.insert(x1[i] - 1);
      if (x1[i] < W)
        sx.insert(x1[i] + 1);
      sx.insert(x2[i]);
      if (x2[i] > 0)
        sx.insert(x2[i] - 1);
      if (x2[i] < W)
        sx.insert(x2[i] + 1);
      sy.insert(yy1[i]);
      if (yy1[i] > 0)
        sy.insert(yy1[i] - 1);
      if (yy1[i] < H)
        sy.insert(yy1[i] + 1);
      sy.insert(y2[i]);
      if (y2[i] > 0)
        sy.insert(y2[i] - 1);
      if (y2[i] < H)
        sy.insert(y2[i] + 1);
    }
    sx.insert(0);
    sx.insert(W);
    sy.insert(0);
    sy.insert(H);
    for (it = sx.begin(); it != sx.end(); it++)
      X.push_back(*it);
    for (it = sy.begin(); it != sy.end(); it++)
      Y.push_back(*it);
    for (int i = 0; i < N; i++) {
      stx = lower_bound(X.begin(), X.end(), x1[i]) - X.begin();
      gox = lower_bound(X.begin(), X.end(), x2[i]) - X.begin();
      sty = lower_bound(Y.begin(), Y.end(), yy1[i]) - Y.begin();
      goy = lower_bound(Y.begin(), Y.end(), y2[i]) - Y.begin();
      for (int j = stx; j < gox; j++) {
        for (int k = sty; k < goy; k++) {
          ary[j][k] = true;
        }
      }
    }
    cnt = 0;
    //??¢?´¢
    xmx = lower_bound(X.begin(), X.end(), W) - X.begin();
    ymx = lower_bound(Y.begin(), Y.end(), H) - Y.begin();
    for (int i = 0; i < xmx - 1; i++) {
      for (int j = 0; j < ymx - 1; j++) {
        if (ary[i][j] == false) {
          st.push(make_pair(i, j));
          while (!st.empty()) {
            pa = st.top();
            st.pop();
            ary[pa.first][pa.second] = true;
            for (int i = 0; i < 4; i++) {
              if (pa.first + dx[i] >= 0 && pa.second + dy[i] >= 0 &&
                  pa.first + dx[i] < xmx - 1 && pa.second + dy[i] < ymx - 1 &&
                  ary[pa.first + dx[i]][pa.second + dy[i]] == false) {
                st.push(make_pair(pa.first + dx[i], pa.second + dy[i]));
              }
            }
          }
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
bool ary[2010][2010];
int x1[1010], x2[1010], yy1[1010], y2[1010];
vector<int> X, Y;
set<int> sx, sy;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int xmx, ymx;

void dfs(int x, int y) {
  ary[x][y] = true;
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] >= 0 && y + dy[i] >= 0 && x + dx[i] < xmx - 1 &&
        y + dy[i] < ymx - 1 && ary[x + dx[i]][y + dy[i]] == false) {
      dfs(x + dx[i], y + dy[i]);
    }
  }
}

int main() {
  int W, H, N, stx, sty, gox, goy, cnt;
  set<int>::iterator it;
  while (true) {
    X.clear();
    Y.clear();
    sx.clear();
    sy.clear();
    for (int i = 0; i < 2010; i++) {
      memset(ary[i], 0, sizeof(ary[i]));
    }
    cin >> W >> H;
    if (H == 0 && W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x1[i] >> yy1[i] >> x2[i] >> y2[i];
      sx.insert(x1[i]);
      if (x1[i] > 0)
        sx.insert(x1[i] - 1);
      if (x1[i] < W)
        sx.insert(x1[i] + 1);
      sx.insert(x2[i]);
      if (x2[i] > 0)
        sx.insert(x2[i] - 1);
      if (x2[i] < W)
        sx.insert(x2[i] + 1);
      sy.insert(yy1[i]);
      if (yy1[i] > 0)
        sy.insert(yy1[i] - 1);
      if (yy1[i] < H)
        sy.insert(yy1[i] + 1);
      sy.insert(y2[i]);
      if (y2[i] > 0)
        sy.insert(y2[i] - 1);
      if (y2[i] < H)
        sy.insert(y2[i] + 1);
    }
    sx.insert(0);
    sx.insert(W);
    sy.insert(0);
    sy.insert(H);
    for (it = sx.begin(); it != sx.end(); it++)
      X.push_back(*it);
    for (it = sy.begin(); it != sy.end(); it++)
      Y.push_back(*it);
    for (int i = 0; i < N; i++) {
      stx = lower_bound(X.begin(), X.end(), x1[i]) - X.begin();
      gox = lower_bound(X.begin(), X.end(), x2[i]) - X.begin();
      sty = lower_bound(Y.begin(), Y.end(), yy1[i]) - Y.begin();
      goy = lower_bound(Y.begin(), Y.end(), y2[i]) - Y.begin();
      for (int j = stx; j < gox; j++) {
        for (int k = sty; k < goy; k++) {
          ary[j][k] = true;
        }
      }
    }
    cnt = 0;
    //??¢?´¢
    xmx = lower_bound(X.begin(), X.end(), W) - X.begin();
    ymx = lower_bound(Y.begin(), Y.end(), H) - Y.begin();
    for (int i = 0; i < xmx - 1; i++) {
      for (int j = 0; j < ymx - 1; j++) {
        if (ary[i][j] == false) {
          dfs(i, j);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}