#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #include #include #include using namespace std; struct UnionFind { private: int n; vector par,siz; public: UnionFind(int n) :n(n),par(n,-1),siz(n,1) {} int root(int u) { assert(0 <= u && u < n); return (par[u] < 0 ? u:par[u] = root(par[u])); } bool same(int u,int v) { assert(0 <= u && u < n && 0 <= v && v < n); return root(u) == root(v); } bool unite(int u,int v) { assert(0 <= u && u < n && 0 <= v && v < n); u = root(u),v = root(v); if(u == v) return false; if(siz[u] < siz[v]) swap(u,v); siz[u] += siz[v]; par[v] = u; return true; } int size(int u) { assert(0 <= u && u < n); return siz[root(u)]; } vector> components() { vector> ret(n); for(int u = 0;u < n;u++) ret[root(u)].push_back(u); ret.erase(remove_if(ret.begin(),ret.end(),[](vector v) { return v.empty();}),ret.end()); return ret; } }; void Main() { int H,W,N,D; cin >> H >> W >> N >> D; vector> A(H,vector(W,-1)); for(int i = 0;i < N;i++) { int x,y; cin >> x >> y; x--; y--; A[x][y] = i; } UnionFind uf(N); for(int i = 0;i < H;i++) { for(int j = 0;j < W;j++) { if(A[i][j] == -1) { continue; } for(int ni = i - D;ni <= i + D;ni++) { for(int nj = j - D;nj <= j + D;nj++) { if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1) { continue; } uf.unite(A[i][j],A[ni][nj]); } } } } int cnt = 0; for(int i = 0;i < N;i++) { if(i == uf.root(i) && uf.size(i) >= 2) { cnt++; } } int mn = cnt,mx = cnt; vector seen(N); for(int i = 0;i < H;i++) { for(int j = 0;j < W;j++) { if(A[i][j] != -1) { continue; } int c[2]{}; for(int ni = i - D;ni <= i + D;ni++) { for(int nj = j - D;nj <= j + D;nj++) { if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1) { continue; } int id = uf.root(A[ni][nj]); if(!seen[id]) { seen[id] = 1; if(uf.size(id) == 1) { c[0]++; } else { c[1]++; } } } } int cur = cnt; if(c[0] == 0) { if(c[1] != 0) { cur -= c[1] - 1; } } else { cur++; if(c[1] != 0) { cur -= c[1] - 1; } } mn = min(mn,cur); mx = max(mx,cur); for(int ni = i - D;ni <= i + D;ni++) { for(int nj = j - D;nj <= j + D;nj++) { if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1) { continue; } int id = uf.root(A[ni][nj]); seen[id] = 0; } } } } cout << mn << " " << mx << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; /* cin >> tt; */ while(tt--) Main(); }