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

using namespace std;

struct Change
{
  int x1, y1, x2, y2;
};

struct Query
{
  int x, y;
};

struct TestCase
{
  int side;
  int change_count;
  int query_count;

  vector<Change> changes;
  vector<Query> queries;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase test_case;
  cin >> test_case.side >> test_case.change_count >> test_case.query_count;
  test_case.changes.resize(test_case.change_count);
  test_case.queries.resize(test_case.query_count);
  for (auto& c : test_case.changes) cin >> c.x1 >> c.y1 >> c.x2 >> c.y2;
  for (auto& q : test_case.queries) cin >> q.x >> q.y;
  return test_case;
}

list<pair<int, int>> base_ranges(Change c)
{
  return {
      {c.x1, c.y1},
      {c.x2 + 1, c.y1},
      {c.x1, c.y2 + 1},
      {c.x2 + 1, c.y2 + 1},
  };
}

struct Position
{
  int x, y;
};

struct Node
{
  struct Edge
  {
    using Key = deque<Edge>::iterator;
    Key reverse;
    int neighbor;
    bool bipartite;
  };

  int index;
  deque<Edge> edges;

  Edge::Key add(int neighbor, bool bipartite)
  {
    Edge e;
    e.neighbor = neighbor;
    e.bipartite = bipartite;
    return edges.insert(edges.end(), e);
  }

  void swap()
  {
    for (auto& e : edges)
    {
      e.reverse->bipartite = !e.bipartite;
      e.bipartite = !e.bipartite;
    }
  }
};

struct Matching
{
  vector<int> node_match;
  int size = 0;
  Matching(size_t node_count) : node_match(node_count, -1) {}
  bool is_node_matched(int node) const { return node_match[node] != -1; }
  bool is_edge_match(int from, int to) const { return node_match[from] == to; }
  void match(int node1, int node2)
  {
    node_match[node1] = node2;
    node_match[node2] = node1;
  }
  void unmatch(int node)
  {
    if (node_match[node] != -1)
    {
      node_match[node_match[node]] = -1;
      size--;
    }
    node_match[node] = -1;
  }
};

using Graph = vector<Node>;

Matching find_maximum_matching(const Graph& graph);
void augment_matching(const Graph& graph, Matching& matching);

void solve_test_case(const TestCase& test_case)
{
  vector<vector<bool>> data;
  data.resize(test_case.side + 2);
  for (auto& v : data) v.resize(test_case.side + 2);

  for (auto c : test_case.changes)
    for (auto [x, y] : base_ranges(c)) { data[x][y] = !data[x][y]; }

  // Calculate result state after first operations
  vector<vector<bool>> state;
  state.resize(test_case.side + 1);
  for (auto& v : state) v.resize(test_case.side + 1, false);

  for (int x = 1; x <= test_case.side; x++)
    for (int y = 1; y <= test_case.side; y++)
      state[x][y] =
          state[x - 1][y] ^ state[x][y - 1] ^ state[x - 1][y - 1] ^ data[x][y];

  // Build graph
  Graph graph(test_case.side * test_case.side);

  auto get_index = [](int x, int y, int side) {
    return (y - 1) * side + (x - 1);
  };

  auto connect = [&](int a, int b, bool bipartite) {
    auto k1 = graph[a].add(b, bipartite);
    auto k2 = graph[b].add(a, bipartite);
    k1->reverse = k2;
    k2->reverse = k1;
  };

  for (int x = 1; x <= test_case.side; x++)
    for (int y = 1; y <= test_case.side; y++)
    {
      int index = get_index(x, y, test_case.side);
      for (int i = 1; i <= test_case.side; i++)
      {
        if (i == y) continue;
        int ind = get_index(x, i, test_case.side);
        connect(index, ind, state[x][y] != state[x][i]);
      }

      for (int i = 1; i <= test_case.side; i++)
      {
        if (i == x) continue;
        int ind = get_index(i, y, test_case.side);
        connect(index, ind, state[x][y] != state[i][y]);
      }
    }

  Matching matching = find_maximum_matching(graph);
  cout << matching.size << "\n";

  // Perform queries
  for (auto q : test_case.queries)
  {
    state[q.x][q.y] = !state[q.x][q.y];
    int index = get_index(q.x, q.y, test_case.side);
    graph[index].swap();
    matching.unmatch(index);
    augment_matching(graph, matching);
    cout << matching.size << "\n";
  }
}

using Path = list<int>;

list<Path> find_augmenting_paths(const Graph& graph, const Matching& matching);
void apply_augmenting_path(const Path& path, Matching& matching);

Matching find_maximum_matching(const Graph& graph)
{
  Matching matching(graph.size());
  augment_matching(graph, matching);
  return matching;
}

void augment_matching(const Graph& graph, Matching& matching)
{
  list<Path> paths;
  do {
    paths = find_augmenting_paths(graph, matching);
    for (const Path& path : paths) apply_augmenting_path(path, matching);
  } while (paths.size() > 0);
}

void apply_augmenting_path(const Path& path, Matching& matching)
{
  int previous;
  bool match = false;
  for (auto current : path)
  {
    if (match) matching.match(previous, current);
    match = !match;
    previous = current;
  }
  matching.size++;
}

using Visited = vector<bool>;

Path find_path_from(int source, const Graph&, const Matching&, Visited&);
list<Path> find_augmenting_paths(const Graph& graph, const Matching& matching)
{
  list<Path> paths;
  Visited visited(graph.size(), false);

  for (size_t node = 0; node < graph.size(); node++)
  {
    if (visited[node] || matching.is_node_matched(node)) continue;
    auto path = find_path_from(node, graph, matching, visited);
    if (path.size() > 0) paths.push_back(path);
  }
  return paths;
}

Path find_path_from(
    int source, const Graph& graph, const Matching& matching, Visited& visited)
{
  Path current_path;
  current_path.push_back(source);
  visited[source] = true;
  stack<int> last_neighbor;
  last_neighbor.push(-1);
  while (!current_path.empty())
  {
    int current = current_path.back();
    if (current_path.size() % 2 == 0 && !matching.is_node_matched(current))
      return current_path;

    for (size_t i = last_neighbor.top() + 1; i < graph[current].edges.size();
         i++)
    {
      int neighbor = graph[current].edges[i].neighbor;
      last_neighbor.pop();
      last_neighbor.push(i);
      if (!graph[current].edges[i].bipartite) continue;
      if (visited[neighbor]) continue;

      // Ignore edges that don't lie on augmenting path
      if (current_path.size() % 2 == 0 &&
          !matching.is_edge_match(current, neighbor))
        continue;
      if (current_path.size() % 2 == 1 &&
          matching.is_edge_match(current, neighbor))
        continue;

      visited[neighbor] = true;
      current_path.push_back(neighbor);
      last_neighbor.push(-1);
      break;
    }
    // There's nothing to do in this node anymore
    if (current_path.back() == current)
    {
      current_path.pop_back();
      last_neighbor.pop();
    }
  }

  return current_path;
}