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

const int MAXN = 300005;

typedef long long int LL;

bool state[205][205];


struct SegmentTree {
  int n;
  int height;
  vector<int> tree;
  vector<bool> delayed;

  SegmentTree(int n_) {
    n = n_;
    tree.resize(2*n+1, 0);
    delayed.resize(n+1, false);
    height = 0;
    for (int sz = 1; sz <= n; sz *= 2) height++;
  }

  int flip(int old, int sz) { return sz - old; }

  bool merge_delay(bool old, bool val) { return old ^ val; }

  int merge_values(int a, int b) { return a + b; }

  void update_value(int pos, int sz) {
    tree[pos] = flip(tree[pos], sz);
    if (pos < n) delayed[pos] = !delayed[pos];
  }

  void apply_delay(int pos, int from_pos, int sz) {
    if ((from_pos < n) && (delayed[from_pos]))
      tree[pos] = flip(tree[pos], sz);
  }

  void push_delay(int pos, int from_pos) {
    if ((pos < n) && (from_pos < n))
      delayed[pos] = merge_delay(delayed[pos], delayed[from_pos]);
  }

  void build(int pos) {
    int sz = 1;
    while (pos > 1) {
      pos /= 2; sz *= 2;
      tree[pos] = merge_values(tree[2*pos], tree[2*pos+1]);
      apply_delay(pos, pos, sz);
    }
  }

  void push(int pos) {
    int sz = 1 << (height-1);
    for (int lvl = height-1; lvl > 0; lvl--) {
      int p = pos >> lvl; sz /= 2;
      if (delayed[p]) {
        push_delay(2*p, p);
        push_delay(2*p+1, p);
        apply_delay(2*p, p, sz);
        apply_delay(2*p+1, p, sz);
        delayed[p] = false;
      }
    }
  }

  void update(int from, int to) {
    push(from+n); push(to+n);
    int left = from+n-1, right = to+n+1, sz = 1;
    while (left/2 != right/2) {
      if (left % 2 == 0) update_value(left+1, sz);
      if (right % 2 == 1) update_value(right-1, sz);
      left /= 2; right /= 2; sz *= 2;
    }
    build(from+n); build(to+n);
  }

  int query(int from, int to) {
    push(from+n); push(to+n);
    int result = 0;
    int left = from+n-1, right = to+n+1;
    while (left/2 != right/2) {
      if (left % 2 == 0) result = merge_values(result, tree[left+1]);
      if (right % 2 == 1) result = merge_values(result, tree[right-1]);
      left /= 2; right /= 2;
    }
    return result;
  }
};

struct HashPair {
  LL operator() (const pair<int,int>& p) const {
    return (LL)p.first * MAXN + p.second;
  }
};

int N;

bool AugmentingPath(int node,
                    const vector<vector<int>>& adj,
                    vector<bool>* visited,
                    vector<int>* matching) {
  if (visited->at(node)) return false;
  visited->at(node) = true;

  for (int x : adj[node])
    if ((matching->at(x) == -1)
     || (AugmentingPath(matching->at(x), adj, visited, matching))) {
      matching->at(node) = x;
      matching->at(x) = node;
      return true;
    }
  return false;
}

int BipartiteMatching(const vector<vector<int>>& adj,
                      vector<bool>& left,
                      vector<int>* matching,
                      vector<int>* vertex_cover) {
  vector<bool> visited(N);
  matching->resize(N, -1);
  vertex_cover->clear();

  bool extended;
  do {
    extended = false;
    fill(visited.begin(), visited.end(), false);
    for (int i = 0; i < N; i++)
      if ((left[i]) && (matching->at(i) == -1))
        if (AugmentingPath(i, adj, &visited, matching)) extended = true;
  } while (extended);

  int result = 0;
  for (int i = 0; i < N; i++) {
    if (matching->at(i) == -1) continue;
    if (!left[i]) continue;
    result++;
    vertex_cover->push_back(visited[i] ? matching->at(i) : i);
  }
  return result;
}

int n;

vector<vector<int>> construct_graph() {
  N = n * n;
  vector<vector<int>> adj = vector<vector<int>>(n * n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int x = 0; x < n; x++) {
        if (state[i][j] != state[x][j])
          adj[i * n + j].push_back(x * n + j);
        if (state[i][j] != state[i][x])
          adj[i * n + j].push_back(i * n + x);
      }
  return adj;
}

SegmentTree tree[2] = { SegmentTree(MAXN), SegmentTree(MAXN) };
vector<pair<int,int>> open_events[2][MAXN], close_events[2][MAXN];
vector<pair<int,int>> query;
vector<int> queries_list[MAXN];
unordered_map<pair<int,int>, int, HashPair> query_presults;
LL total_sum;
LL theoretical_max;
int sums[2][MAXN];
int cnt_0[2], cnt_n[2];
unordered_map<pair<int,int>, bool, HashPair> last_flips;

LL make_answer() {
  LL value_one = total_sum - (LL)cnt_n[0] * cnt_n[1];
  LL value_zero = ((LL)n * n - total_sum) - (LL)cnt_0[0] * cnt_0[1];
  return min(theoretical_max, min(value_one, value_zero));
}

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

  int m, q;
  cin >> n >> m >> q;

  bool brute = false;
  if (((LL)n*n*q <= 100000000) && (n <= 200)) {
    brute = true;
    cerr << "brute\n";
  }
  else
    cerr << "wzo wrong\n";

  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    assert(x1 >= 1);
    assert(x1 <= x2);
    assert(x2 <= n);
    assert(y1 >= 0);
    assert(y1 <= y2);
    assert(y2 <= n);
    open_events[0][x1].push_back(make_pair(y1, y2));
    close_events[0][x2].push_back(make_pair(y1, y2));
    open_events[1][y1].push_back(make_pair(x1, x2));
    close_events[1][y2].push_back(make_pair(x1, x2));
  }
  query.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> query[i].first >> query[i].second;
    assert(query[i].first >= 1);
    assert(query[i].first <= n);
    assert(query[i].second >= 1);
    assert(query[i].second <= n);
    queries_list[query[i].first].push_back(query[i].second);
  }

  for (int flip = 0; flip <= 1; flip++) {
    for (int i = 1; i <= n; i++) {
      for (const auto& evt : open_events[flip][i])
        tree[flip].update(evt.first, evt.second);

      sums[flip][i] = tree[flip].query(1, n);

      if (flip == 0) {
        for (int j : queries_list[i])
          query_presults[make_pair(i, j)] = tree[flip].query(j, j);
        total_sum += sums[flip][i];
        if (brute) {
          for (int j = 1; j <= n; j++)
            state[i-1][j-1] = tree[flip].query(j, j);
        }
      }

      for (const auto& evt : close_events[flip][i])
        tree[flip].update(evt.first, evt.second);
    }

    for (int i = 1; i <= n; i++) {
      if (sums[flip][i] == 0) cnt_0[flip]++;
      if (sums[flip][i] == n) cnt_n[flip]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    theoretical_max += min(sums[0][i], n - sums[0][i]);
    theoretical_max += min(sums[1][i], n - sums[1][i]);
  }

  if (brute) {
    for (int i = 0; i <= q; i++) {

    vector<vector<int>> adj = construct_graph();
    vector<bool> left(n*n, false);
    for (int x = 0; x < n; x++)
      for (int y = 0; y < n; y++)
        left[x * n + y] = state[x][y];

    vector<int> matching;
    vector<int> cover;
    cout << BipartiteMatching(adj, left, &matching, &cover) << "\n";

    if (i == q) break;

    state[query[i].first-1][query[i].second-1] = !state[query[i].first-1][query[i].second-1];

    }

    return 0;
  }


  cout << make_answer() << "\n";

  for (int i = 0; i < q; i++) {
    /*
    for (int flip = 0; flip <= 1; flip++) {
      cerr << "sums[" << flip << "] = ";
      for (int j = 1; j <= n; j++) cerr << sums[flip][j] << " ";
      cerr << "\n";
      cerr << "cnt_0[" << flip << "] = " << cnt_0[flip] << "\n";
      cerr << "cnt_n[" << flip << "] = " << cnt_n[flip] << "\n";
    }
    cerr << "total_sum = " << total_sum << "\n";
    */
    if (i == q) break;

    int delta_state = 1 - 2 * (last_flips[query[i]] ^ query_presults[query[i]]);
    last_flips[query[i]] = !last_flips[query[i]];

    if (sums[0][query[i].first] == 0) cnt_0[0]--;
    if (sums[1][query[i].second] == 0) cnt_0[1]--;
    if (sums[0][query[i].first] == n) cnt_n[0]--;
    if (sums[1][query[i].second] == n) cnt_n[1]--;
    theoretical_max -= min(sums[0][query[i].first], n - sums[0][query[i].first]);
    theoretical_max -= min(sums[1][query[i].second], n - sums[1][query[i].second]);

    total_sum += delta_state;
    sums[0][query[i].first] += delta_state;
    sums[1][query[i].second] += delta_state;

    if (sums[0][query[i].first] == 0) cnt_0[0]++;
    if (sums[1][query[i].second] == 0) cnt_0[1]++;
    if (sums[0][query[i].first] == n) cnt_n[0]++;
    if (sums[1][query[i].second] == n) cnt_n[1]++;
    theoretical_max += min(sums[0][query[i].first], n - sums[0][query[i].first]);
    theoretical_max += min(sums[1][query[i].second], n - sums[1][query[i].second]);

    //cerr << "theoretical_max = " << theoretical_max << "\n";

    cout << make_answer() << "\n";
  }

  return 0;
}