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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2")

// #define int long long

using namespace std;

struct VertexData
{
	uint8_t poziomo;
	uint8_t pionowo;
};

struct P2
{
	uint32_t x;
	uint32_t y;
};

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, m, k, q;
	cin >> n >> m >> k >> q;
	map<pair<int, int>, int> vertex_map;
	vector<VertexData> vertex_data;
	vector<P2> vertex_pos;
	vector<vector<int>> graph(k + q);
	vector<bool> deleted(k + q, false);
	int last_i = k;
	for (int i = 0; i < k; i++)
	{
		int x, y;
		cin >> y >> x;
		vertex_map.insert({{x, y}, i});
		uint8_t poziomo = 0;
		uint8_t pionowo = 0;
		vertex_data.push_back({poziomo, pionowo});
		vertex_pos.push_back({x, y});
	}

	for (int i = 0; i < k; i++)
	{
		auto vd = vertex_pos[i];
		int x = vd.x;
		int y = vd.y;

		auto top = vertex_map.find({x, y - 1});
		if (top != vertex_map.end())
		{
			int top_v = (*top).second;

			vertex_data[top_v].pionowo++;
			vertex_data[i].pionowo++;
			graph[i].push_back(top_v);
			graph[top_v].push_back(i);
		}

		auto left = vertex_map.find({x - 1, y});
		if (left != vertex_map.end())
		{
			int left_v = (*left).second;

			vertex_data[left_v].poziomo++;
			vertex_data[i].poziomo++;
			graph[i].push_back(left_v);
			graph[left_v].push_back(i);
		}
	}

	for (int i = 0; i < q + 1; i++)
	{
		if (i != 0)
		{
			int x, y;
			cin >> y >> x;

			// znajdz sasiadow
			int a1 = -1, a2 = -1, a3 = -1, a4 = -1;
			auto top = vertex_map.find({x, y - 1});
			if (top != vertex_map.end())
			{
				a1 = (*top).second;
			}
			top = vertex_map.find({x, y + 1});
			if (top != vertex_map.end())
			{
				a2 = (*top).second;
			}
			top = vertex_map.find({x - 1, y});
			if (top != vertex_map.end())
			{
				a3 = (*top).second;
			}
			top = vertex_map.find({x + 1, y});
			if (top != vertex_map.end())
			{
				a4 = (*top).second;
			}

			// update klocka
			auto kloc = vertex_map.find({x, y});
			if (kloc == vertex_map.end())
			{
				// dodaje kloca
				vertex_map.insert({{x, y}, last_i});
				graph.push_back({});

				int poziomo = 0, pionowo = 0;
				if (a1 != -1)
				{
					graph[last_i].push_back(a1);
					graph[a1].push_back(last_i);
					pionowo++;
					vertex_data[a1].pionowo++;
				}
				if (a2 != -1)
				{
					graph[last_i].push_back(a2);
					graph[a2].push_back(last_i);

					pionowo++;
					vertex_data[a2].pionowo++;
				}
				if (a3 != -1)
				{
					graph[last_i].push_back(a3);
					graph[a3].push_back(last_i);

					poziomo++;
					vertex_data[a3].poziomo++;
				}
				if (a4 != -1)
				{
					graph[last_i].push_back(a4);
					graph[a4].push_back(last_i);

					poziomo++;
					vertex_data[a4].poziomo++;
				}
				vertex_data.push_back({poziomo, pionowo});
				vertex_pos.push_back({x, y});

				last_i++;
			}
			else
			{
				// usun klocka
				vertex_map.erase({x, y});
				int my_id = (*kloc).second;
				deleted[my_id] = true;

				if (a1 != -1)
				{
					vertex_data[a1].pionowo--;
				}
				if (a2 != -1)
				{
					vertex_data[a2].pionowo--;
				}
				if (a3 != -1)
				{
					vertex_data[a3].poziomo--;
				}
				if (a4 != -1)
				{
					vertex_data[a4].poziomo--;
				}
			}
		}

		// skan
		vector<VertexData> vertex_data_copy = vertex_data; // oof
		stack<int> s;
		for (int i = 0; i < k + q; i++)
		{
			if (i >= vertex_data.size())
				break;
			if (deleted[i])
				continue;
			if (vertex_data[i].poziomo == 0 || vertex_data[i].pionowo == 0)
			{
				s.push(i);
			}
		}

		int counter = 0;
		vector<bool> visited(k + q, false);
		while (!s.empty())
		{
			int curr = s.top();
			s.pop();

			if (visited[curr])
				continue;

			counter++;
			for (auto neigh : graph[curr])
			{
				if (deleted[neigh])
					continue;

				if (vertex_pos[curr].x == vertex_pos[neigh].x)
				{
					vertex_data_copy[neigh].pionowo--;
				}
				else
				{
					vertex_data_copy[neigh].poziomo--;
				}

				if (vertex_data_copy[neigh].poziomo == 0 || vertex_data_copy[neigh].pionowo == 0)
				{
					if (!visited[neigh])
						s.push(neigh);
				}
			}
			visited[curr] = true;
		}
		cout << counter << endl;
	}
}