/* -*- coding: utf-8 -*- * * 2696.cc: No.2696 Sign Creation - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_HW = 100000; const int MAX_N = MAX_HW; const int INF = 1 << 30; /* typedef */ typedef set si; struct UFT { vector links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ int ps[MAX_N], es[MAX_HW], gs[MAX_N], szs[MAX_N]; UFT uft; /* subroutines */ /* main */ int main() { int h, w, n, d; scanf("%d%d%d%d", &h, &w, &n, &d); int hw = h * w; fill(es, es + hw, -1); for (int i = 0; i < n; i++) { int xi, yi; scanf("%d%d", &xi, &yi); xi--, yi--; ps[i] = xi * w + yi; es[ps[i]] = i; } uft.init(n); for (int i = 0; i < n; i++) { int xi = ps[i] / w, yi = ps[i] % w; for (int dx = -d; dx <= d; dx++) { int vx = xi + dx, d1 = d - abs(dx); if (vx >= 0 && vx < h) for (int dy = -d1; dy <= d1; dy++) { int vy = yi + dy, v = vx * w + vy; if (vy >= 0 && vy < w && es[v] >= 0) uft.merge(i, es[v]); } } } fill(gs, gs + n, -1); int gn = 0; for (int i = 0; i < n; i++) { int ri = uft.root(i); if (gs[ri] < 0) gs[ri] = gn++, szs[gs[ri]] = uft.size(ri); gs[i] = es[ps[i]] = gs[ri]; } //for (int i = 0, p = 0; i < h; i++) { //for (int j = 0; j < w; j++, p++) //printf("%2d", (es[p] >= 0) ? es[p] : -1); //putchar('\n'); //} int t0 = 0; for (int i = 0; i < gn; i++) if (szs[i] >= 2) t0++; //printf(" t0=%d\n", t0); int mint = INF, maxt = -1; for (int u = 0; u < hw; u++) if (es[u] < 0) { int ux = u / w, uy = u % w; si qs; for (int dx = -d; dx <= d; dx++) { int vx = ux + dx, d1 = d - abs(dx); if (vx >= 0 && vx < h) for (int dy = -d1; dy <= d1; dy++) { int vy = uy + dy, v = vx * w + vy; if (vy >= 0 && vy < w && es[v] >= 0) qs.insert(es[v]); } } int x1 = 1, x2 = 0; for (auto e: qs) { if (szs[e] == 1) x1++; else x2++; } int t = t0; if (x1 > 1 && x2 == 0) t++; else if (x2 > 1) t -= x2 - 1; mint = min(mint, t); maxt = max(maxt, t); } printf("%d %d\n", mint, maxt); return 0; }