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

#include <bits/stdc++.h> // {{{
// clang-format off
#define ARG4(_1, _2, _3, _4, ...) _4
#define rep(...) ARG4(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define REP(i, a) FOR(i, 0, a)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(...) ARG4(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define RREP(i, a) RFOR(i, 0, a)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (int)(a); --i)
#define ALL(c) (c).begin(), (c).end()
#define TEN(n) ((ll)(1e##n))
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define USE1(T) template <typename T> inline
#define USE2(T, U) template <typename T, typename U> inline
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
 
template <typename T> using duo = std::pair<T, T>;
template <typename T> using vec = std::vector<T>;
using ll = long long;
using pii = duo<int>;
USE2(T,U)bool chmax(T&x,U a){return x<a&&(x=a,1);}
USE2(T,U)bool chmin(T&x,U a){return a<x&&(x=a,1);}
USE1(T=int)T in(){T x;std::cin>>x;return x;}
USE1(T=int)vec<T>in(int n){vec<T>v;v.reserve(n);rep(i,n)v.pb(in<T>());return v;}
USE1(T)vec<std::pair<T,int>>zipi(const vec<T>&x,int s=0){int N=x.size();vec<std::pair<T,int>>v;v.reserve(N);rep(i,N)v.pb(x[i],s+i);return v;}
USE1(T)vec<T>ndvec(T v,int n){return vec<T>(n,v);}
USE2(T,...Ts)auto ndvec(T v,int n,Ts...ns)->vec<decltype(ndvec(v,ns...))>{return ndvec(ndvec(v,ns...),n);}
USE1(T)void pr(T x){std::cout<<x<<'\\n';}
USE2(T,...Ts)void pr(T x,Ts...xs){std::cout<<x<<' ';pr(xs...);}
USE1(T=int)T rd(){T x=0,m=0,k;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||'9'<k)break;x=x*10+k-'0';}return x;}
USE1(T=int)void wr(T x,char c='\\n'){int s=0,m=0;char b[32];if(x<0)m=1,x=-x;for(;x;x/=10)b[s++]=x%10;if(!s)b[s++]=0;if(m)mypc('-');for(;s--;)mypc(b[s]+'0');mypc(c);}
// clang-format on
// }}}
struct IoSetup { // {{{
  IoSetup() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.precision(10);
    std::cerr.precision(10);
  }
} iosetup; //}}}
using namespace std;
const int inf = 1001001001;
const ll infl = 1001001001001001001ll;
const int dd[] = {0, 1, 0, -1, 0};

bool vis[2020][2020];
int ZW, ZH;

template <typename T> void compress(vec<T> &v, map<T, int> &zip) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
  rep(i, v.size()) { zip[v[i]] = i; }
}

struct Rect {
  int sx, sy, ex, ey;
  Rect() {}
  Rect(int sx, int sy, int ex, int ey) : sx(sx), sy(sy), ex(ex), ey(ey) {}
  friend istream &operator>>(istream &is, Rect &rhs) {
    return is >> rhs.sx >> rhs.sy >> rhs.ex >> rhs.ey;
  }
};

int dfs(int x, int y) {
  if (x < 0 || ZW <= x || y < 0 || ZH <= y)
    return 0;
  if (vis[y][x])
    return 0;
  vis[y][x] = true;
  rep(d, 4) { dfs(x + dd[d], y + dd[d + 1]); }
  return 1;
}

signed main() {
  int W, H;
  vec<int> xs, ys;
  vec<Rect> rect;
  map<int, int> zip_x, zip_y;

  while (W = in(), H = in(), W || H) {
    rect.clear();
    xs.clear();
    ys.clear();
    zip_x.clear();
    zip_y.clear();

    int N = in();
    rep(i, N) { rect.pb(in<Rect>()); }
    xs.pb(0);
    xs.pb(W);
    ys.pb(0);
    ys.pb(H);
    for (auto &&r : rect) {
      xs.pb(r.sx);
      xs.pb(r.ex);
      ys.pb(r.sy);
      ys.pb(r.ey);
    }
    compress(xs, zip_x);
    compress(ys, zip_y);
    ZW = xs.size() - 1;
    ZH = ys.size() - 1;

    memset(vis, 0, sizeof(vis));
    for (auto &&r : rect) {
      int zsx = zip_x[r.sx];
      int zex = zip_x[r.ex];
      int zsy = zip_y[r.sy];
      int zey = zip_y[r.ey];
      rep(y, zsy, zey) {
        rep(x, zsx, zex) { vis[y][x] = true; }
      }
    }
    int ans = 0;
    rep(y, ZH) {
      rep(x, ZW) {
        if (dfs(x, y))
          ans++;
      }
    }
    pr(ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h> // {{{
// clang-format off
#define ARG4(_1, _2, _3, _4, ...) _4
#define rep(...) ARG4(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define REP(i, a) FOR(i, 0, a)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(...) ARG4(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define RREP(i, a) RFOR(i, 0, a)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (int)(a); --i)
#define ALL(c) (c).begin(), (c).end()
#define TEN(n) ((ll)(1e##n))
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define USE1(T) template <typename T> inline
#define USE2(T, U) template <typename T, typename U> inline
#define mygc(c) (c) = getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
 
template <typename T> using duo = std::pair<T, T>;
template <typename T> using vec = std::vector<T>;
using ll = long long;
using pii = duo<int>;
USE2(T,U)bool chmax(T&x,U a){return x<a&&(x=a,1);}
USE2(T,U)bool chmin(T&x,U a){return a<x&&(x=a,1);}
USE1(T=int)T in(){T x;std::cin>>x;return x;}
USE1(T=int)vec<T>in(int n){vec<T>v;v.reserve(n);rep(i,n)v.pb(in<T>());return v;}
USE1(T)vec<std::pair<T,int>>zipi(const vec<T>&x,int s=0){int N=x.size();vec<std::pair<T,int>>v;v.reserve(N);rep(i,N)v.pb(x[i],s+i);return v;}
USE1(T)vec<T>ndvec(T v,int n){return vec<T>(n,v);}
USE2(T,...Ts)auto ndvec(T v,int n,Ts...ns)->vec<decltype(ndvec(v,ns...))>{return ndvec(ndvec(v,ns...),n);}
USE1(T)void pr(T x){std::cout<<x<<'\\n';}
USE2(T,...Ts)void pr(T x,Ts...xs){std::cout<<x<<' ';pr(xs...);}
USE1(T=int)T rd(){T x=0,m=0,k;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||'9'<k)break;x=x*10+k-'0';}return x;}
USE1(T=int)void wr(T x,char c='\\n'){int s=0,m=0;char b[32];if(x<0)m=1,x=-x;for(;x;x/=10)b[s++]=x%10;if(!s)b[s++]=0;if(m)mypc('-');for(;s--;)mypc(b[s]+'0');mypc(c);}
// clang-format on
// }}}
struct IoSetup { // {{{
  IoSetup() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.precision(10);
    std::cerr.precision(10);
  }
} iosetup; //}}}
using namespace std;
const int inf = 1001001001;
const ll infl = 1001001001001001001ll;
const int dd[] = {0, 1, 0, -1, 0};

bool vis[4040][4040];
int ZW, ZH;

template <typename T> void compress(vec<T> &v, map<T, int> &zip) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
  rep(i, v.size()) { zip[v[i]] = i; }
}

struct Rect {
  int sx, sy, ex, ey;
  Rect() {}
  Rect(int sx, int sy, int ex, int ey) : sx(sx), sy(sy), ex(ex), ey(ey) {}
  friend istream &operator>>(istream &is, Rect &rhs) {
    return is >> rhs.sx >> rhs.sy >> rhs.ex >> rhs.ey;
  }
};

int dfs(int x, int y) {
  if (x < 0 || ZW <= x || y < 0 || ZH <= y)
    return 0;
  if (vis[y][x])
    return 0;
  vis[y][x] = true;
  rep(d, 4) { dfs(x + dd[d], y + dd[d + 1]); }
  return 1;
}

signed main() {
  int W, H;
  vec<int> xs, ys;
  vec<Rect> rect;
  map<int, int> zip_x, zip_y;

  while (W = in(), H = in(), W || H) {
    rect.clear();
    xs.clear();
    ys.clear();
    zip_x.clear();
    zip_y.clear();

    int N = in();
    rep(i, N) { rect.pb(in<Rect>()); }
    xs.pb(0);
    xs.pb(W);
    ys.pb(0);
    ys.pb(H);
    for (auto &&r : rect) {
      xs.pb(r.sx);
      xs.pb(max(r.sx - 1, 0));
      xs.pb(r.ex);
      xs.pb(min(r.ex + 1, W));
      ys.pb(r.sy);
      ys.pb(max(r.sy - 1, 0));
      ys.pb(r.ey);
      ys.pb(min(r.ey + 1, H));
    }
    compress(xs, zip_x);
    compress(ys, zip_y);
    ZW = xs.size() - 1;
    ZH = ys.size() - 1;

    memset(vis, 0, sizeof(vis));
    for (auto &&r : rect) {
      int zsx = zip_x[r.sx];
      int zex = zip_x[r.ex];
      int zsy = zip_y[r.sy];
      int zey = zip_y[r.ey];
      rep(y, zsy, zey) {
        rep(x, zsx, zex) { vis[y][x] = true; }
      }
    }
    int ans = 0;
    rep(y, ZH) {
      rep(x, ZW) {
        if (dfs(x, y))
          ans++;
      }
    }
    pr(ans);
  }
  return 0;
}