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
#include<bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)


#define VI vector<int>
#define PII pair<int,int>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define lint long long int


#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; }
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}


#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue


#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));


using namespace std;

int n,m,k,q;
const int ZDEJMOWALNY = 0;
const int KWADRAT = 1;
const int ELKA = 2;

unordered_map<int,int> nr[200005];
PII wsp[150005];
int deg[150005];
int typ[150005];

int koniec[150005];
int poczatek[150005];

int deg_dx[] = {-1,1,0,0};
int deg_dy[] = {0,0,1,-1}; 

VI elki, nelki;
int zdejmowalne;

int stopien(int x, int y) {
	int res = 0;
	for (int i = 0; i < 4; i++) {
		if (nr[x + deg_dx[i]].find(y + deg_dy[i]) != nr[x+deg_dx[i]].end()) res++;
	}
	return res;
}

bool wKwadracie(int x, int y) {
	for (int dx = -1; dx <= 1; dx +=2) {
		for (int dy = -1; dy <=1; dy+=2) {
			if (nr[x+dx].find(y) != nr[x+dx].end()) {
				if (nr[x].find(y+dy) != nr[x].end()) {
					if (nr[x+dx].find(y+dy) != nr[x+dx].end()) {
						return true;
					}
				}
			}
		}
	}
	return false;
}

bool czyElka(int x, int y) {
	if (nr[x+1].find(y)!=nr[x+1].end() && nr[x-1].find(y)!=nr[x-1].end()) return false;
	if (nr[x].find(y+1)!=nr[x].end() && nr[x].find(y-1)!=nr[x].end()) return false;
	return true;
}

void przelicz(int i) {
		deg[i] = stopien(wsp[i].st, wsp[i].nd);
		if (deg[i] >= 2 && wKwadracie(wsp[i].st, wsp[i].nd)) typ[i] = KWADRAT;
		else if (deg[i] <= 1 || deg[i] >= 3) typ[i] = ZDEJMOWALNY;
		else if (czyElka(wsp[i].st, wsp[i].nd)) typ[i] = ELKA;
		else typ[i] = ZDEJMOWALNY;
}

int nxt(int x, int y) {
	if (nr[x].find(y-1) != nr[x].end()) return nr[x][y-1]; 
	if (nr[x+1].find(y) != nr[x+1].end()) return nr[x+1][y];
	return nr[x-1][y];
}

int prv(int x, int y) {
	if (nr[x].find(y+1) != nr[x].end()) return nr[x][y+1]; 
	if (nr[x+1].find(y) != nr[x+1].end()) return nr[x+1][y];
	return nr[x-1][y];
}


void update(int x, int y, int id) {
	if (nr[x].find(y) != nr[x].end()) {
		// USUWAM	
		int kto = nr[x][y];
		//if (typ[kto] == ELKA) elki.erase(kto);
		if (typ[kto] == ZDEJMOWALNY) zdejmowalne--;
		typ[kto] = -1;
		nr[x].erase(y);

		FOR(dx,-1,2) FOR(dy,-1,2) {
			if (nr[x+dx].find(y+dy) == nr[x+dx].end()) continue;
			int ja = nr[x+dx][y+dy];

			int staryTyp = typ[ja]; 
			przelicz(ja);
			if (staryTyp == typ[ja]) continue;
			//if (staryTyp == ELKA) elki.erase(ja);
			if (staryTyp == ZDEJMOWALNY) zdejmowalne--;
			if (typ[ja] == ELKA) elki.pb(ja);
			if (typ[ja] == ZDEJMOWALNY) zdejmowalne++;
		}
	}
	else {
		// DODAJE
		nr[x][y] = id;
		wsp[id] = {x,y};
		przelicz(id);
		if (typ[id] == ELKA) elki.pb(id);
		if (typ[id] == ZDEJMOWALNY) zdejmowalne++;

		FOR(dx,-1,2) FOR(dy,-1,2) {
			if (dx == 0 && dy == 0) continue;
			if (nr[x+dx].find(y+dy) == nr[x+dx].end()) continue;
			int ja = nr[x+dx][y+dy];

			int staryTyp = typ[ja]; 
			przelicz(ja);
			if (staryTyp == typ[ja]) continue;
			//if (staryTyp == ELKA) elki.erase(ja);
			if (staryTyp == ZDEJMOWALNY) zdejmowalne--;
			if (typ[ja] == ELKA) elki.pb(ja);
			if (typ[ja] == ZDEJMOWALNY) zdejmowalne++;
		}
	}
}

int policz_koniec(int elka) {
	if (typ[elka] != ELKA) return elka;
	if (koniec[elka] != -1) return koniec[elka];
	koniec[elka] = policz_koniec(nxt(wsp[elka].st, wsp[elka].nd));
	return koniec[elka];
}


int policz_poczatek(int elka) {
	if (typ[elka] != ELKA) return elka;
	if (poczatek[elka] != -1) return poczatek[elka];
	poczatek[elka] = policz_poczatek(prv(wsp[elka].st, wsp[elka].nd));
	return poczatek[elka];
}


void solve() {
	nelki.clear();
	FORE(it, elki) {
		if (typ[*it] == ELKA) {
			nelki.pb(*it); 
			poczatek[*it] = koniec[*it] = -1;
		}
	}
	int res = 0;
	FORE(it, nelki) {
			policz_koniec(*it);
			policz_poczatek(*it);
			if (typ[poczatek[*it]] != KWADRAT || typ[koniec[*it]] != KWADRAT) res++;
	}
	elki = nelki;
	printf("%d\n", res + zdejmowalne);
}

void prepare() {
	FOR(i,0,k) {
		przelicz(i);
	}
	FOR(i,0,k) poczatek[i] = koniec[i] = -1;
	int res = 0;
	FOR(i,0,k) {
		if (typ[i] == ZDEJMOWALNY) zdejmowalne++;
		if (typ[i] == ELKA) {
			elki.pb(i);
			policz_koniec(i);
			policz_poczatek(i);
			if (typ[poczatek[i]] != KWADRAT || typ[koniec[i]] != KWADRAT) res++;
		}
	}
	printf("%d\n", zdejmowalne + res);
}

int main() {
	read4(n,m,k,q);
	FOR(i,0,k) {
		make2(x, y);
		nr[x][y] = i;
		wsp[i] = {x,y}; 
	}
	prepare();
	FOR(j,0,q) {
		make2(x, y);
		update(x,y, k+j);
		solve();
	}
}