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
#include <cstdio>
#include <cstdint>
#include <vector>
#include <set>
#include <queue>

using namespace std;

const int64_t M = 1000000;

int main () {
	int n, m, k, q;
	scanf("%d %d %d %d", &n, &m, &k, &q);
	set<int64_t> board;
	for (int i = 0; i < k; ++i) {
		int64_t x, y;
		scanf("%lld %lld", &x, &y);
		board.insert(x * M + y);
	}
	for (int i = 0; i <= q; ++i) {
		if (i) {
			int64_t x, y;
			scanf("%lld %lld", &x, &y);
			int64_t key = x * M + y;
			if (board.contains(key)) {
				board.erase(key);
			}
			else {
				board.insert(key);
			}
		}
		set<int64_t> remaining = board;
		queue<int64_t> q;
		for (set<int64_t>::iterator it = remaining.begin(); it != remaining.end(); it++) {
			q.push(*it);
		}
		int cnt = 0;
		while (!q.empty()) {
			int64_t item = q.front();
			q.pop();
			if (!remaining.contains(item)) {
				continue;
			}
			int64_t x = item / M;
			int64_t y = item % M;
			int64_t key_l = (x - 1) * M + y;
			int64_t key_r = (x + 1) * M + y;
			int64_t key_t = x * M + y + 1;
			int64_t key_b = x * M + y - 1;
			if ((remaining.contains(key_l) || remaining.contains(key_r)) && (remaining.contains(key_t) || remaining.contains(key_b))) {
				continue;
			}
			remaining.erase(item);
			++cnt;
			q.push(key_l);
			q.push(key_r);
			q.push(key_t);
			q.push(key_b);
		}
		printf("%d\n", cnt);
	}
	return 0;
}