#include <bits/stdc++.h> using namespace std; int n; int calc(vector<vector<int> > & v) { int cnt0 = 0; int cnt1 = 0; vector<bool> all0x(n, true); vector<bool> all0y(n, true); vector<bool> all1x(n, true); vector<bool> all1y(n, true); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { cnt0++; all1x[i] = false; all1y[j] = false; } else { cnt1++; all0x[i] = false; all0y[j] = false; } } } int cnt0x = 0; int cnt0y = 0; int cnt1x = 0; int cnt1y = 0; for (int i = 0; i < n; i++) { if (all0x[i]) cnt0x++; if (all0y[i]) cnt0y++; if (all1x[i]) cnt1x++; if (all1y[i]) cnt1y++; } int ans = min(cnt0 - cnt0x * cnt0y, cnt1 - cnt1x * cnt1y); return ans; } vector<vector<int> > g; vector<int> mt; vector<int> used; bool try_kuhn(int v) { if (used[v]) return false; used[v] = true; for (size_t i = 0; i < g[v].size(); ++i) { int to = g[v][i]; if (mt[to] == -1 || try_kuhn(mt[to])) { mt[to] = v; return true; } } return false; }; int calc2(vector<vector<int> > & v) { int h = 0; int k = 0; vector<vector<int> > num(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { num[i][j] = h; h++; } else { num[i][j] = k; k++; } } } g.assign(h, vector<int>()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { for (int k = 0; k < n; k++) { if (v[i][k] == 1) { g[num[i][j]].push_back(num[i][k]); } if (v[k][j] == 1) { g[num[i][j]].push_back(num[k][j]); } } } } } mt.assign(k, -1); vector<char> used1(h); for (int i=0; i<h; ++i) { for (size_t j=0; j<g[i].size(); ++j) { if (mt[g[i][j]] == -1) { mt[g[i][j]] = i; used1[i] = true; break; } } } for (int i=0; i<h; ++i) { if (used1[i]) continue; used.assign(h, false); try_kuhn(i); } int ans = 0; for (int i=0; i<k; ++i) { if (mt[i] != -1) { ans++; } } return ans; } void solve() { int m, q; cin >> n >> m >> q; vector<vector<int> > v(n, vector<int>(n)); for (int j = 0; j < m; j++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { v[x][y] = 1 - v[x][y]; } } } cout << calc2(v) << "\n"; for (int j = 0; j < q; j++) { int x, y; cin >> x >> y; x--; y--; v[x][y] = 1 - v[x][y]; cout << calc2(v) << "\n"; } } int main() { ios_base::sync_with_stdio(false); cout.precision(15); cout << fixed; cin.tie(0); int T = 1; //cin >> T; for (int tst = 0; tst < T; tst++) { solve(); } return 0; }
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 | #include <bits/stdc++.h> using namespace std; int n; int calc(vector<vector<int> > & v) { int cnt0 = 0; int cnt1 = 0; vector<bool> all0x(n, true); vector<bool> all0y(n, true); vector<bool> all1x(n, true); vector<bool> all1y(n, true); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { cnt0++; all1x[i] = false; all1y[j] = false; } else { cnt1++; all0x[i] = false; all0y[j] = false; } } } int cnt0x = 0; int cnt0y = 0; int cnt1x = 0; int cnt1y = 0; for (int i = 0; i < n; i++) { if (all0x[i]) cnt0x++; if (all0y[i]) cnt0y++; if (all1x[i]) cnt1x++; if (all1y[i]) cnt1y++; } int ans = min(cnt0 - cnt0x * cnt0y, cnt1 - cnt1x * cnt1y); return ans; } vector<vector<int> > g; vector<int> mt; vector<int> used; bool try_kuhn(int v) { if (used[v]) return false; used[v] = true; for (size_t i = 0; i < g[v].size(); ++i) { int to = g[v][i]; if (mt[to] == -1 || try_kuhn(mt[to])) { mt[to] = v; return true; } } return false; }; int calc2(vector<vector<int> > & v) { int h = 0; int k = 0; vector<vector<int> > num(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { num[i][j] = h; h++; } else { num[i][j] = k; k++; } } } g.assign(h, vector<int>()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v[i][j] == 0) { for (int k = 0; k < n; k++) { if (v[i][k] == 1) { g[num[i][j]].push_back(num[i][k]); } if (v[k][j] == 1) { g[num[i][j]].push_back(num[k][j]); } } } } } mt.assign(k, -1); vector<char> used1(h); for (int i=0; i<h; ++i) { for (size_t j=0; j<g[i].size(); ++j) { if (mt[g[i][j]] == -1) { mt[g[i][j]] = i; used1[i] = true; break; } } } for (int i=0; i<h; ++i) { if (used1[i]) continue; used.assign(h, false); try_kuhn(i); } int ans = 0; for (int i=0; i<k; ++i) { if (mt[i] != -1) { ans++; } } return ans; } void solve() { int m, q; cin >> n >> m >> q; vector<vector<int> > v(n, vector<int>(n)); for (int j = 0; j < m; j++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { v[x][y] = 1 - v[x][y]; } } } cout << calc2(v) << "\n"; for (int j = 0; j < q; j++) { int x, y; cin >> x >> y; x--; y--; v[x][y] = 1 - v[x][y]; cout << calc2(v) << "\n"; } } int main() { ios_base::sync_with_stdio(false); cout.precision(15); cout << fixed; cin.tie(0); int T = 1; //cin >> T; for (int tst = 0; tst < T; tst++) { solve(); } return 0; } |