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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Coor;
typedef pair<Coor, int> Pair;

int w, h;
int n;
vector<int> xs;
vector<int> ys;
vector<Pair> diff;
map<int, int> xtox;
map<int, int> ytoy;
int xfrom;
int xto;
int yfrom;
int yto;

short field[4001][4001];

char count(int x, int y) {
  char ret = 0;
  queue<Coor> q;

  q.push(Coor(x, y));
  while (!q.empty()) {
    Coor c = q.front();
    q.pop();
    x = c.first;
    y = c.second;

    if (x < xfrom || xto <= x || y < yfrom || yto <= y)
      continue;
    if (field[y][x])
      continue;

    field[y][x] = 1;
    ret = 1;
    q.push(Coor(x - 1, y));
    q.push(Coor(x + 1, y));
    q.push(Coor(x, y - 1));
    q.push(Coor(x, y + 1));
  }
  return ret;
}

int main() {
  int ans;

  while (1) {
    xs.clear();
    ys.clear();
    diff.clear();
    xtox.clear();
    ytoy.clear();
    memset(field, 0, sizeof(field));

    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      exit(0);

    scanf("%d", &n);
    xs.push_back(0);
    xs.push_back(w - 1);
    ys.push_back(0);
    ys.push_back(h - 1);
    for (int i = 0; i < n; i++) {
      int x1, x2;
      int y1, y2;

      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      xs.push_back(x1 - 1);
      xs.push_back(x1);
      xs.push_back(x2);
      xs.push_back(x2 + 1);
      ys.push_back(y1 - 1);
      ys.push_back(y1);
      ys.push_back(y2);
      ys.push_back(y2 + 1);
      diff.push_back(Pair(Coor(x1, y1), 1));
      diff.push_back(Pair(Coor(x1, y2), -1));
      diff.push_back(Pair(Coor(x2, y1), -1));
      diff.push_back(Pair(Coor(x2, y2), 1));
    }

    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());

    for (int i = 0; i < xs.size(); i++) {
      xtox[xs[i]] = i;
    }

    for (int i = 0; i < ys.size(); i++) {
      ytoy[ys[i]] = i;
    }

    for (int i = 0; i < diff.size(); i++) {
      Pair p = diff[i];
      int x = p.first.first;
      int y = p.first.second;
      int add = p.second;

      field[ytoy[y]][xtox[x]] += add;
    }

    yfrom = ytoy[0];
    yto = ytoy[h - 1];
    xfrom = xtox[0];
    xto = xtox[w - 1];
    h = ys.size();
    w = xs.size();
    for (int i = 0; i < h; i++) {
      for (int j = 1; j < w; j++) {
        field[i][j] += field[i][j - 1];
      }
    }

    for (int i = 1; i < h; i++) {
      for (int j = 0; j < w; j++) {
        field[i][j] += field[i - 1][j];
      }
    }

    ans = 0;
    for (int i = yfrom; i < yto; i++) {
      for (int j = xfrom; j < xto; j++) {
        if (count(j, i))
          ans++;
      }
    }

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

Error: 0

Fixed submission:

/*
    頭がボケているのボケ夫です
 */

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> Coor;
typedef pair<Coor, int> Pair;

int w, h;
int n;
vector<int> xs;
vector<int> ys;
vector<Pair> diff;
map<int, int> xtox;
map<int, int> ytoy;
int xfrom;
int xto;
int yfrom;
int yto;

short field[4001][4001];

char count(short x, short y) {
  if (x < xfrom || xto <= x || y < yfrom || yto <= y)
    return 0;
  if (field[y][x])
    return 0;

  field[y][x] = 1;
  count(x - 1, y);
  count(x + 1, y);
  count(x, y - 1);
  count(x, y + 1);
  return 1;
}

int main() {
  int ans;

  while (1) {
    xs.clear();
    ys.clear();
    diff.clear();
    xtox.clear();
    ytoy.clear();
    memset(field, 0, sizeof(field));

    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      exit(0);

    scanf("%d", &n);
    xs.push_back(0);
    xs.push_back(w - 1);
    ys.push_back(0);
    ys.push_back(h - 1);
    for (int i = 0; i < n; i++) {
      int x1, x2;
      int y1, y2;

      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      xs.push_back(x1 - 1);
      xs.push_back(x1);
      xs.push_back(x2);
      xs.push_back(x2 + 1);
      ys.push_back(y1 - 1);
      ys.push_back(y1);
      ys.push_back(y2);
      ys.push_back(y2 + 1);
      diff.push_back(Pair(Coor(x1, y1), 1));
      diff.push_back(Pair(Coor(x1, y2), -1));
      diff.push_back(Pair(Coor(x2, y1), -1));
      diff.push_back(Pair(Coor(x2, y2), 1));
    }

    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());

    for (int i = 0; i < xs.size(); i++) {
      xtox[xs[i]] = i;
    }

    for (int i = 0; i < ys.size(); i++) {
      ytoy[ys[i]] = i;
    }

    for (int i = 0; i < diff.size(); i++) {
      Pair p = diff[i];
      int x = p.first.first;
      int y = p.first.second;
      int add = p.second;

      field[ytoy[y]][xtox[x]] += add;
    }

    yfrom = ytoy[0];
    yto = ytoy[h - 1];
    xfrom = xtox[0];
    xto = xtox[w - 1];
    h = ys.size();
    w = xs.size();
    for (int i = 0; i < h; i++) {
      for (int j = 1; j < w; j++) {
        field[i][j] += field[i][j - 1];
      }
    }

    for (int i = 1; i < h; i++) {
      for (int j = 0; j < w; j++) {
        field[i][j] += field[i - 1][j];
      }
    }

    ans = 0;
    for (int i = yfrom; i < yto; i++) {
      for (int j = xfrom; j < xto; j++) {
        if (count((short)j, (short)i))
          ans++;
      }
    }

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