#include #ifdef LOCAL #include "./debug.cpp" #else #define debug(...) #define print_line #endif using namespace std; using ll = long long; struct disjoint_set_union { disjoint_set_union(int n) { par = vector(n); sz = vector(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } par[x] = find(par[x]); return par[x]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (sz[x] < sz[y]) { swap(x, y); } par[y] = x; sz[x] += sz[y]; } int size(int x) { return sz[find(x)]; } bool is_united(int x, int y) { return find(x) == find(y); } private: vector par, sz; }; int main() { int H, W, N, D; cin >> H >> W >> N >> D; vector X(N), Y(N); for (int i = 0; i < N; i++) { cin >> X[i] >> Y[i]; X[i]--; Y[i]--; } vector> A(H, vector(W, -1)); for (int i = 0; i < N; i++) { A[X[i]][Y[i]] = i; } disjoint_set_union dsu(N); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (A[i][j] == -1) { continue; } for (int i2 = max(0, i - D); i2 <= min(H - 1, i + D); i2++) { for (int j2 = max(0, j - D); j2 <= min(W - 1, j + D); j2++) { if (abs(i - i2) + abs(j - j2) > D) { continue; } if (A[i2][j2] == -1) { continue; } dsu.unite(A[i][j], A[i2][j2]); } } } } int groupnum = 0; for (int i = 0; i < N; i++) { if (dsu.find(i) == i && dsu.size(i) > 1) { groupnum++; } } debug(groupnum); int ansmin = groupnum, ansmax = groupnum; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (A[i][j] != -1) { continue; } set idxs; bool flag = true, flag2 = false; for (int i2 = max(0, i - D); i2 <= min(H - 1, i + D); i2++) { for (int j2 = max(0, j - D); j2 <= min(W - 1, j + D); j2++) { if (abs(i - i2) + abs(j - j2) > D) { continue; } if (A[i2][j2] == -1) { continue; } if (dsu.size(A[i2][j2]) > 1) { flag = false; idxs.insert(dsu.find(A[i2][j2])); } else { flag2 = true; continue; } } } if (flag && !flag2) { continue; } if (flag && flag2) { ansmax = max(ansmax, groupnum + 1); } else { ansmin = min(ansmin, groupnum - (int)idxs.size() + 1); ansmax = max(ansmax, groupnum - (int)idxs.size() + 1); } } } cout << ansmin << " " << ansmax << endl; }