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
//fast
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define all(x) x.begin(),x.end()
#define rep(n) for (int i = 0 ; i<n ; i++)
#define pb push_back

const int base = 2e5+7;
set<int> ele[base];
set<int> odw[base];
set<int> removed[base];
pair<int,int> kier[4] = {{-1,0},{1,0},{0,-1},{0,1}};

bool candel(int x, int y){
	bool a1 = ele[x-1].find(y)!=ele[x-1].end();
	bool b1 = odw[x-1].find(y)!=odw[x-1].end();
	bool a2 = ele[x].find(y-1)!=ele[x].end();
	bool b2 = odw[x].find(y-1)!=odw[x].end();
	bool a3 = ele[x].find(y+1)!=ele[x].end();
	bool b3 = odw[x].find(y+1)!=odw[x].end();
	bool a4 = ele[x+1].find(y)!=ele[x+1].end();
	bool b4 = odw[x+1].find(y)!=odw[x+1].end();
	if ((!a1 || b1) && (!a4 || b4)) return 1;
	if ((!a2 || b2) && (!a3 || b3)) return 1;
	return 0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
	int n,m,k,q;
	cin >> n >> m >> k >> q;
	rep(k){
		int a,b;
		cin >> a >> b;
		ele[a].insert(b);
	}
	q++;
	while (q--){
		queue<pair<int,int>> kol;
		rep(n+2){
			odw[i].clear();
			removed[i].clear();
		}
		int ans = 0;
		for (int i = 1 ; i<=n ; i++){
			for (auto j : ele[i]){
				if (candel(i,j)){
					removed[i].insert(j);
					odw[i].insert(j);
					kol.push({i,j});
					ans++;
				}
			}
		}
		while (kol.size()){
			int x = kol.front().first;
			int y = kol.front().second;
			kol.pop();
			for (auto dir : kier){
				int a = x+dir.first;
				int b = y+dir.second;
				if ((ele[a].find(b)!=ele[a].end()) && (odw[a].find(b)==odw[a].end()) && candel(a,b)){
					removed[a].insert(b);
					odw[a].insert(b);
					kol.push({a,b});
					ans++;
				}
			}
		}
		cout << ans << '\n';
		if (q!=0){
			int a,b;
			cin >> a >> b;
			auto it = ele[a].find(b);
			k++;
			if (it!=ele[a].end()){
				ele[a].erase(b);
				k-=2;
			}else ele[a].insert(b);
		}
	}
}