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
#include<iostream>
#include<vector>

#define mp make_pair
#define fi first
#define se second

using namespace std;

const int N = 2001;// 300000;

int n, m, q;
int r1, c1, r2, c2;
long long pref[N][N];
bool st[N][N];
int rows[N], cols[N];
int zeros_in_row[N];
int ones_in_row[N];
bool is_zero_column[N];
bool is_one_column[N];
// int one_columns;
// int zero_columns;

void insert_rect(int r1, int c1, int r2, int c2) {
  r1--;c1--;r2--;c2--;
  pref[r1][c1]++;
  pref[r1][c2 + 1]--;
  pref[r2 + 1][c1]--;
  pref[r2 + 1][c2 + 1]++;
}

bool is_zero_col(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 1 && ones_in_row[i] > zeros_in_row[i]) {
      return false;
    }
  }
  return true;
}

void zero_column(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 1 || zeros_in_row[i] == n) {
      rows[i]++;
    }
  }
}

void unzero_column(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 1 || zeros_in_row[i] == n) {
      rows[i]--;
    }
  }
}

bool is_one_col(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 0 && ones_in_row[i] < zeros_in_row[i]) {
      return false;
    }
  }
  return true;
}

void one_column(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 0 || ones_in_row[i] == n) {
      rows[i]--;
    }
  }
}

void unone_column(int col) {
  for (int i = 0; i < n; i++) {
    if (st[i][col] == 0 || ones_in_row[i] == n) {
      rows[i]++;
    }
  }
}

void zero_and_one_columns() {
  for (int i = 0; i < n; i++) {
    if (is_zero_col(i)) {
      // cout << i << " is zero column___________________" << endl;
      is_zero_column[i] = true;
    } else {
      is_zero_column[i] = false;
    }
    if (is_one_col(i)) {
      // cout << i << " is one column___________________" << endl;
      is_one_column[i] = true;
    }
    else {
      is_one_column[i] = false;
    }
  }
}

void fill_st() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i > 0) {
        pref[i][j] += pref[i - 1][j];
      }
      if (j > 0) {
        pref[i][j] += pref[i][j - 1];
      }
      if (i > 0 && j > 0) {
        pref[i][j] -= pref[i - 1][j - 1];
      }
      st[i][j] = pref[i][j] % 2;
      // if (st[i][j] == 0) {
      //   // rows[i]--;
      //   // cols[j]--;
      //   if (cols[j] == -n) {
      //     // zero_columns++;
      //   }
      // } else {
      //   // rows[i]++;
      //   // cols[j]++;
      //   if (cols[j] == n) {
      //     // one_columns++;
      //   }
      // }
      // cout << st[i][j] << " ";
      if (st[i][j]) {
        ones_in_row[i]++;
      } else {
        zeros_in_row[i]++;
      }
    }
    // cout << endl;
  }
  // cout << endl;
  // zero_and_one_columns();
  // for (int j = 0; j < n; j++) {
  //   possibly_zero(j);
  //   possibly_one(j);
  // }
}

void fill_res_pref() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      pref[i][j] = 0;
      pref[i][j] += i > 0 ? pref[i - 1][j] : 0;
      pref[i][j] += j > 0 ? pref[i][j - 1] : 0;
      pref[i][j] -= i > 0 && j > 0 ? pref[i - 1][j - 1] : 0;

      if (i > 0 && st[i][j] != st[i - 1][j]) {
        long long tmp = i >= 2 ? pref[i - 2][j] : 0;
        tmp += j > 0 ? pref[i][j - 1] : 0;
        tmp -= i >= 2 && j > 0 ? pref[i - 2][j - 1] : 0;
        pref[i][j] = max(pref[i][j], tmp + 1);
      }

      if (j > 0 && st[i][j] != st[i][j - 1]) {
        long long tmp = j >= 2 ? pref[i][j - 2] : 0;
        tmp += i > 0 ? pref[i - 1][j] : 0;
        tmp -= j >= 2 && i > 0 ? pref[i - 1][j - 2] : 0;
        pref[i][j] = max(pref[i][j], tmp + 1);
      }
      // cout << pref[i][j] << " ";
    }
    // cout << endl;
  }
}

void fill_rows() {
  zero_and_one_columns();
  for (int i = 0; i < n; i++) {
    rows[i] = 0;
    int zeros_to_use_in_row = 0;
    int ones_to_use_in_row = 0;
    for (int j = 0; j < n; j++) {
      rows[i] += st[i][j] ? 1 : -1;
      if (zeros_in_row[i] > ones_in_row[i] && is_zero_column[j] && st[i][j] == 0) {
        zeros_to_use_in_row++;
      }
      if (zeros_in_row[i] < ones_in_row[i] && is_one_column[j] && st[i][j] == 1) {
        ones_to_use_in_row++;
      }
    }
    if (zeros_to_use_in_row > ones_in_row[i]) {
      rows[i] += zeros_to_use_in_row - ones_in_row[i];
    }
    if (ones_to_use_in_row > zeros_in_row[i]) {
      rows[i] -= ones_to_use_in_row - zeros_in_row[i];
    }
    // cout << rows[i] << " ";
  }
  // cout << endl;
}

int res_old() {
  fill_rows();
  int res = 0;
  int zeros = 0;
  int ones = 0;
  // cout << "______________ROWS___________________" << endl;
  for (int i = 0; i < n; i++) {
    // cout << rows[i] << endl;
    res += (n - abs(rows[i])) / 2;
    // cout << "res += " << (n - abs(rows[i])) / 2 << endl;
    if (rows[i] > 0) {
      ones += rows[i];
    } else {
      zeros -= rows[i];
    }
  }
  // cout << endl;
  // cout << "in rows " << res << " ones " << ones << " zeros " << zeros << endl;
  res += min(ones, zeros);
  return res;
}

int res() {
  fill_rows();
  int max_all = 0;
  int max_col = 0;
  int in_rows = 0;
  int zeros = 0;
  int ones = 0;
  for (int i = 0; i < n; i++) {
    if (rows[i] > 0) {
      ones += rows[i];
    } else {
      zeros -= rows[i];
    }
    in_rows += min(zeros_in_row[i], ones_in_row[i]);
  }
  // cout << "zeros " << zeros << " ones " << ones << endl;
  max_all = min(zeros, ones);
  // TODO res += (ile w rzedach)
  for (int j = 0; j < n; j++) {
    zeros = 0;
    ones = 0;
    for (int i = 0; i < n; i++) {
      if (zeros_in_row[i] > ones_in_row[i] && st[i][j] == 0) {
        zeros++;
      }
      if (zeros_in_row[i] < ones_in_row[i] && st[i][j] == 1) {
        ones++;
      }
    }
    max_col += min(ones, zeros);
  }
  // cout << "in rows " << in_rows << " min(" << max_col << ", " << max_all << ")" << endl;
  return in_rows + min(max_col, max_all);
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m >> q;

  while(m--) {
    cin >> r1 >> c1 >> r2 >> c2;
    insert_rect(r1, c1, r2, c2);
  }
  fill_st();

  // fill_res_pref();
  cout << res() << endl;

  while(q--) {
    cin >> r1 >> c1;
    r1--;
    c1--;
    if (st[r1][c1] == 0) {
      // possibly_unzero(c1);
      st[r1][c1] = 1;
      zeros_in_row[r1]--;
      ones_in_row[r1]++;
      // if (cols[c1] == -n) {
      //   // zero_columns--;
      //   unzero_column(c1);
      // }
      rows[r1]++;
      // if (!is_zero_col(c1)) {
        rows[r1]++;
      // }
      cols[c1] += 2;
      // if (cols[c1] == n) {
      //   // one_columns++;
      //   one_column(c1);
      // }
      // possibly_one(c1);
    } else {
      // possibly_unone(c1);
      st[r1][c1] = 0;
      zeros_in_row[r1]++;
      ones_in_row[r1]--;
      // if (cols[c1] == n) {
      //   // one_columns--;
      //   unone_column(c1);
      // }
      rows[r1]--;
      // if (!is_one_col(c1)) {
        rows[r1]--;
      // }
      cols[c1] -= 2;
      // if (cols[c1] == -n) {
      //   // zero_columns++;
      //   zero_column(c1);
      // }
      // possibly_zero(c1);
    }
    // fill_res_pref();
    cout << res() << endl;

    // for (int i = 0; i < n; i++) {
    //   if (is_zero_col(i)) {
    //     unzero_column(i);
    //   }
    //   if (is_one_col(i)) {
    //     unone_column(i);
    //   }
    // }

    for (int i = 0; i < n; i++) {
      int tmp = 0;
      for (int j = 0; j < n; j++) {
        if (st[i][j]) {
          tmp++;
        } else {
          tmp--;
        }
        // cout << st[i][j];// << " ";
      }
      // cout << "     " << rows[i] << endl;
      if (rows[i] != tmp) {
        // cout << "NIE " << i << " ma " << rows[i] << " zamiast " << tmp << endl;
      }
    }
    // cout << endl << endl;
    // cout << "zero_columns " << zero_columns << endl;
    // cout << "one_columns " << one_columns << endl;
  }

  return 0;
}