#include <bits/stdc++.h> using namespace std; int t = 0; bool match(int i, int j, vector<vector<int>> &vis, vector<vector<bool>> const& M, vector<vector<pair<int, int>>> &partner) { vis[i][j] = t; const int n = M.size(); for (int k = 0; k < n; ++k) { if (M[k][j] != M[i][j] && partner[k][j].first == -1) { partner[k][j] = {i, j}; partner[i][j] = {k, j}; return true; } if (M[i][k] != M[i][j] && partner[i][k].first == -1) { partner[i][k] = {i, j}; partner[i][j] = {i, k}; return true; } } for (int k = 0; k < n; ++k) { auto x = partner[k][j]; if (M[k][j] != M[i][j] && vis[x.first][x.second] != t && match(x.first, x.second, vis, M, partner)) { partner[k][j] = {i, j}; partner[i][j] = {k, j}; return true; } x = partner[i][k]; if (M[i][k] != M[i][j] && vis[x.first][x.second] != t && match(x.first, x.second, vis, M, partner)) { partner[i][k] = {i, j}; partner[i][j] = {i, k}; return true; } } return false; } int turbomatch(vector<vector<bool>> const& M, vector<vector<pair<int, int>>> &partner, vector<vector<int>> &vis) { const int n = M.size(); int cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j, ++t) { if (M[i][j]) { cnt += match(i, j, vis, M, partner); } } } return cnt; } struct segtree { vector<int> T; int n; segtree (int _n) : n(_n) { T.resize(2*n); } void modify(int l, int r, int val) { for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l&1) T[l++] += val; if (r&1) T[--r] += val; } } void push() { for (int i = 1; i < n; ++i) { T[i << 1] += T[i]; T[i << 1 | 1] += T[i]; T[i] = 0; } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<segtree> T(n+1, segtree(n+1)); // updates using n segtrees, O(mlogn) // construction of the trees takes O(2n^2) though while (m--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; T[x1-1].modify(y1-1, y2, 1); T[x2].modify(y1-1, y2, -1); } for (int i = 0; i < n; ++i) T[i].push(); for (int y = 0; y < n; ++y) { int s = 0; for (int x = 0; x < n; ++x) { s += T[x].T[y+n+1]; T[x].T[y+n+1] = s & 1; } } vector<vector<bool>> M; for (int i = 0; i < n; ++i) { M.push_back(vector<bool>(T[i].T.begin()+n+1, T[i].T.end()-1)); T[i].T.clear(); } T.clear(); vector<vector<pair<int, int>>> partner(n, vector<pair<int, int>>(n, {-1, 0})); vector<vector<int>> vis(n, vector<int>(n)); // turbomatching is O(|V|(|V| + |E|)) // |V| = n^2, |E| = n*|V| = n^3 // O(|V|(|V| + |E|)) = O(|V||E|) = O(n^5) int ans = turbomatch(M, partner, vis); cout << ans << '\n'; // TODO: Speed this up by just calling match(a, b) // O(qn^2) while (q--) { int a, b; cin >> a >> b; --a, --b; M[a][b] = !M[a][b]; // zeroing ab's partner auto p = partner[a][b]; if (p.first != -1) { partner[p.first][p.second] = {-1, 0}; ++t; ans += match(p.first, p.second, vis, M, partner) - 1; } partner[a][b] = {-1, 0}; ++t; ans += match(a, b, vis, M, partner); cout << ans << '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; int t = 0; bool match(int i, int j, vector<vector<int>> &vis, vector<vector<bool>> const& M, vector<vector<pair<int, int>>> &partner) { vis[i][j] = t; const int n = M.size(); for (int k = 0; k < n; ++k) { if (M[k][j] != M[i][j] && partner[k][j].first == -1) { partner[k][j] = {i, j}; partner[i][j] = {k, j}; return true; } if (M[i][k] != M[i][j] && partner[i][k].first == -1) { partner[i][k] = {i, j}; partner[i][j] = {i, k}; return true; } } for (int k = 0; k < n; ++k) { auto x = partner[k][j]; if (M[k][j] != M[i][j] && vis[x.first][x.second] != t && match(x.first, x.second, vis, M, partner)) { partner[k][j] = {i, j}; partner[i][j] = {k, j}; return true; } x = partner[i][k]; if (M[i][k] != M[i][j] && vis[x.first][x.second] != t && match(x.first, x.second, vis, M, partner)) { partner[i][k] = {i, j}; partner[i][j] = {i, k}; return true; } } return false; } int turbomatch(vector<vector<bool>> const& M, vector<vector<pair<int, int>>> &partner, vector<vector<int>> &vis) { const int n = M.size(); int cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j, ++t) { if (M[i][j]) { cnt += match(i, j, vis, M, partner); } } } return cnt; } struct segtree { vector<int> T; int n; segtree (int _n) : n(_n) { T.resize(2*n); } void modify(int l, int r, int val) { for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l&1) T[l++] += val; if (r&1) T[--r] += val; } } void push() { for (int i = 1; i < n; ++i) { T[i << 1] += T[i]; T[i << 1 | 1] += T[i]; T[i] = 0; } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<segtree> T(n+1, segtree(n+1)); // updates using n segtrees, O(mlogn) // construction of the trees takes O(2n^2) though while (m--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; T[x1-1].modify(y1-1, y2, 1); T[x2].modify(y1-1, y2, -1); } for (int i = 0; i < n; ++i) T[i].push(); for (int y = 0; y < n; ++y) { int s = 0; for (int x = 0; x < n; ++x) { s += T[x].T[y+n+1]; T[x].T[y+n+1] = s & 1; } } vector<vector<bool>> M; for (int i = 0; i < n; ++i) { M.push_back(vector<bool>(T[i].T.begin()+n+1, T[i].T.end()-1)); T[i].T.clear(); } T.clear(); vector<vector<pair<int, int>>> partner(n, vector<pair<int, int>>(n, {-1, 0})); vector<vector<int>> vis(n, vector<int>(n)); // turbomatching is O(|V|(|V| + |E|)) // |V| = n^2, |E| = n*|V| = n^3 // O(|V|(|V| + |E|)) = O(|V||E|) = O(n^5) int ans = turbomatch(M, partner, vis); cout << ans << '\n'; // TODO: Speed this up by just calling match(a, b) // O(qn^2) while (q--) { int a, b; cin >> a >> b; --a, --b; M[a][b] = !M[a][b]; // zeroing ab's partner auto p = partner[a][b]; if (p.first != -1) { partner[p.first][p.second] = {-1, 0}; ++t; ans += match(p.first, p.second, vis, M, partner) - 1; } partner[a][b] = {-1, 0}; ++t; ans += match(a, b, vis, M, partner); cout << ans << '\n'; } } |