#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; const int inf=1<<30; const ll INF=1LL<<62; using P = pair; typedef pair PP; const ll MOD=998244353; const int MAXN=100000; class UnionFind{ public: std::vector par; std::vector rank; std::vector sz;//sz[i]で,頂点iが含まれているグループのサイズ std::vector root; UnionFind(int size){ rank=std::vector(size+1,0); par = std::vector(size+1,0); std::iota(par.begin(),par.end(),0);//#include sz = std::vector(size+1,1); root=std::vector(size+1); std::iota(root.begin(),root.end(),0); } ~UnionFind(){ std::vector().swap(rank); std::vector().swap(par); std::vector().swap(sz); std::vector().swap(root); } int find(int x){ if(par[x]==x)return x; else return par[x] = find(par[x]); } bool issame(int u,int v){ return find(u)==find(v); } void merge(int u,int v){ u = find(u); v = find(v); if(u==v) return; if(rank[u]=rank[v] par[v]=u; sz[u]+=sz[v]; if(rank[u]==rank[v])rank[u]++; } int size(int u){//頂点uが属すグループの大きさを表す. u=find(u); return sz[u]; } std::map> element(){ std::map> mp; for(int i=0;i>H>>W>>N>>dist; vector>pos(H,vector(W,-1)); vector

XY(N); for(int i=0;i>XY[i].first>>XY[i].second; XY[i].first--; XY[i].second--; pos[XY[i].first][XY[i].second]=i; } vector

B; for(int dx=-dist;dx<=dist;dx++){ for(int dy=-dist;dy<=dist;dy++){ int c=abs(dx)+abs(dy); if(c>dist)continue; B.push_back({dx,dy}); } } UnionFind uf(H*W); for(int i=0;i D,E; vector F(H*W); for(int i=0;i ans; for(int i=0;i DD,EE; for(auto [dx,dy]:B){ int nx=i+dx; int ny=j+dy; if(0<=nx && nx