#include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; // START PushRelabelMaxFlow #define FORE(i, t) for (auto i = t.begin(); i != t.end(); ++i) struct Edge { int to, capacity, flow; Edge *rev; Edge(int _to, int _capacity, int _flow = 0) : to(_to), capacity(_capacity), flow(_flow) {} }; struct Vertex { int excess, height; list<Edge> adj; list<Edge>::iterator cur; Vertex() : excess(0), height(0), cur(adj.begin()) {} }; struct PushRelabelMaxFlow { int number_of_vertices, source, sink; vector<Vertex> V; PushRelabelMaxFlow(int _number_of_vertices, int _source, int _sink) : number_of_vertices(_number_of_vertices), source(_source), sink(_sink), V(_number_of_vertices) { V[source].height = number_of_vertices; } void add_edge(int from, int to, int forward_capacity = 0, int backward_capacity = 0) { V[from].adj.push_front(Edge(to, forward_capacity)); V[to].adj.push_front(Edge(from, backward_capacity)); V[from].adj.front().rev = &V[to].adj.front(); V[to].adj.front().rev = &V[from].adj.front(); } inline bool inside(int x) { return x != source && x != sink; } void push(int x, Edge &e) { int w = min(V[x].excess, e.capacity - e.flow); V[x].excess -= w; V[e.to].excess += w; e.flow += w; e.rev->flow = -e.flow; } void lift(int x) { int mn = INF; FORE(it, V[x].adj) { if (it->capacity > it->flow) { mn = min(mn, V[it->to].height); } } V[x].height = 1 + mn; } void discharge(int x) { while (V[x].excess > 0) { list<Edge>::iterator &y = V[x].cur; if (y == V[x].adj.end()) { lift(x); y = V[x].adj.begin(); } else if ((y->capacity > y->flow) && (V[x].height == V[y->to].height + 1)) { push(x, *y); } else { ++y; } } } int compute_max_flow() { for (auto &it : V[source].adj) { it.flow = it.capacity; it.rev->flow = -it.capacity; V[it.to].excess += it.capacity; V[source].excess -= it.capacity; } list<int> l; REP(i, number_of_vertices) { if (inside(i)) { l.PB(i); } } FORE(it, l) { int old_height = V[*it].height; discharge(*it); if (V[*it].height > old_height) { int x = *it; l.erase(it); l.push_front(x); it = l.begin(); } } return V[sink].excess; } }; // END PushRelabelMaxFlow int get_vertex(int x, int y, int n) { return (x - 1) * n + (y - 1); } void build_graph(PushRelabelMaxFlow &graph, const vector<vector<bool>> &b, int n) { int source = n * n; int sink = n * n + 1; FOR(x, 1, n) { FOR(y, 1, n) { int from = get_vertex(x, y, n); if (b[x][y]) { graph.add_edge(source, from, 1); FOR(nx, 1, n) { if (nx != x and not b[nx][y]) { int to = get_vertex(nx, y, n); graph.add_edge(from, to, 1); } } FOR(ny, 1, n) { if (ny != y and not b[x][ny]) { int to = get_vertex(x, ny, n); graph.add_edge(from, to, 1); } } } else { graph.add_edge(from, sink, 1); } } } } void inline one() { int n, m, q; cin >> n >> m >> q; vector<vector<bool>> b(n + 1, vector<bool>(n + 1)); REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; FOR(x, x1, x2) { FOR(y, y1, y2) { b[x][y] = not b[x][y]; } } } int v = n * n + 2; int source = n * n; int sink = n * n + 1; REP(i, q + 1) { auto graph = PushRelabelMaxFlow(v, source, sink); build_graph(graph, b, n); int result = graph.compute_max_flow(); cout << result << "\n"; if (i < q) { int x, y; cin >> x >> y; b[x][y] = not b[x][y]; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; // START PushRelabelMaxFlow #define FORE(i, t) for (auto i = t.begin(); i != t.end(); ++i) struct Edge { int to, capacity, flow; Edge *rev; Edge(int _to, int _capacity, int _flow = 0) : to(_to), capacity(_capacity), flow(_flow) {} }; struct Vertex { int excess, height; list<Edge> adj; list<Edge>::iterator cur; Vertex() : excess(0), height(0), cur(adj.begin()) {} }; struct PushRelabelMaxFlow { int number_of_vertices, source, sink; vector<Vertex> V; PushRelabelMaxFlow(int _number_of_vertices, int _source, int _sink) : number_of_vertices(_number_of_vertices), source(_source), sink(_sink), V(_number_of_vertices) { V[source].height = number_of_vertices; } void add_edge(int from, int to, int forward_capacity = 0, int backward_capacity = 0) { V[from].adj.push_front(Edge(to, forward_capacity)); V[to].adj.push_front(Edge(from, backward_capacity)); V[from].adj.front().rev = &V[to].adj.front(); V[to].adj.front().rev = &V[from].adj.front(); } inline bool inside(int x) { return x != source && x != sink; } void push(int x, Edge &e) { int w = min(V[x].excess, e.capacity - e.flow); V[x].excess -= w; V[e.to].excess += w; e.flow += w; e.rev->flow = -e.flow; } void lift(int x) { int mn = INF; FORE(it, V[x].adj) { if (it->capacity > it->flow) { mn = min(mn, V[it->to].height); } } V[x].height = 1 + mn; } void discharge(int x) { while (V[x].excess > 0) { list<Edge>::iterator &y = V[x].cur; if (y == V[x].adj.end()) { lift(x); y = V[x].adj.begin(); } else if ((y->capacity > y->flow) && (V[x].height == V[y->to].height + 1)) { push(x, *y); } else { ++y; } } } int compute_max_flow() { for (auto &it : V[source].adj) { it.flow = it.capacity; it.rev->flow = -it.capacity; V[it.to].excess += it.capacity; V[source].excess -= it.capacity; } list<int> l; REP(i, number_of_vertices) { if (inside(i)) { l.PB(i); } } FORE(it, l) { int old_height = V[*it].height; discharge(*it); if (V[*it].height > old_height) { int x = *it; l.erase(it); l.push_front(x); it = l.begin(); } } return V[sink].excess; } }; // END PushRelabelMaxFlow int get_vertex(int x, int y, int n) { return (x - 1) * n + (y - 1); } void build_graph(PushRelabelMaxFlow &graph, const vector<vector<bool>> &b, int n) { int source = n * n; int sink = n * n + 1; FOR(x, 1, n) { FOR(y, 1, n) { int from = get_vertex(x, y, n); if (b[x][y]) { graph.add_edge(source, from, 1); FOR(nx, 1, n) { if (nx != x and not b[nx][y]) { int to = get_vertex(nx, y, n); graph.add_edge(from, to, 1); } } FOR(ny, 1, n) { if (ny != y and not b[x][ny]) { int to = get_vertex(x, ny, n); graph.add_edge(from, to, 1); } } } else { graph.add_edge(from, sink, 1); } } } } void inline one() { int n, m, q; cin >> n >> m >> q; vector<vector<bool>> b(n + 1, vector<bool>(n + 1)); REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; FOR(x, x1, x2) { FOR(y, y1, y2) { b[x][y] = not b[x][y]; } } } int v = n * n + 2; int source = n * n; int sink = n * n + 1; REP(i, q + 1) { auto graph = PushRelabelMaxFlow(v, source, sink); build_graph(graph, b, n); int result = graph.compute_max_flow(); cout << result << "\n"; if (i < q) { int x, y; cin >> x >> y; b[x][y] = not b[x][y]; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |