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
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using   ll         = long long;
using   vi         = vector<int>;
using   pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

void run();

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);
	run();
	cout << flush; _Exit(0);
}

mt19937 twister(191284728);

struct Node {
	// weak = has vertical and horizontal neighbour
	// strong = has vertical and horizontal weak neighbour
	// core = part of 2x2 square
	bool used = 0, weak = 0, strong = 0, core = 0;
	int parent = -1, left = -1, right = -1, size = 1, contrib = 0;
	uint32_t weight = uint32_t(twister());
};

vector<ll> keys;
vector<Node> V;
int total, stay;

ll getKey(ll x, ll y) { return (x << 32) | y; }
pii getCoords(int i) { return { int(keys[i] >> 32), int(keys[i] & 0xFFFFFFFF) }; }

int cellIndex(int x, int y) {
	ll key = getKey(x, y);
	auto it = lower_bound(all(keys), key);
	assert(it != keys.end() && *it == key);
	return int(it - keys.begin());
}

int root(int i) {
	while (V[i].parent != -1) i = V[i].parent;
	return i;
}

void updateStay(int i) {
	int l = i, r = i;
	while (V[l].left != -1) l = V[l].left;
	while (V[r].right != -1) r = V[r].right;
	int cores = V[l].core + V[r].core;
	V[i].contrib = (cores == 2 ? V[i].size : cores);
	stay += V[i].contrib;
}

void updateSize(int i) {
	V[i].size = 1;
	if (V[i].left != -1) V[i].size += V[V[i].left].size;
	if (V[i].right != -1) V[i].size += V[V[i].right].size;
}

int join(int l, int r, int p) {
	if (l == -1) {
		if (r != -1) V[r].parent = p;
		return r;
	} else if (r == -1) {
		V[l].parent = p;
		return l;
	} else if (V[l].weight < V[r].weight) {
		V[l].right = join(V[l].right, r, l);
		V[l].parent = p;
		updateSize(l);
		return l;
	} else {
		V[r].left = join(l, V[r].left, r);
		V[r].parent = p;
		updateSize(r);
		return r;
	}
}

void updateEdge(int i, int j, bool dir) {
	if (!V[i].strong || !V[j].strong) return;
	if (V[i].core && V[j].core) return;
	if (dir) swap(i, j);
	i = root(i);
	j = root(j);
	stay -= V[i].contrib + V[j].contrib;
	updateStay(join(i, j, -1));
}

void tearOut(int t) {
	int l = V[t].left, r = V[t].right, i = t;
	while (V[i].parent != -1) {
		int p = V[i].parent;
		if (V[p].left == i) {
			V[p].left = r;
			if (r != -1) V[r].parent = p;
			r = p;
		} else {
			V[p].right = l;
			if (l != -1) V[l].parent = p;
			l = p;
		}
		updateSize(p);
		i = p;
	}
	stay -= V[i].contrib;
	V[t].parent = V[t].left = V[t].right = -1;
	V[t].size = 1;
	updateStay(t);
	if (l != -1) {
		V[l].parent = -1;
		updateStay(l);
	}
	if (r != -1) {
		V[r].parent = -1;
		updateStay(r);
	}
}

void flipCell(int x, int y) {
	int ind[5];
	rep(i, 0, 5) ind[i] = cellIndex(x-2+i, y-2);

	total += 1 - V[ind[2]+2].used*2;
	V[ind[2]+2].used = !V[ind[2]+2].used;

	rep(dx, 1, 4) rep(dy, 1, 4) {
		Node &cur = V[ind[dx]+dy];
		cur.weak = cur.strong = cur.core = 0;
		if (cur.used) {
			cur.weak = (V[ind[dx-1]+dy].used || V[ind[dx+1]+dy].used) && (V[ind[dx]+dy-1].used || V[ind[dx]+dy+1].used);
			for (int s : {-1, 1}) {
				for (int t : {-1, 1}) {
					cur.core = cur.core || (V[ind[dx+s]+dy+t].used && V[ind[dx+s]+dy].used && V[ind[dx]+dy+t].used);
				}
			}
		}
	}

	rep(dx, 1, 4) rep(dy, 1, 4) {
		Node &cur = V[ind[dx]+dy];
		if (cur.weak) {
			cur.strong = (V[ind[dx-1]+dy].weak || V[ind[dx+1]+dy].weak) && (V[ind[dx]+dy-1].weak || V[ind[dx]+dy+1].weak);
		}
	}

	rep(dx, 1, 4) rep(dy, 1, 4) {
		tearOut(ind[dx]+dy);
	}

	rep(dx, 0, 4) rep(dy, 1, 4) {
		bool dir = (x+y+dx+dy) % 2;
		updateEdge(ind[dx]+dy, ind[dx+1]+dy, dir);
	}

	rep(dx, 1, 4) rep(dy, 0, 4) {
		bool dir = (x+y+dx+dy+1) % 2;
		updateEdge(ind[dx]+dy, ind[dx]+dy+1, dir);
	}
}

void readCells(vector<pii>& vec) {
	each(p, vec) {
		cin >> p.x >> p.y;
		p.x += 100;
		p.y += 100;
		rep(i, -2, 3) rep(j, -2, 3) {
			keys.pb(getKey(p.x+i, p.y+j));
		}
	}
}

void run() {
	int n, m, k, q;
	cin >> n >> m >> k >> q;

	vector<pii> initial(k), updates(q);
	readCells(initial);
	readCells(updates);

	sort(all(keys));
	keys.erase(unique(all(keys)), keys.end());
	V.resize(sz(keys));

	each(p, initial) flipCell(p.x, p.y);
	cout << total-stay << '\n';
	each(p, updates) {
		flipCell(p.x, p.y);
		cout << total-stay << '\n';
	}
}