1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n)    fwd(i, 0, n)
#define all(X)       X.begin(), X.end()
#define sz(X)        int(size(X))
#define pb           push_back
#define eb           emplace_back
#define st           first
#define nd           second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
	*(int *)0 = 0;
});
#	define DTP(x, y)                                      \
		auto operator<<(auto &o, auto a)->decltype(y, o) { \
			o << "(";                                      \
			x;                                             \
			return o << ")";                               \
		}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
	((cerr << x << ", "), ...) << '\n';
}
#	define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#	define deb(...) 0
#endif

struct NodeStats {
	// To chyba odpowiada sumom w poddrzewie (?), ale chuj by to wiedział
	// Może to mogą być inty, ale ponownie: chuj by to wiedział
	int other_num = 0, fixed_num = 0, zigzag_num = 0, neighbor_num = 0;
	bool operator!=(const NodeStats &o) const {
		return other_num != o.other_num || fixed_num != o.fixed_num ||
			   zigzag_num != o.zigzag_num || neighbor_num != o.neighbor_num;
	}
};

inline void add(NodeStats &a, const NodeStats &b) {
	a.other_num += b.other_num;
	a.fixed_num += b.fixed_num;
	a.zigzag_num += b.zigzag_num;
	a.neighbor_num += b.neighbor_num;
}

inline void subtract(NodeStats &a, const NodeStats &b) {
	a.other_num -= b.other_num;
	a.fixed_num -= b.fixed_num;
	a.zigzag_num -= b.zigzag_num;
	a.neighbor_num -= b.neighbor_num;
}

inline void set_sum(NodeStats &a, const NodeStats &b1, const NodeStats &b2) {
	a.other_num = b1.other_num + b2.other_num;
	a.fixed_num = b1.fixed_num + b2.fixed_num;
	a.zigzag_num = b1.zigzag_num + b2.zigzag_num;
	a.neighbor_num = b1.neighbor_num + b2.neighbor_num;
}

// https://judge.yosupo.jp/submission/243706
// jak to jest zbugowane to cię znajdę adam
struct Node {
	Node *p, *c[2];
	Node() {
		p = c[0] = c[1] = 0;
	}

	bool rev = 0;
	NodeStats s, v, vs;
	void push() {
		if (!rev)
			return;
		rep(i, 2) if (c[i]) c[i]->flip();
		rev = 0;
	}
	void pull() {
		set_sum(s, v, vs);
		if (c[0])
			add(s, c[0]->s);
		if (c[1])
			add(s, c[1]->s);
	}
	void flip() {
		swap(c[0], c[1]), rev ^= 1;
	}

	int up() {
		if (!p)
			return -2;
		rep(i, 2) if (p->c[i] == this) return i;
		return -1; // p is path-parent pointer
	}
	bool isRoot() {
		return up() < 0;
	}
	friend void setLink(Node *x, Node *y, int d) {
		if (y)
			y->p = x;
		if (d >= 0)
			x->c[d] = y;
	}
	void rot() {
		int x = up();
		Node *pp = p;
		setLink(pp->p, this, pp->up());
		setLink(pp, c[x ^ 1], x);
		setLink(this, pp, x ^ 1);
		pp->pull();
	}
	void fix() {
		if (!isRoot())
			p->fix();
		push();
	}
	void splay() {
		for (fix(); !isRoot();) {
			if (p->isRoot())
				rot();
			else if (up() == p->up())
				p->rot(), rot();
			else
				rot(), rot();
		}
		pull();
	}
};

struct LinkCut {
	vector<Node> t;
	LinkCut(int n) : t(n) {
	}

	void link(int u, int v) { // link u->v
		makeRoot(&t[v]);
		access(&t[u]);
		setLink(&t[v], &t[u], 0);
		t[v].pull();
	}
	void cut(int u, int v) { // cut u->v
		makeRoot(&t[u]);
		access(&t[v]);
		t[v].c[0] = t[u].p = 0;
		t[v].pull();
	}
	bool connected(int u, int v) {
		return lca(&t[u], &t[v]);
	}
	Node *lca(Node *u, Node *v) {
		if (u == v)
			return u;
		access(u);
		access(v);
		if (!u->p)
			return 0;
		u->splay();
		return u->p ?: u;
	}
	void access(Node *u) {
		for (Node *x = u, *y = 0; x; x = x->p) {
			x->splay();
			// swap virt here
			if (y)
				subtract(x->vs, y->s);
			if (x->c[1])
				add(x->vs, x->c[1]->s);
			x->c[1] = y;
			x->pull();
			y = x;
		}
		u->splay();
	}
	void makeRoot(Node *u) {
		access(u), u->flip(), u->push();
	}
};

class ComponentStatistics {
   private:
	LinkCut lct;
	int result;
	int contrib_of_stats(const NodeStats &s) {
		if (s.other_num == 1)
			return 1;
		if (s.fixed_num == 1)
			return 0;
		if (s.neighbor_num == 2)
			return 0;
		return s.zigzag_num;
	}

   public:
	ComponentStatistics(
		const vector<NodeStats> &initial_stats,
		const vector<pii> &initial_edges) :
		lct(sz(initial_stats)), result(0) {
		rep(i, sz(initial_stats)) {
			lct.t[i].v = initial_stats[i];
			lct.t[i].pull();
			result += contrib_of_stats(initial_stats[i]);
		}
		for (auto [u, v] : initial_edges) {
			add_edge(u, v);
		}
	}
	void change_stats(int v, NodeStats new_stats) {
		lct.access(&lct.t[v]);
		result -= contrib_of_stats(lct.t[v].s);
		lct.t[v].v = new_stats;
		lct.t[v].pull();
		result += contrib_of_stats(lct.t[v].s);
	}
	void add_edge(int v, int u) {
		lct.access(&lct.t[v]);
		result -= contrib_of_stats(lct.t[v].s);
		lct.access(&lct.t[u]);
		result -= contrib_of_stats(lct.t[u].s);
		lct.link(v, u);
		lct.access(&lct.t[v]);
		result += contrib_of_stats(lct.t[v].s);
	}
	void remove_edge(int v, int u) {
		lct.access(&lct.t[v]);
		result -= contrib_of_stats(lct.t[v].s);
		lct.cut(v, u);
		lct.access(&lct.t[u]);
		result += contrib_of_stats(lct.t[u].s);
		lct.access(&lct.t[v]);
		result += contrib_of_stats(lct.t[v].s);
	}
	int get_result() {
		return result;
	}
};

vector<NodeStats> cur_stats;

typedef map<pii, int> Grid;

typedef array<array<int, 3>, 3> SmallNeighborhood;
typedef array<array<int, 7>, 7> BigNeighborhood;

void populate_small_neighborhood(
	const Grid &grid, pii p, SmallNeighborhood &nb) {
	rep(i, 3) {
		auto it = grid.lower_bound({p.st - 1 + i, p.nd - 1});
		rep(j, 3) {
			if (it == grid.end()) {
				nb[i][j] = -1;
				continue;
			}
			if (it->st.st != p.st - 1 + i || it->st.nd != p.nd - 1 + j) {
				nb[i][j] = -1;
				continue;
			}
			nb[i][j] = it->nd;
			it = next(it);
		}
	}
}

void populate_big_neighborhood(const Grid &grid, pii p, BigNeighborhood &nb) {
	rep(i, 7) {
		auto it = grid.lower_bound({p.st - 3 + i, p.nd - 3});
		rep(j, 7) {
			if (it == grid.end()) {
				nb[i][j] = -1;
				continue;
			}
			if (it->st.st != p.st - 3 + i || it->st.nd != p.nd - 3 + j) {
				nb[i][j] = -1;
				continue;
			}
			nb[i][j] = it->nd;
			it = next(it);
		}
	}
}

NodeStats calc_point_stats(const Grid &grid, pii p) {
	static SmallNeighborhood nb;
	populate_small_neighborhood(grid, p, nb);
	NodeStats stats;
	// Check if inside a 2x2 square
	bool inside_square = false;
	if (nb[0][0] != -1 && nb[0][1] != -1 && nb[1][0] != -1)
		inside_square = true;
	if (nb[0][1] != -1 && nb[0][2] != -1 && nb[1][2] != -1)
		inside_square = true;
	if (nb[1][0] != -1 && nb[2][0] != -1 && nb[2][1] != -1)
		inside_square = true;
	if (nb[1][2] != -1 && nb[2][1] != -1 && nb[2][2] != -1)
		inside_square = true;
	// Is inside square so is fixed
	if (inside_square) {
		stats.fixed_num = 1;
		return stats;
	}
	int x_neighbors = (nb[0][1] != -1) + (nb[2][1] != -1);
	int y_neighbors = (nb[1][0] != -1) + (nb[1][2] != -1);
	// Is a corner -> zigzag
	if (x_neighbors == 1 && y_neighbors == 1) {
		stats.zigzag_num = 1;
		return stats;
	}
	// None of the above, so is other
	stats.other_num = 1;
	return stats;
}

typedef array<array<NodeStats, 7>, 7> BigNeighborhoodStats;

void calc_point_stats_on_neighborhood(
	const BigNeighborhood &big_nb, BigNeighborhoodStats &stats, int i, int j) {
	stats[i][j] = NodeStats();
	// Check if inside a 2x2 square
	bool inside_square = false;
	if (big_nb[i - 1][j - 1] != -1 && big_nb[i - 1][j] != -1 &&
		big_nb[i][j - 1] != -1)
		inside_square = true;
	if (big_nb[i - 1][j] != -1 && big_nb[i - 1][j + 1] != -1 &&
		big_nb[i][j + 1] != -1)
		inside_square = true;
	if (big_nb[i][j - 1] != -1 && big_nb[i + 1][j - 1] != -1 &&
		big_nb[i + 1][j] != -1)
		inside_square = true;
	if (big_nb[i][j + 1] != -1 && big_nb[i + 1][j] != -1 &&
		big_nb[i + 1][j + 1] != -1)
		inside_square = true;
	// Is inside square so is fixed
	if (inside_square) {
		stats[i][j].fixed_num = 1;
		return;
	}
	int x_neighbors = (big_nb[i - 1][j] != -1) + (big_nb[i + 1][j] != -1);
	int y_neighbors = (big_nb[i][j - 1] != -1) + (big_nb[i][j + 1] != -1);
	// Is a corner -> zigzag
	if (x_neighbors == 1 && y_neighbors == 1) {
		stats[i][j].zigzag_num = 1;
		return;
	}
	// None of the above, so is other
	stats[i][j].other_num = 1;
}

void update_point_stats_on_neighborhood(
	const BigNeighborhood &big_nb, BigNeighborhoodStats &stats, int i, int j) {
	if (stats[i][j].zigzag_num == 0)
		return;
	if (big_nb[i - 1][j] != -1 && stats[i - 1][j].fixed_num == 1) {
		stats[i][j].neighbor_num += 1;
	}
	if (big_nb[i + 1][j] != -1 && stats[i + 1][j].fixed_num == 1) {
		stats[i][j].neighbor_num += 1;
	}
	if (big_nb[i][j - 1] != -1 && stats[i][j - 1].fixed_num == 1) {
		stats[i][j].neighbor_num += 1;
	}
	if (big_nb[i][j + 1] != -1 && stats[i][j + 1].fixed_num == 1) {
		stats[i][j].neighbor_num += 1;
	}
}

void update_point_stats(const Grid &grid, pii p, NodeStats &stats) {
	if (stats.zigzag_num == 0)
		return;
	static SmallNeighborhood nb;
	populate_small_neighborhood(grid, p, nb);
	if (nb[0][1] != -1 && cur_stats[nb[0][1]].fixed_num == 1) {
		stats.neighbor_num += 1;
	}
	if (nb[1][0] != -1 && cur_stats[nb[1][0]].fixed_num == 1) {
		stats.neighbor_num += 1;
	}
	if (nb[1][2] != -1 && cur_stats[nb[1][2]].fixed_num == 1) {
		stats.neighbor_num += 1;
	}
	if (nb[2][1] != -1 && cur_stats[nb[2][1]].fixed_num == 1) {
		stats.neighbor_num += 1;
	}
}

vector<pii> get_initial_edges(Grid &grid) {
	for (auto &[p, id] : grid) {
		cur_stats[id] = calc_point_stats(grid, p);
	}
	vector<pii> edges;
	SmallNeighborhood nb;
	for (auto &[p, id] : grid) {
		update_point_stats(grid, p, cur_stats[id]);
		if (cur_stats[id].zigzag_num == 0)
			continue;
		populate_small_neighborhood(grid, p, nb);
		if (nb[0][1] != -1 && cur_stats[nb[0][1]].zigzag_num == 1) {
			edges.push_back({id, nb[0][1]});
		}
		if (nb[1][0] != -1 && cur_stats[nb[1][0]].zigzag_num == 1) {
			edges.push_back({id, nb[1][0]});
		}
	}
	return edges;
}

void modify_point(
	Grid &grid, pii p, ComponentStatistics &cs, int new_point_id) {
	static BigNeighborhood old_nb, new_nb;
	populate_big_neighborhood(grid, p, old_nb);
	static BigNeighborhoodStats old_stats, new_stats;
	rep(i, 7) rep(j, 7) {
		if (old_nb[i][j] == -1)
			continue;
		old_stats[i][j] = cur_stats[old_nb[i][j]];
	}
	new_nb = old_nb;
	if (grid.find(p) == grid.end()) {
		grid[p] = new_point_id;
		new_nb[3][3] = new_point_id;
	} else {
		grid.erase(p);
		new_nb[3][3] = -1;
	}
	rep(i, 7) rep(j, 7) {
		if (new_nb[i][j] == -1)
			continue;
		int max_from_center = max(abs(i - 3), abs(j - 3));
		if (max_from_center == 3)
			new_stats[i][j] = old_stats[i][j];
		else
			calc_point_stats_on_neighborhood(new_nb, new_stats, i, j);
	}
	fwd(i, 1, 6) fwd(j, 1, 6) {
		if (new_nb[i][j] == -1)
			continue;
		update_point_stats_on_neighborhood(new_nb, new_stats, i, j);
	}
	auto did_edge_change =
		[&](int x1, int y1, int x2, int y2) -> array<int, 3> {
		bool had_edge = false;
		if (old_nb[x1][y1] != -1 && old_nb[x2][y2] != -1) {
			if (old_stats[x1][y1].zigzag_num == 1 &&
				old_stats[x2][y2].zigzag_num == 1) {
				had_edge = true;
			}
		}
		bool has_edge = false;
		if (new_nb[x1][y1] != -1 && new_nb[x2][y2] != -1) {
			if (new_stats[x1][y1].zigzag_num == 1 &&
				new_stats[x2][y2].zigzag_num == 1) {
				has_edge = true;
			}
		}
		if (had_edge == has_edge)
			return {2, -1, -1};
		if (had_edge) {
			return {-1, old_nb[x1][y1], old_nb[x2][y2]};
		} else {
			return {1, new_nb[x1][y1], new_nb[x2][y2]};
		}
	};
	vector<array<int, 3> > edge_changes;
	fwd(i, 1, 6) fwd(j, 1, 6) {
		if (i > 1) {
			array<int, 3> change = did_edge_change(i, j, i - 1, j);
			if (change[0] != 2)
				edge_changes.push_back(change);
		}
		if (j > 1) {
			array<int, 3> change = did_edge_change(i, j, i, j - 1);
			if (change[0] != 2)
				edge_changes.push_back(change);
		}
	}
	sort(all(edge_changes));
	for (auto [change_type, u, v] : edge_changes) {
		if (change_type == -1) {
			cs.remove_edge(u, v);
		}
	}
	if (new_nb[3][3] == -1) {
		new_nb[3][3] = old_nb[3][3];
		new_stats[3][3] = NodeStats();
		new_stats[3][3].fixed_num = 1;
	}
	fwd(i, 1, 6) fwd(j, 1, 6) {
		if (new_nb[i][j] == -1)
			continue;
		if (new_stats[i][j] != cur_stats[new_nb[i][j]]) {
			cs.change_stats(new_nb[i][j], new_stats[i][j]);
			cur_stats[new_nb[i][j]] = new_stats[i][j];
		}
	}
	for (auto [change_type, u, v] : edge_changes) {
		if (change_type == 1) {
			cs.add_edge(u, v);
		}
	}
}

void sol() {
	int n, m, k, q;
	cin >> n >> m >> k >> q;
	int tot_points = k + q;
	cur_stats = vector<NodeStats>(tot_points);
	rep(i, tot_points) cur_stats[i].fixed_num = 1;
	Grid grid;
	rep(i, k) {
		int x, y;
		cin >> x >> y;
		grid[{x, y}] = i;
	}
	vector<pii> edges = get_initial_edges(grid);
	ComponentStatistics cs(cur_stats, edges);
	cout << cs.get_result() << '\n';
	rep(i, q) {
		int x, y;
		cin >> x >> y;
		modify_point(grid, {x, y}, cs, k + i);
		cout << cs.get_result() << '\n';
	}
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	cout << fixed << setprecision(10);
	sol();
#ifdef LOCF
	cout.flush();
	cerr << "- - - - - - - - -\n";
	(void)!system(
		"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
																		// ....kB
#endif
}