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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // XIII LO Szczecin
using LL = long long;    // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates

// liczę na słabe testy
struct SzybkaHeura { 
	struct SumTree {
		int sz = 1;
		vector<pair<int, LL>> t;

		SumTree(vector<int> init_values) {
			int n = size(init_values);
			while(sz < n)
				sz *= 2;
			t.resize(2 * sz);
			REP(i, n)
				t[i + sz].second = t[i + sz].first = init_values[i];
			for(int i = sz - 1; i > 0; --i)
				t[i] = {
					min(t[2 * i].first, t[2 * i + 1].first),
					t[2 * i].second + t[2 * i + 1].second
				};
		}

		void change(int i, int delta) {
			assert(0 <= i and i < sz);
			t[i += sz].first += delta;
			t[i].second += delta;
			while(i /= 2)
				t[i] = {
					min(t[2 * i].first, t[2 * i + 1].first),
					t[2 * i].second + t[2 * i + 1].second
				};
		}

		LL get_sum_geq0_suffix(int v = 1) {
			if(t[v].first >= 0)
				return t[v].second;
			if(v >= sz)
				return 0;
			if(t[2 * v + 1].first >= 0)
				return get_sum_geq0_suffix(2 * v) + t[2 * v + 1].second;
			else
				return get_sum_geq0_suffix(2 * v + 1);
		}
	};

	struct Solve {
		int n;
		vector<int> row_sorted;
		LL lit_cnt;
		SumTree diff_tree;

		vector<int> get_diffs(vector<int> row, vector<int> col) {
			vector<int> row_minus_leq(n);
			int cnt_leq = 0;
			REP(i, size(row)) {
				while(cnt_leq < size(row) and col[cnt_leq] <= i)
					++cnt_leq;
				row_minus_leq[i] = row[i] - cnt_leq;
			}
			return row_minus_leq;
		}

		Solve(vector<int> row, vector<int> col) 
			: n(size(row)), row_sorted(row), lit_cnt(accumulate(row.begin(), row.end(), 0LL)),
			  diff_tree(get_diffs(row, col))
		{}

		int last_eq(vector<int> &v, int val) {
			return int(upper_bound(v.begin(), v.end(), val) - v.begin()) - 1;
		}
		int first_eq(vector<int> &v, int val) {
			return int(lower_bound(v.begin(), v.end(), val) - v.begin());
		}

		void change(int row_val, int col_val, int delta) {
			lit_cnt += delta;
			int i;
			if(delta == 1)
				i = last_eq(row_sorted, row_val);
			else
				i = first_eq(row_sorted, row_val);
			diff_tree.change(i, delta);
			row_sorted[i] += delta;
			if(delta == 1 and col_val < n)
				diff_tree.change(col_val, 1);
			else if(delta == -1 and col_val <= n)
				diff_tree.change(col_val - 1, -1);
		}

		LL answer() {
			return lit_cnt - diff_tree.get_sum_geq0_suffix();
		}
	};

	struct BitNode {
		int lit = 0, unlit = 0, lazy = 0, subsize = 1;
	};
	static BitNode merge(BitNode a, BitNode b) {
		BitNode ret;
		ret.subsize = a.subsize + b.subsize;
		ret.lit = a.lit + b.lit;
		ret.unlit = a.unlit + b.unlit;
		return ret;
	}

	struct BitTree {
		int sz = 2;
		vector<BitNode> t;

		BitTree(int n) {
			while(sz < n)
				sz *= 2;
			t.resize(2 * sz);
			REP(i, n)
				t[i + sz].unlit = 1;
			for(int v = sz - 1; v > 0; --v)
				t[v] = merge(t[2 * v], t[2 * v + 1]);
		}

		int get_sum() {
			return t[1].lit;
		}

		void push(int v) {
			if(t[v].lazy) {
				t[2 * v].lazy ^= 1;
				swap(t[2 * v].lit, t[2 * v].unlit);
				t[2 * v + 1].lazy ^= 1;
				swap(t[2 * v + 1].lit, t[2 * v + 1].unlit);
				t[v].lazy = false;
			}
		}

		void swap_lit(int l, int r, int v = 1) {
			if(l == 0 and r == t[v].subsize - 1) {
				swap(t[v].lit, t[v].unlit);
				t[v].lazy ^= 1;
				return;
			}
			push(v);
			int s2 = t[v].subsize / 2;
			if(r < s2)
				swap_lit(l, r, 2 * v);
			else if(l >= s2)
				swap_lit(l - s2, r - s2, 2 * v + 1);
			else {
				swap_lit(l, s2 - 1, 2 * v);
				swap_lit(0, r - s2, 2 * v + 1);
			}
			t[v] = merge(t[2 * v], t[2 * v + 1]);
		}

		int get_at_pos(int i, int v = 1) {
			if(t[v].subsize == 1)
				return t[v].lit;
			push(v);
			int s2 = t[v].subsize / 2;
			if(i < s2)
				return get_at_pos(i, 2 * v);
			else
				return get_at_pos(i - s2, 2 * v + 1);
		}
	};

	SzybkaHeura(int n, int m, int q) {
		vector<array<int, 4>> rectangles(m);
		for(auto &v : rectangles) {
			int xl, yd, xr, yu;
			cin >> xl >> yd >> xr >> yu;
			--xl, --yd, --xr, --yu;
			assert(xl <= xr and yd <= yu);
			v = {xl, yd, xr, yu};
		}

		vector<pair<int, int>> requests(q);
		for(auto &[x, y] : requests) {
			cin >> x >> y;
			--x, --y;
		}
		vector<pair<int, int>> sorted_req = requests;
		sort(sorted_req.begin(), sorted_req.end());
		sorted_req.erase(unique(sorted_req.begin(), sorted_req.end()), sorted_req.end());
		vector<int> init_state(size(sorted_req), -1);

		vector<vector<pair<int, int>>> events_at(n + 1);
		REP(i, size(sorted_req))
			events_at[sorted_req[i].first].emplace_back(n, i);
		REP(i, m) {
			events_at[rectangles[i][0]].emplace_back(rectangles[i][1], rectangles[i][3]);
			events_at[rectangles[i][2] + 1].emplace_back(rectangles[i][1], rectangles[i][3]);
		}

		BitTree tree(n);
		vector<int> col_cnt(n);
		REP(x, n + 1) {
			sort(events_at[x].begin(), events_at[x].end());
			for(auto [l, r] : events_at[x])
				if(l == n)
					init_state[r] = tree.get_at_pos(sorted_req[r].second);
				else
					tree.swap_lit(l, r);
			if(x < n)
				col_cnt[x] = tree.get_sum();
		}

		for(auto &v : events_at)
			v.clear();
		REP(i, m) {
			events_at[rectangles[i][1]].emplace_back(rectangles[i][0], rectangles[i][2]);
			events_at[rectangles[i][3] + 1].emplace_back(rectangles[i][0], rectangles[i][2]);
		}
		vector<int> row_cnt(n);
		REP(y, n) {
			for(auto [l, r] : events_at[y])
				if(l == n)
					init_state[r] = tree.get_at_pos(sorted_req[r].second);
				else
					tree.swap_lit(l, r);
			row_cnt[y] = tree.get_sum();
		}
		debug(row_cnt, col_cnt, init_state);

		auto id = [&](int x, int y) {
			return int(lower_bound(sorted_req.begin(), sorted_req.end(), make_pair(x, y)) - sorted_req.begin());
		};
		vector<int> curr_state = init_state;

		vector<int> row_sorted = row_cnt, col_sorted = col_cnt;
		sort(row_sorted.begin(), row_sorted.end());
		sort(col_sorted.begin(), col_sorted.end());
		Solve solve_lit(row_sorted, col_sorted);
		for(auto *v : {&row_sorted, &col_sorted}) {
			reverse(v->begin(), v->end());
			for(int &x : *v)
				x = n - x;
		}
		Solve solve_unlit(row_sorted, col_sorted);

		cout << min(solve_lit.answer(), solve_unlit.answer()) << '\n';
		REP(r, q) {
			auto [x, y] = requests[r];
			int &s = curr_state[id(x, y)];
			int delta = s ? -1 : 1;
			solve_lit.change(row_cnt[y], col_cnt[x], delta);
			solve_unlit.change(n - row_cnt[y], n - col_cnt[x], -delta);
			col_cnt[x] += delta;
			row_cnt[y] += delta;
			s ^= 1;
			cout << min(solve_lit.answer(), solve_unlit.answer()) << '\n';
		}
	}
};

struct Brute {
	vector<vector<int>> graph;
	vector<int> match, vis;
	int t = 0;
	
	bool match_dfs(int v) {
		vis[v] = t;
		for(int u : graph[v])
			if(match[u] == -1) {
				match[u] = v;
				match[v] = u;
				return true;
			}
	
		for(int u : graph[v])
			if(vis[match[u]] != t && match_dfs(match[u])) {
				match[u] = v;
				match[v] = u;
				return true;
			}
		return false;
	}
	
	int matching() {
		int n = int(graph.size());
		match.assign(n, -1);
		vis.assign(n, 0);
	
		int ans = 0, d = -1;
		while(d != 0) {
			d = 0;
			++t;
			for(int v = 0; v < n; ++v)
				if(match[v] == -1)
					d += match_dfs(v);
			ans += d;
		}
		return ans;
	}

	Brute(int n, int m, int q) {
		vector prefix(n + 1, vector<int>(n + 1));
		while(m --> 0) {
			int xl, yd, xr, yu;
			cin >> xl >> yd >> xr >> yu;
			--xl, --yd, --xr, --yu;
			prefix[xl][yd] ^= 1;
			prefix[xl][yu + 1] ^= 1;
			prefix[xr + 1][yd] ^= 1;
			prefix[xr + 1][yu + 1] ^= 1;
		}
		vector state(n, vector<int>(n));
		REP(x, n) {
			FOR(y, 1, n - 1)
				prefix[x][y] ^= prefix[x][y - 1];
			if(x > 0)
				REP(y, n)
					prefix[x][y] ^= prefix[x - 1][y];
			state[x] = prefix[x];
		}

		auto answer = [&] {
			graph.clear();
			graph.resize(n * n);
			auto id = [&](int x, int y) {
				return x + y * n;
			};
			REP(x, n)
				REP(y, n)
					REP(k, n) {
						if(state[x][y] != state[k][y])
							graph[id(x, y)].emplace_back(id(k, y));
						if(state[x][y] != state[x][k])
							graph[id(x, y)].emplace_back(id(x, k));
					}
			cout << matching() << '\n';
		};

		answer();
		while(q --> 0) {
			int x, y;
			cin >> x >> y;
			--x, --y;
			state[x][y] = not state[x][y];

			answer();
		}
	}
};

LL brute_running_time(LL n, LL m, LL q) {
	auto lg = [&](LL x) {
		return __lg(int(x + 1)) + 1;
	};
	return m + (q + 1) * n * n * lg(n);
}

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

	constexpr int tl_limit = int(2e7);
	assert(brute_running_time(50, int(1e4), 0) <= tl_limit);
	assert(brute_running_time(200, int(1e5), 10) <= tl_limit);

	int n, m, q;
	cin >> n >> m >> q;
	if(brute_running_time(n, m, q) <= tl_limit)
		Brute(n, m, q);
	else
		SzybkaHeura(n, m, q);
}