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
#include <bits/stdc++.h>
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
using std::vector;

void init_io() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
}

struct RectangleSide {
  int x = 0;
  int y0 = 0;
  int y1 = 0;
};

struct Correction {
  int index = 0;
  int delta = 0;
  int x = 0;
  int y = 0;
};

int N;
vector<RectangleSide> rectangle_sides_dim[2]; // 14 MB
vector<Correction> corrections;           // 5 MB
vector<int> column_sums_dim[2];         // 2 MB
int64_t total_sum;

void read_input() {
  int num_rectangles;
  int num_corrections;
  std::cin >> N;
  std::cin >> num_rectangles;
  std::cin >> num_corrections;

  REP(dim, 2) {
    rectangle_sides_dim[dim].reserve(2 * num_rectangles);
  }
  REP(i, num_rectangles) {
    int x[2][2]; // [dim][endpoint]
    REP(endpoint, 2) REP(dim, 2) std::cin >> x[dim][endpoint];
    REP(dim, 2) --x[dim][0];
    REP(dim, 2) REP(endpoint, 2) {
      rectangle_sides_dim[dim].push_back(
          RectangleSide{x[dim][endpoint], x[dim^1][0], x[dim^1][1]}
      );
    }
  }

  corrections.reserve(num_corrections);
  REP(i, num_corrections) {
    Correction correction;
    correction.index = i;
    std::cin >> correction.x >> correction.y;
    --correction.x;
    --correction.y;
    corrections.push_back(correction);
  }
}

class Broom {
public:
  Broom();
  void flip_prefix(int len);
  int get(int x) const;
  int sum() const { return entries[1].sum(); }

private:
  struct Entry {
    int flip = 0;
    int sums[2] = {};

    int sum(int flipit=0) const { return sums[flip ^ flipit]; }
    void do_flip() { flip ^= 1; }
  };

  static void add(Entry &a, const Entry &b, const Entry &c);

  int m_size;
  // full binary tree
  vector<Entry> entries; // 12 MB
};

Broom::Broom() {
  m_size = 1;
  while (m_size <= N + 5) m_size *= 2;
  entries.resize(2*m_size);
  REP(i, m_size) entries[m_size + i].sums[1] = 1;
  FORD(i, m_size-1, 1) add(entries[i], entries[2*i], entries[2*i+1]);
}

void Broom::flip_prefix(int len) {
  int p = m_size + len;
  while (p > 1) {
    if (p&1) {
      --p;
      entries[p].do_flip();
    }
    p >>= 1;
    add(entries[p], entries[2*p], entries[2*p+1]);
  }
}

void Broom::add(Entry &a, const Entry &b, const Entry &c) {
  REP(i, 2) a.sums[i] = b.sum(i) + c.sum(i);
}

int Broom::get(int x) const {
  int flip = 0;
  for (int p=m_size+x; p; p>>=1) flip ^= entries[p].flip;
  return flip;
}

template<int dim>
void calc_column_sums() {
  vector<int> &column_sums = column_sums_dim[dim];
  vector<RectangleSide> &rectangle_sides = rectangle_sides_dim[dim];
  column_sums.reserve(N);

  std::sort(rectangle_sides.begin(), rectangle_sides.end(),
    [](const RectangleSide &a, const RectangleSide &b) {
      return a.x < b.x;
    }
  );

  if constexpr (dim == 0) {
    std::sort(corrections.begin(), corrections.end(),
      [](const Correction &a, const Correction &b) {
        return std::make_tuple(a.x, a.y, a.index) < std::make_tuple(b.x, b.y, b.index);
      }
    );
    total_sum = 0;
  }

  auto next_rectangle_side = rectangle_sides.begin();
  auto next_correction __attribute__((unused)) = corrections.begin();

  Broom broom;
  REP(x, N) {
    while (next_rectangle_side != rectangle_sides.end() &&
           next_rectangle_side->x == x) {
      broom.flip_prefix(next_rectangle_side->y0);
      broom.flip_prefix(next_rectangle_side->y1);
      ++next_rectangle_side;
    }

    const int column_sum = broom.sum();
    column_sums.push_back(column_sum);

    if constexpr (dim == 0) {
      total_sum += column_sum;

      int prev_y = -1;
      int prev_delta = 1;
      while (next_correction != corrections.end() &&
             next_correction->x == x) {
        if (next_correction->y != prev_y) {
          prev_y = next_correction->y;
          prev_delta = 2 * broom.get(prev_y) - 1;
        }
        prev_delta = -prev_delta;
        next_correction->delta = prev_delta;
        ++next_correction;
      }
    }
  }

  if constexpr (dim == 0) {
    std::sort(corrections.begin(), corrections.end(),
      [](const Correction &a, const Correction &b) {
        return a.index < b.index;
      }
    );
  }
}

class PrefixSumMinimizer {
public:
  template <typename F>
  void initialize(F init);

  void update(int x, int64_t val);

  int64_t min_prefix_sum() const {
    return entries[1].min_prefix_sum;
  }

private:
  struct Entry {
    int64_t sum=0;
    int64_t min_prefix_sum=0;

    void set_leaf(int64_t v) {
      sum = v;
      min_prefix_sum = std::min(v, int64_t{0});
    }

    void add(const Entry &a, const Entry &b) {
      sum = a.sum + b.sum;
      min_prefix_sum = std::min(a.min_prefix_sum, a.sum + b.min_prefix_sum);
    }
  };

  int m_size;
  vector<Entry> entries; // 16 MB
};

template <typename F>
void PrefixSumMinimizer::initialize(F init) {
  m_size = 1;
  while (m_size <= N + 5) m_size *= 2;
  entries.resize(2*m_size);
  REP(i, N) entries[m_size + i].set_leaf(init(i));
  FORD(i, m_size-1, 1) entries[i].add(entries[2*i], entries[2*i+1]);
}

void PrefixSumMinimizer::update(int x, int64_t val) {
  int p = m_size + x;
  entries[p].set_leaf(val);
  while(p > 1) {
    p >>= 1;
    entries[p].add(entries[2*p], entries[2*p+1]);
  }
}

vector<int> sorted_column_sums_dim[2];   // 4 MB

// [y]: number of y such that sorted_column_sums_dim[1][y] <= x.
vector<int> inverted_sorted_column_sum_dim1; // 2 MB

PrefixSumMinimizer prefix_sum_minimizer;

void initialize_sorted() {
  REP(dim, 2) {
    sorted_column_sums_dim[dim] = column_sums_dim[dim];
    std::sort(sorted_column_sums_dim[dim].begin(), sorted_column_sums_dim[dim].end());
  }
  {
    inverted_sorted_column_sum_dim1.resize(N);
    int y = 0;
    REP(x, N) {
      while (y < N && sorted_column_sums_dim[1][y] <= x) ++y;
      inverted_sorted_column_sum_dim1[x] = y;
    }
  }
  prefix_sum_minimizer.initialize([](int x) {
      return sorted_column_sums_dim[0][x] - inverted_sorted_column_sum_dim1[x];
  });
}

int64_t current_score() {
  return
    int64_t(N) * int64_t(N)
    - total_sum
    + prefix_sum_minimizer.min_prefix_sum();
}

template<int dim>
void process_correction_dim(int x, int y, int delta) {
  int &sum = column_sums_dim[dim][x];
  vector<int>::iterator sorted_it;
  if (delta > 0) {
    sorted_it = std::upper_bound(
        sorted_column_sums_dim[dim].begin(),
        sorted_column_sums_dim[dim].end(),
        sum);
    assert(sorted_it != sorted_column_sums_dim[dim].begin());
    --sorted_it;
  } else {
    sorted_it = std::lower_bound(
        sorted_column_sums_dim[dim].begin(),
        sorted_column_sums_dim[dim].end(),
        sum);
    assert(sorted_it != sorted_column_sums_dim[dim].end());
  }
  assert(*sorted_it == sum);
  sum += delta;
  *sorted_it += delta;
  // still sorted now!
  int sorted_x = sorted_it - sorted_column_sums_dim[dim].begin();

  if constexpr (dim == 0) {
    prefix_sum_minimizer.update(sorted_x, sum - inverted_sorted_column_sum_dim1[sorted_x]);
  } else {
    if (delta > 0) {
      int &inv = inverted_sorted_column_sum_dim1[sum-1];
      assert(inv == sorted_x+1);
      --inv;
      prefix_sum_minimizer.update(sum-1, sorted_column_sums_dim[0][sum-1] - inv);
    } else {
      int &inv = inverted_sorted_column_sum_dim1[sum];
      assert(inv == sorted_x);
      ++inv;
      prefix_sum_minimizer.update(sum, sorted_column_sums_dim[0][sum] - inv);
    }
  }
}

void process_correction(const Correction &correction) {
  total_sum += correction.delta;
  process_correction_dim<0>(correction.x, correction.y, correction.delta);
  process_correction_dim<1>(correction.y, correction.x, correction.delta);
}

void debug_vector(const char *name, const vector<int> &v) {
  std::cerr << name << " ";
  for (auto x : v) std::cerr << x << " ";
  std::cerr << "\n";
}

void debug() {
  std::cerr << "DEBUG\n";
  std::cerr << "total_sum = " << total_sum << "\n";
  debug_vector("column_sums_dim[0]", column_sums_dim[0]);
  debug_vector("column_sums_dim[1]", column_sums_dim[1]);
  debug_vector("sorted_column_sums_dim[0]", sorted_column_sums_dim[0]);
  debug_vector("sorted_column_sums_dim[1]", sorted_column_sums_dim[1]);
  debug_vector("inverted_sorted_column_sum_dim1", inverted_sorted_column_sum_dim1);
  std::cerr << "minimizer: " << prefix_sum_minimizer.min_prefix_sum() << "\n";
}

void run_corrections() {
  auto it = corrections.begin();
  for (;;) {
    std::cout << current_score() << "\n";
    if (it == corrections.end()) break;
    process_correction(*it);
    ++it;
  }
}

int main() {
  init_io();
  read_input();
  calc_column_sums<0>();
  calc_column_sums<1>();
  initialize_sorted();
  run_corrections();
}