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

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int h, w, n;
int ww, hh;
vector<pair<pair<int, int>, pair<int, int>>> tp;
map<int, int> xx;
map<int, int> yy;

bool **d;

bool **newd(int w, int h) {
  bool **r = new bool *[w];
  for (int i = 0; i < w; i++) {
    r[i] = new bool[h];
  }
  return r;
}

void deld(bool **r, int w, int h) {
  for (int i = 0; i < w; i++) {
    delete[] r[i];
  }
  delete[] r;
}

void paint(int x, int y, int w, int h, bool c) {
  // printf("paint:(%d,%d),%dx%d\\n",x,y,w,h);
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      // printf("%d+%d,%d+%d\\n",x,i,y,j);
      d[x + i][y + j] = c;
    }
  }
}
void paint(pair<pair<int, int>, pair<int, int>> p, bool c) {
  int x, y, w, h;
  x = xx[p.first.first];
  y = yy[p.first.second];
  w = xx[p.second.first] - x;
  h = yy[p.second.second] - y;
  paint(x, y, w, h, c);
}

void solve(int x, int y) {
  queue<pair<int, int>> que;
  que.push(pair<int, int>(x, y));
  pair<int, int> v[4] = {pair<int, int>(1, 0), pair<int, int>(-1, 0),
                         pair<int, int>(0, 1), pair<int, int>(0, -1)};
  pair<int, int> p, pp;
  while (que.size() > 0) {
    p = que.front();
    // printf("%d,%d\\n",p.first,p.second);
    que.pop();
    for (int i = 0; i < 4; i++) {
      pp.first = p.first + v[i].first;
      pp.second = p.second + v[i].second;
      if (pp.first < 0 || pp.first >= w || pp.second < 0 || pp.second >= h) {
        continue;
      }
      if (d[pp.first][pp.second] == false) {
        d[pp.first][pp.second] = true;
        que.push(pp);
      }
    }
  }
}

void dep() {
  for (int j = 0; j < h; j++) {
    for (int i = 0; i < w; i++) {
      if (d[i][j] == false) {
        printf(".");
      } else {
        printf("#");
      }
    }
    printf("\\n");
  }
  printf("\\n");
}

int main() {
  int a, b, c, dd;
  while (scanf("%d %d", &ww, &hh) && (ww || hh)) {
    tp.clear();
    scanf("%d", &n);
    vector<int> vx;
    vector<int> vy;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &a, &b, &c, &dd);
      tp.push_back(pair<pair<int, int>, pair<int, int>>(pair<int, int>(a, b),
                                                        pair<int, int>(c, dd)));
      vx.push_back(a);
      vx.push_back(c);
      vy.push_back(b);
      vy.push_back(dd);
    }
    vx.push_back(0);
    vy.push_back(0);
    vx.push_back(ww);
    vy.push_back(hh);
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());
    xx.clear();
    yy.clear();
    for (int i = 0; i < vx.size(); i++) {
      // printf("x %d:%d\\n",vx[i],i);
      xx[vx[i]] = i;
    }
    for (int i = 0; i < vy.size(); i++) {
      // printf("y %d:%d\\n",vy[i],i);
      yy[vy[i]] = i;
    }
    w = xx[ww];
    h = yy[hh];

    d = newd(w, h);
    // printf("%dx%d\\n",w,h);
    paint(0, 0, w, h, false);

    for (int i = 0; i < n; i++) {
      paint(tp[i], true);
    }
    // puts("A");
    int ans = 0;
    // dep();
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        if (d[i][j] == false) {
          solve(i, j);
          ans++;
          // dep();
        }
      }
    }
    printf("%d\\n", ans);
    deld(d, w, h);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

int h, w, n;
int ww, hh;
vector<pair<pair<int, int>, pair<int, int>>> tp;
map<int, int> xx;
map<int, int> yy;

bool **d;

bool **newd(int w, int h) {
  bool **r = new bool *[w];
  for (int i = 0; i < w; i++) {
    r[i] = new bool[h];
  }
  return r;
}

void deld(bool **r, int w, int h) {
  for (int i = 0; i < w; i++) {
    delete[] r[i];
  }
  delete[] r;
}

void paint(int x, int y, int w, int h, bool c) {
  // printf("paint:(%d,%d),%dx%d\\n",x,y,w,h);
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      // printf("%d+%d,%d+%d\\n",x,i,y,j);
      d[x + i][y + j] = c;
    }
  }
}
void paint(pair<pair<int, int>, pair<int, int>> p, bool c) {
  int x, y, w, h;
  x = xx[p.first.first];
  y = yy[p.first.second];
  w = xx[p.second.first] - x;
  h = yy[p.second.second] - y;
  paint(x, y, w, h, c);
}

void solve(int x, int y) {
  queue<pair<int, int>> que;
  que.push(pair<int, int>(x, y));
  pair<int, int> v[4] = {pair<int, int>(1, 0), pair<int, int>(-1, 0),
                         pair<int, int>(0, 1), pair<int, int>(0, -1)};
  pair<int, int> p, pp;
  while (que.size() > 0) {
    p = que.front();
    // printf("%d,%d\\n",p.first,p.second);
    que.pop();
    for (int i = 0; i < 4; i++) {
      pp.first = p.first + v[i].first;
      pp.second = p.second + v[i].second;
      if (pp.first < 0 || pp.first >= w || pp.second < 0 || pp.second >= h) {
        continue;
      }
      if (d[pp.first][pp.second] == false) {
        d[pp.first][pp.second] = true;
        que.push(pp);
      }
    }
  }
}

void dep() {
  for (int j = 0; j < h; j++) {
    for (int i = 0; i < w; i++) {
      if (d[i][j] == false) {
        printf(".");
      } else {
        printf("#");
      }
    }
    printf("\\n");
  }
  printf("\\n");
}

int main() {
  int a, b, c, dd;
  while (scanf("%d %d", &ww, &hh) && (ww || hh)) {
    tp.clear();
    scanf("%d", &n);
    vector<int> vx;
    vector<int> vy;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d %d", &a, &b, &c, &dd);
      tp.push_back(pair<pair<int, int>, pair<int, int>>(pair<int, int>(a, b),
                                                        pair<int, int>(c, dd)));
      vx.push_back(a);
      vx.push_back(c);
      vy.push_back(b);
      vy.push_back(dd);
    }
    vx.push_back(0);
    vy.push_back(0);
    vx.push_back(ww);
    vx.push_back(hh);
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());
    xx.clear();
    yy.clear();
    for (int i = 0; i < vx.size(); i++) {
      xx[vx[i]] = i;
    }
    for (int i = 0; i < vy.size(); i++) {
      yy[vy[i]] = i;
    }
    w = xx[ww];
    h = yy[hh];

    d = newd(w, h);
    // printf("%dx%d\\n",w,h);
    paint(0, 0, w, h, false);

    for (int i = 0; i < n; i++) {
      paint(tp[i], true);
    }
    // puts("A");
    int ans = 0;
    // dep();
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        if (d[i][j] == false) {
          solve(i, j);
          ans++;
          // dep();
        }
      }
    }
    printf("%d\\n", ans);
    deld(d, w, h);
  }
}