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

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int N, M, K, Q;
	cin >> N >> M >> K >> Q;
	int tot_cnt = 0;
	int tot_support = 0;
	int tot_ones = 0;

	map<pair<int,int>, int> block;
	map<pair<int,int>, int> nsupport;
	map<pair<int,int>, int> state;

	map<int, map<int,int> > f1, f2;
	map<int, set<int>> f1_seen, f2_seen;
	int INF = 1e9;
	auto set_val_real = [&](map<int, int>& f, int x, int st) {
		if(f.empty()){
			f[-INF] = 0;
			f[INF] = 0;
		}
		auto it = f.lower_bound(x);
		if(it->first == x){
			int v = it->second;
			if(v == st) return;
			auto [px, pv] = *prev(it);
			auto [nx, nv] = *next(it);
			tot_ones -= (x - px - 1) * int(v == 2 && pv == 2);
			tot_ones -= (nx - x - 1) * int(nv == 2 && v == 2);
			if(st == 1){
				tot_ones += (nx - px - 1) * int(nv == 2 && pv == 2);
				f.erase(it);
			} else {
				v = st;
				tot_ones += (x - px - 1) * int(v == 2 && pv == 2);
				tot_ones += (nx - x - 1) * int(nv == 2 && v == 2);
				it->second = v;
			}
		} else {
			if(st == 1){
				// nothing
			} else {
				auto [px, pv] = *prev(it);
				auto [nx, nv] = *it;
				int v = st;
				tot_ones -= (nx - px - 1) * int(nv == 2 && pv == 2);
				tot_ones += (x - px - 1) * int(v == 2 && pv == 2);
				tot_ones += (nx - x - 1) * int(nv == 2 && v == 2);
				f[x] = v;
			}
		}
	};
	auto set_val = [&](map<int, int>& f, set<int>& f_seen, int x, int st) {
		if(!f_seen.count(x-1)){
			f_seen.insert(x-1);
			set_val_real(f, x-1, 0);
		}
		if(!f_seen.count(x+1)) {
			f_seen.insert(x+1);
			set_val_real(f, x+1, 0);
		}
		f_seen.insert(x);
		set_val_real(f, x, st);
		// for(auto [c, v] : f){
		// 	cerr << c << ',' << v << ' ';
		// }
		// cerr << '\n';
	};

	auto upd = [&](int x, int y, int st){
		if(state[{x, y}] == st) return;
		// cerr << "upd " << x << ' ' << y << ' ' << st << '\n';
		state[{x, y}] = st;
		set_val(f1[x+y], f1_seen[x+y], x-y, st);
		set_val(f1[x+y+1], f1_seen[x+y+1], x-y, st);
		set_val(f2[x-y], f2_seen[x-y], x+y, st);
		set_val(f2[x-y+1], f2_seen[x-y+1], x+y, st);
	};

	auto del = [&](int x, int y) -> void {
		// cerr << "del " << x << ' ' << y << '\n';
		for(int lx : {x-1, x}) for(int ly : {y-1, y}){
			bool exists = true;
			for(int ax : {lx, lx+1}) for(int ay : {ly, ly+1}){
				if(!block[{ax, ay}]) exists = false;
			}
			if(exists){
				for(int ax : {lx, lx+1}) for(int ay : {ly, ly+1}){
					nsupport[{ax, ay}] -= 1;
					tot_support -= nsupport[{ax, ay}] == 0;
				}
			}
		}
		block[{x, y}] = 0;
		tot_cnt -= block[{x, y}] == 0;
		for(int ux : {x-1, x, x+1}){
			for(int uy : {y-1, y, y+1}){
				int new_state = int(block[{ux, uy}] > 0) + int(nsupport[{ux, uy}] > 0);
				upd(ux, uy, new_state);
			}
		}
	};
	auto ins = [&](int x, int y) -> void {
		// cerr << "ins " << x << ' ' << y << '\n';
		tot_cnt += block[{x, y}] == 0;
		block[{x, y}] = 1;
		for(int lx : {x-1, x}) for(int ly : {y-1, y}){
			bool exists = true;
			for(int ax : {lx, lx+1}) for(int ay : {ly, ly+1}){
				if(!block[{ax, ay}]) exists = false;
			}
			if(exists){
				for(int ax : {lx, lx+1}) for(int ay : {ly, ly+1}){
					tot_support += nsupport[{ax, ay}] == 0;
					nsupport[{ax, ay}] += 1;
				}
			}
		}
		for(int ux : {x-1, x, x+1}){
			for(int uy : {y-1, y, y+1}){
				int new_state = int(block[{ux, uy}] > 0) + int(nsupport[{ux, uy}] > 0);
				upd(ux, uy, new_state);
			}
		}
	};
	auto cnt = [&]() -> int {
		// for(int i = 0; i < 3; i++){
		// 	for(int j = 0; j < 3; j++){
		// 		cerr << " X"[block[{i, j}]];
		// 	}
		// 	cerr << '\n';
		// }
		// cerr << tot_support << ' ' << tot_ones << '\n';
		int fixed = tot_support + tot_ones;
		return tot_cnt - fixed;
	};
	for(int i = 0; i < K; i++){
		int x, y;
		cin >> x >> y;
		x--; y--;
		if(block[{x, y}]){
			del(x, y);
		} else {
			ins(x, y);
		}
	}
	for(int q = 0; q <= Q; q++){
		int ans = cnt();
		cout << ans << '\n';
		if(q < Q){
			int x, y;
			cin >> x >> y;
			x--; y--;
			if(block[{x, y}]){
				del(x, y);
			} else {
				ins(x, y);
			}
		}
	}
}