//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 2e3 + 10, M=310; const int base = 1 << 4; int pl[N][N]; bool bylo[N][N]; int kol[N], rzad[N]; int kol_wol[N], rzad_wol[N]; struct Dinic { struct Edge { int v, c, inv; }; private: int n; vector<vector<Edge>> e_orig, e; VI dis, beg; bool Bfs(int s, int t) { fill_n(dis.begin(), n + 1, n + 1); dis[s] = 0; VI que; que.push_back(s); F(i, 0, sz(que)) { int v = que[i]; for (auto edge : e[v]) { int nei = edge.v; if (edge.c && dis[nei] > dis[v] + 1) { dis[nei] = dis[v] + 1; que.push_back(nei); if (nei == t) { return true; } } } } return false; } int Dfs(int v, int t, int min_cap) { int result = 0; if (v == t || min_cap == 0) { return min_cap; } for (int& i = beg[v]; i < sz(e[v]); i++) { int nei = e[v][i].v, c = e[v][i].c; if (dis[nei] == dis[v] + 1 && c > 0) { int flow_here = Dfs(nei, t, min(min_cap, c)); result += flow_here; min_cap -= flow_here; e[v][i].c -= flow_here; e[nei][e[v][i].inv].c += flow_here; } if (min_cap == 0) { break; } } return result; } void ResizeVectors() { e_orig.resize(n + 2); beg.resize(n + 2); dis.resize(n + 2); } public: Dinic() {n = -1;} void AddEdge(int a, int b, int cap, int bi_dir) { if (n < max(a, b)) { n = max(n, max(a, b)); ResizeVectors(); } e_orig[a].pb(Edge{b, cap, sz(e_orig[b])}); e_orig[b].pb(Edge{a, bi_dir * cap, sz(e_orig[a]) - 1}); } int MaxFlow(int s, int t) { if (t > n || s > n) { n = max(s, t); ResizeVectors(); } e = e_orig; int result = 0; while (Bfs(s, t)) { fill_n(beg.begin(), n + 1, 0); result += Dfs(s, t, kInf); } return result; } vector<bool> MinCut(int s, int t) { assert(!Bfs(s, t)); vector<bool> res(n + 1); FOR (i, 0, n) { res[i] = (dis[i] <= n); } return res; } vector<pi> EdgeCut(int s, int t) { vector<bool> left_part = MinCut(s, t); vector<pi> cut; FOR (v, 0, n) { for (auto edge : e_orig[v]) { if (edge.c != 0 && left_part[v] && !left_part[edge.v]) { cut.pb({v, edge.v}); } } } return cut; } #ifndef int #warning #endif static const int kInf = 1e18; // UWAZAC, JESLI NIE INT = LONG LONG }; void solve() { int n, m, q, res = 0; cin >> n >> m >> q; F(i, 0, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; pl[x1][y1] ^= 1; pl[x2 + 1][y1] ^= 1; pl[x1][y2 + 1] ^= 1; pl[x2 + 1][y2 + 1] ^= 1; } FOR(i, 1, n) { FOR(j, 1, n) { pl[i][j] ^= pl[i - 1][j] ^ pl[i][j - 1] ^ pl[i - 1][j - 1]; // cout << pl[i][j] << " "; } // cout << "\n"; } FOR(i, 1, n) { FOR(j, 1, n) { rzad[i] += pl[i][j]; kol[j] += pl[i][j]; } } FOR(i, 1, n) { kol_wol[i] = n - kol[i]; rzad_wol[i] = n - rzad[i]; } int start = 0, end = (n + 1) * (n + 1) + 10; Dinic dinic; FOR(i, 1, n) { FOR(j, 1, n) { if (pl[i][j]) { dinic.AddEdge(start, i * (n + 1) + j, 1, 0); FOR(a, 1, n) { FOR(b, 1, n) { if (pl[i][j] && !pl[a][b] && (a == i || b == j)) { dinic.AddEdge(i * (n + 1) + j, a * (n + 1) + b, 1, 0); } } } } else { dinic.AddEdge(i * (n + 1) + j, end, 1, 0); } } } res = dinic.MaxFlow(start, end); // debug(res); cout << res << "\n"; } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); // cout.tie(0); cerr.tie(0); cout << setprecision(9) << fixed; cerr << setprecision(6) << fixed; int test = 1, f; // cin >> test; F(_test, 0, test) { //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 5 2 -2 1 -2 4 5 5 0 1 1 4 1 1 3 4 3 1 5 4 5 1 2 1 2 1 4 1 4 3 3 0 1 1 2 1 1 2 1 2 1 3 2 3 10 5 0 1 1 4 1 1 3 4 3 1 5 4 5 1 2 1 2 1 4 1 4 */
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 | //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 2e3 + 10, M=310; const int base = 1 << 4; int pl[N][N]; bool bylo[N][N]; int kol[N], rzad[N]; int kol_wol[N], rzad_wol[N]; struct Dinic { struct Edge { int v, c, inv; }; private: int n; vector<vector<Edge>> e_orig, e; VI dis, beg; bool Bfs(int s, int t) { fill_n(dis.begin(), n + 1, n + 1); dis[s] = 0; VI que; que.push_back(s); F(i, 0, sz(que)) { int v = que[i]; for (auto edge : e[v]) { int nei = edge.v; if (edge.c && dis[nei] > dis[v] + 1) { dis[nei] = dis[v] + 1; que.push_back(nei); if (nei == t) { return true; } } } } return false; } int Dfs(int v, int t, int min_cap) { int result = 0; if (v == t || min_cap == 0) { return min_cap; } for (int& i = beg[v]; i < sz(e[v]); i++) { int nei = e[v][i].v, c = e[v][i].c; if (dis[nei] == dis[v] + 1 && c > 0) { int flow_here = Dfs(nei, t, min(min_cap, c)); result += flow_here; min_cap -= flow_here; e[v][i].c -= flow_here; e[nei][e[v][i].inv].c += flow_here; } if (min_cap == 0) { break; } } return result; } void ResizeVectors() { e_orig.resize(n + 2); beg.resize(n + 2); dis.resize(n + 2); } public: Dinic() {n = -1;} void AddEdge(int a, int b, int cap, int bi_dir) { if (n < max(a, b)) { n = max(n, max(a, b)); ResizeVectors(); } e_orig[a].pb(Edge{b, cap, sz(e_orig[b])}); e_orig[b].pb(Edge{a, bi_dir * cap, sz(e_orig[a]) - 1}); } int MaxFlow(int s, int t) { if (t > n || s > n) { n = max(s, t); ResizeVectors(); } e = e_orig; int result = 0; while (Bfs(s, t)) { fill_n(beg.begin(), n + 1, 0); result += Dfs(s, t, kInf); } return result; } vector<bool> MinCut(int s, int t) { assert(!Bfs(s, t)); vector<bool> res(n + 1); FOR (i, 0, n) { res[i] = (dis[i] <= n); } return res; } vector<pi> EdgeCut(int s, int t) { vector<bool> left_part = MinCut(s, t); vector<pi> cut; FOR (v, 0, n) { for (auto edge : e_orig[v]) { if (edge.c != 0 && left_part[v] && !left_part[edge.v]) { cut.pb({v, edge.v}); } } } return cut; } #ifndef int #warning #endif static const int kInf = 1e18; // UWAZAC, JESLI NIE INT = LONG LONG }; void solve() { int n, m, q, res = 0; cin >> n >> m >> q; F(i, 0, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; pl[x1][y1] ^= 1; pl[x2 + 1][y1] ^= 1; pl[x1][y2 + 1] ^= 1; pl[x2 + 1][y2 + 1] ^= 1; } FOR(i, 1, n) { FOR(j, 1, n) { pl[i][j] ^= pl[i - 1][j] ^ pl[i][j - 1] ^ pl[i - 1][j - 1]; // cout << pl[i][j] << " "; } // cout << "\n"; } FOR(i, 1, n) { FOR(j, 1, n) { rzad[i] += pl[i][j]; kol[j] += pl[i][j]; } } FOR(i, 1, n) { kol_wol[i] = n - kol[i]; rzad_wol[i] = n - rzad[i]; } int start = 0, end = (n + 1) * (n + 1) + 10; Dinic dinic; FOR(i, 1, n) { FOR(j, 1, n) { if (pl[i][j]) { dinic.AddEdge(start, i * (n + 1) + j, 1, 0); FOR(a, 1, n) { FOR(b, 1, n) { if (pl[i][j] && !pl[a][b] && (a == i || b == j)) { dinic.AddEdge(i * (n + 1) + j, a * (n + 1) + b, 1, 0); } } } } else { dinic.AddEdge(i * (n + 1) + j, end, 1, 0); } } } res = dinic.MaxFlow(start, end); // debug(res); cout << res << "\n"; } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); // cout.tie(0); cerr.tie(0); cout << setprecision(9) << fixed; cerr << setprecision(6) << fixed; int test = 1, f; // cin >> test; F(_test, 0, test) { //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 5 2 -2 1 -2 4 5 5 0 1 1 4 1 1 3 4 3 1 5 4 5 1 2 1 2 1 4 1 4 3 3 0 1 1 2 1 1 2 1 2 1 3 2 3 10 5 0 1 1 4 1 1 3 4 3 1 5 4 5 1 2 1 2 1 4 1 4 */ |