#include <iostream> #include <vector> using namespace std; int flip_rz[2005]; int flip_kol[2005][2005]; int flip_g; int tab[2005][2005]; int n, m, q; int vis[4000006]; int para[4000006]; vector <int> wolny_kol[2005][2]; vector <int> bez_kol[2005][2]; vector <int> wolny_rz[2005][2]; vector <int> bez_rz[2005][2]; int ans = 0; int nr(int i, int j) { return (i - 1)* n + j; } void match(int i, int j) { int u = nr(i, j); vis[u]= true; int c; //if (wyp) cout << u; while (!wolny_kol[j][tab[i][j]].empty()) { int v = wolny_kol[j][tab[i][j]][wolny_kol[j][tab[i][j]].size() - 1]; if (para[v]) { wolny_kol[j][tab[i][j]].pop_back(); continue; } para[para[u]] = 0; para[u] = v; para[v] = u; wolny_kol[j][tab[i][j]].pop_back(); vis[u] = false; return; } while (!wolny_rz[i][tab[i][j]].empty()) { int v = wolny_rz[i][tab[i][j]][wolny_rz[i][tab[i][j]].size() - 1]; if (para[v]) { wolny_rz[i][tab[i][j]].pop_back(); continue; } para[para[u]] = 0; para[u] = v; para[v] = u; wolny_rz[i][tab[i][j]].pop_back(); vis[u] = false; return; } for (int v: bez_kol[j][tab[i][j]]) { if (v == para[u]) continue; int u2 = para[v]; if (vis[u2]) { continue; } match(((u2 - 1)/n) + 1, (u2 - 1)%n + 1); if (para[v] == 0) { para[para[u]] = 0; para[v] = u; para[u] = v; vis[u] = false; return; } } for (int v: bez_rz[i][tab[i][j]]) { if (v == para[u]) continue; int u2 = para[v]; if (vis[u2]) { continue; } match(((u2 - 1)/n) + 1, (u2 - 1)%n + 1); if (para[v] == 0) { para[para[u]] = 0; para[v] = u; para[u] = v; vis[u] = false; return; } } vis[u] = true; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; while (m--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; flip_rz[x1] ^= 1; flip_rz[x2 + 1] ^= 1; flip_kol[x1][1] ^= 1; flip_kol[x1][y1] ^= 1; flip_kol[x1][y2 + 1] ^= 1; flip_kol[x2 + 1][1] ^= 1; flip_kol[x2 + 1][y1] ^= 1; flip_kol[x2 + 1][y2 + 1] ^= 1; } for (int i = 1; i <= n; i++) { flip_g = 0; for (int j = 1; j <= n; j++) { flip_g ^= flip_kol[i][j]; tab[i][j] = (tab[i - 1][j] ^ flip_rz[i]) ^ flip_g; if (!tab[i][j]) { wolny_kol[j][1].push_back(nr(i, j)); wolny_rz[i][1].push_back(nr(i, j)); bez_kol[j][1].push_back(nr(i, j)); bez_rz[i][1].push_back(nr(i, j)); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (tab[i][j]) { match(i, j); if (para[nr(i, j)]) { ans++; } } } } cout << ans << "\n"; while (q--) { int a, b; cin >> a >> b; int u = nr(a, b); int v = para[u]; if (v) ans--; para[u] = 0; para[v] = 0; tab[a][b] ^= 1; for (int i = 1; i <= n; i++) { wolny_kol[i][0].clear(); wolny_rz[i][0].clear(); bez_kol[i][0].clear(); bez_rz[i][0].clear(); wolny_kol[i][1].clear(); wolny_rz[i][1].clear(); bez_kol[i][1].clear(); bez_rz[i][1].clear(); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (!tab[i][j]) { if (!para[nr(i, j)]) { wolny_kol[j][1].push_back(nr(i, j)); wolny_rz[i][1].push_back(nr(i, j)); } bez_kol[j][1].push_back(nr(i, j)); bez_rz[i][1].push_back(nr(i, j)); } else { if (!para[nr(i, j)]) { wolny_kol[j][0].push_back(nr(i, j)); wolny_rz[i][0].push_back(nr(i, j)); } bez_kol[j][0].push_back(nr(i, j)); bez_rz[i][0].push_back(nr(i, j)); } vis[nr(i, j)] = false; } } match(a, b); if (para[u]) ans++; if (v) match(((v - 1)/n) + 1, (v - 1)%n + 1); if (para[v]) ans++; 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 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 | #include <iostream> #include <vector> using namespace std; int flip_rz[2005]; int flip_kol[2005][2005]; int flip_g; int tab[2005][2005]; int n, m, q; int vis[4000006]; int para[4000006]; vector <int> wolny_kol[2005][2]; vector <int> bez_kol[2005][2]; vector <int> wolny_rz[2005][2]; vector <int> bez_rz[2005][2]; int ans = 0; int nr(int i, int j) { return (i - 1)* n + j; } void match(int i, int j) { int u = nr(i, j); vis[u]= true; int c; //if (wyp) cout << u; while (!wolny_kol[j][tab[i][j]].empty()) { int v = wolny_kol[j][tab[i][j]][wolny_kol[j][tab[i][j]].size() - 1]; if (para[v]) { wolny_kol[j][tab[i][j]].pop_back(); continue; } para[para[u]] = 0; para[u] = v; para[v] = u; wolny_kol[j][tab[i][j]].pop_back(); vis[u] = false; return; } while (!wolny_rz[i][tab[i][j]].empty()) { int v = wolny_rz[i][tab[i][j]][wolny_rz[i][tab[i][j]].size() - 1]; if (para[v]) { wolny_rz[i][tab[i][j]].pop_back(); continue; } para[para[u]] = 0; para[u] = v; para[v] = u; wolny_rz[i][tab[i][j]].pop_back(); vis[u] = false; return; } for (int v: bez_kol[j][tab[i][j]]) { if (v == para[u]) continue; int u2 = para[v]; if (vis[u2]) { continue; } match(((u2 - 1)/n) + 1, (u2 - 1)%n + 1); if (para[v] == 0) { para[para[u]] = 0; para[v] = u; para[u] = v; vis[u] = false; return; } } for (int v: bez_rz[i][tab[i][j]]) { if (v == para[u]) continue; int u2 = para[v]; if (vis[u2]) { continue; } match(((u2 - 1)/n) + 1, (u2 - 1)%n + 1); if (para[v] == 0) { para[para[u]] = 0; para[v] = u; para[u] = v; vis[u] = false; return; } } vis[u] = true; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; while (m--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; flip_rz[x1] ^= 1; flip_rz[x2 + 1] ^= 1; flip_kol[x1][1] ^= 1; flip_kol[x1][y1] ^= 1; flip_kol[x1][y2 + 1] ^= 1; flip_kol[x2 + 1][1] ^= 1; flip_kol[x2 + 1][y1] ^= 1; flip_kol[x2 + 1][y2 + 1] ^= 1; } for (int i = 1; i <= n; i++) { flip_g = 0; for (int j = 1; j <= n; j++) { flip_g ^= flip_kol[i][j]; tab[i][j] = (tab[i - 1][j] ^ flip_rz[i]) ^ flip_g; if (!tab[i][j]) { wolny_kol[j][1].push_back(nr(i, j)); wolny_rz[i][1].push_back(nr(i, j)); bez_kol[j][1].push_back(nr(i, j)); bez_rz[i][1].push_back(nr(i, j)); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (tab[i][j]) { match(i, j); if (para[nr(i, j)]) { ans++; } } } } cout << ans << "\n"; while (q--) { int a, b; cin >> a >> b; int u = nr(a, b); int v = para[u]; if (v) ans--; para[u] = 0; para[v] = 0; tab[a][b] ^= 1; for (int i = 1; i <= n; i++) { wolny_kol[i][0].clear(); wolny_rz[i][0].clear(); bez_kol[i][0].clear(); bez_rz[i][0].clear(); wolny_kol[i][1].clear(); wolny_rz[i][1].clear(); bez_kol[i][1].clear(); bez_rz[i][1].clear(); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (!tab[i][j]) { if (!para[nr(i, j)]) { wolny_kol[j][1].push_back(nr(i, j)); wolny_rz[i][1].push_back(nr(i, j)); } bez_kol[j][1].push_back(nr(i, j)); bez_rz[i][1].push_back(nr(i, j)); } else { if (!para[nr(i, j)]) { wolny_kol[j][0].push_back(nr(i, j)); wolny_rz[i][0].push_back(nr(i, j)); } bez_kol[j][0].push_back(nr(i, j)); bez_rz[i][0].push_back(nr(i, j)); } vis[nr(i, j)] = false; } } match(a, b); if (para[u]) ans++; if (v) match(((v - 1)/n) + 1, (v - 1)%n + 1); if (para[v]) ans++; cout << ans << "\n"; } } |