#include <iostream> #include <algorithm> #include <vector> #include <map> #include <tuple> using namespace std; struct TreeNode { bool val = false; int sum = 0; }; struct RnageQuery { int x1, x2, y1, y2; }; struct RangeQueryEnd { int x, y1, y2; bool operator<(RangeQueryEnd& a) { return this->x < a.x; } }; struct ElementQuery { int x, y; bool val; }; vector<TreeNode> tree; vector<RnageQuery> rqs; vector<int> eqis; vector<ElementQuery> eqs; vector<int> met[2]; vector<RangeQueryEnd> rqes; int count_col[2][2] = { 0 }; int sum = 0; int n, m, q, n2; void swap_rqs() { int t; for (int i = 0;i < m;i++) { t = rqs[i].x1; rqs[i].x1 = rqs[i].y1; rqs[i].y1 = t; t = rqs[i].x2; rqs[i].x2 = rqs[i].y2; rqs[i].y2 = t; } } void set_rqes() { for (int i = 0;i < m;i++) { rqes[i * 2].x = rqs[i].x1; rqes[i * 2].y1 = rqs[i].y1; rqes[i * 2].y2 = rqs[i].y2; rqes[i * 2 + 1].x = rqs[i].x2 + 1; rqes[i * 2 + 1].y1 = rqs[i].y1; rqes[i * 2 + 1].y2 = rqs[i].y2; } } void set_n2() { n2 = 1; while (n2 < n) { n2 *= 2; } } void clear_tree() { tree.clear(); tree.resize(n2 * 2); } bool sort_eqis_comp(int a, int b) { return eqs[a].x < eqs[b].x; } void sort_eqis() { sort(eqis.begin(), eqis.end(), sort_eqis_comp); } void read_input() { cin >> n >> m >> q; rqes.resize(m * 2); rqs.resize(m); eqs.resize(q); eqis.resize(q); met[0].resize(n); met[1].resize(n); for (int i = 0;i < m;i++) { cin >> rqs[i].x1 >> rqs[i].y1 >> rqs[i].x2 >> rqs[i].y2; rqs[i].x1--; rqs[i].y1--; rqs[i].x2--; rqs[i].y2--; } for (int i = 0;i < q;i++) { cin >> eqs[i].x >> eqs[i].y; eqs[i].x--; eqs[i].y--; eqis[i] = i; } } void set_range(int x, int y) { x += n2; y += n2; tree[x].val = !tree[x].val; if(x != y) tree[y].val = !tree[y].val; if (tree[x].val) { tree[x].sum = 1; } else { tree[x].sum = 0; } if(x != y) if (tree[y].val) { tree[y].sum = 1; } else { tree[y].sum = 0; } int gs = 1; if(x != y) while (x / 2 != y / 2) { if (x % 2 == 0) { tree[x + 1].val = !tree[x + 1].val; tree[x + 1].sum = gs - tree[x + 1].sum; } if (y % 2 == 1) { tree[y - 1].val = !tree[y - 1].val; tree[y - 1].sum = gs - tree[y - 1].sum; } tree[x/2].sum = tree[(x / 2) * 2].sum + tree[(x / 2) * 2 + 1].sum; if (tree[x / 2].val) tree[x / 2].sum = gs * 2 - tree[x / 2].sum; tree[y / 2].sum = tree[(y / 2) * 2].sum + tree[(y / 2) * 2 + 1].sum; if (tree[y / 2].val) tree[y / 2].sum = gs * 2 - tree[y / 2].sum; x /= 2; y /= 2; gs *= 2; } while (x != 1) { x /= 2; tree[x].sum = tree[x * 2].sum + tree[x * 2 + 1].sum; if (tree[x].val) tree[x].sum = gs * 2 - tree[x].sum; gs *= 2; } } bool get_tree_val(int x) { x += n; bool v = tree[x].val; while (x != 1) { x /= 2; if (tree[x].val) v = !v; } return v; } int get_tree_sum() { return tree[1].sum; } void swep_col(int t) { clear_tree(); set_rqes(); sort(rqes.begin(), rqes.end()); vector<RangeQueryEnd>::iterator it = rqes.begin(); for (int i = 0;i < n;i++) { while (it != rqes.end() && it->x == i) { set_range(it->y1, it->y2); it++; } int s = get_tree_sum(); met[t][i] = s; if (s == 0) count_col[t][0]++; if (s == n) count_col[t][1]++; } } void swep_co2() { clear_tree(); sort_eqis(); vector<RangeQueryEnd>::iterator it = rqes.begin(); vector<int>::iterator it_el = eqis.begin(); for (int i = 0;i < n;i++) { while (it != rqes.end() && it->x == i) { set_range(it->y1, it->y2); it++; } while (it_el != eqis.end() && eqs[*it_el].x == i) { eqs[*it_el].val = get_tree_val(eqs[*it_el].y); it_el++; } } } void element_query_reappear() { map<tuple<int, int>, bool> rapp; for (vector<ElementQuery>::iterator it = eqs.begin();it != eqs.end();it++) { map<tuple<int, int>, bool>::iterator f = rapp.find({it->x, it->y}); if (f == rapp.end()) rapp[{it->x, it->y}] = it->val; else { f->second = !f->second; it->val = f->second; } } } void sum_gen() { for (int i = 0;i < n;i++) sum += met[0][i]; } void print_sol() { int sol = min(sum, n*n - sum); sol -= count_col[0][0] * count_col[1][0]; sol -= count_col[1][1] * count_col[0][1]; cout << sol << "\n"; } void update(ElementQuery& e) { if (e.val) { sum--; met[0][e.x]--; met[1][e.y]--; if (met[0][e.x] == n - 1) count_col[0][1]--; if (met[0][e.x] == 0) count_col[0][0]++; if (met[1][e.y] == n - 1) count_col[1][1]--; if (met[1][e.y] == 0) count_col[1][0]++; } else { sum++; met[0][e.x]++; met[1][e.y]++; if (met[0][e.x] == n) count_col[0][1]++; if (met[0][e.x] == 1) count_col[0][0]--; if (met[1][e.y] == n) count_col[1][1]++; if (met[1][e.y] == 1) count_col[1][0]--; } } void print_sols() { print_sol(); for (vector<ElementQuery>::iterator it = eqs.begin(); it != eqs.end();it++) { update(*it); print_sol(); } } int main() { read_input(); set_n2(); swep_col(0); swep_co2(); swap_rqs(); swep_col(1); element_query_reappear(); sum_gen(); print_sols(); 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 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | #include <iostream> #include <algorithm> #include <vector> #include <map> #include <tuple> using namespace std; struct TreeNode { bool val = false; int sum = 0; }; struct RnageQuery { int x1, x2, y1, y2; }; struct RangeQueryEnd { int x, y1, y2; bool operator<(RangeQueryEnd& a) { return this->x < a.x; } }; struct ElementQuery { int x, y; bool val; }; vector<TreeNode> tree; vector<RnageQuery> rqs; vector<int> eqis; vector<ElementQuery> eqs; vector<int> met[2]; vector<RangeQueryEnd> rqes; int count_col[2][2] = { 0 }; int sum = 0; int n, m, q, n2; void swap_rqs() { int t; for (int i = 0;i < m;i++) { t = rqs[i].x1; rqs[i].x1 = rqs[i].y1; rqs[i].y1 = t; t = rqs[i].x2; rqs[i].x2 = rqs[i].y2; rqs[i].y2 = t; } } void set_rqes() { for (int i = 0;i < m;i++) { rqes[i * 2].x = rqs[i].x1; rqes[i * 2].y1 = rqs[i].y1; rqes[i * 2].y2 = rqs[i].y2; rqes[i * 2 + 1].x = rqs[i].x2 + 1; rqes[i * 2 + 1].y1 = rqs[i].y1; rqes[i * 2 + 1].y2 = rqs[i].y2; } } void set_n2() { n2 = 1; while (n2 < n) { n2 *= 2; } } void clear_tree() { tree.clear(); tree.resize(n2 * 2); } bool sort_eqis_comp(int a, int b) { return eqs[a].x < eqs[b].x; } void sort_eqis() { sort(eqis.begin(), eqis.end(), sort_eqis_comp); } void read_input() { cin >> n >> m >> q; rqes.resize(m * 2); rqs.resize(m); eqs.resize(q); eqis.resize(q); met[0].resize(n); met[1].resize(n); for (int i = 0;i < m;i++) { cin >> rqs[i].x1 >> rqs[i].y1 >> rqs[i].x2 >> rqs[i].y2; rqs[i].x1--; rqs[i].y1--; rqs[i].x2--; rqs[i].y2--; } for (int i = 0;i < q;i++) { cin >> eqs[i].x >> eqs[i].y; eqs[i].x--; eqs[i].y--; eqis[i] = i; } } void set_range(int x, int y) { x += n2; y += n2; tree[x].val = !tree[x].val; if(x != y) tree[y].val = !tree[y].val; if (tree[x].val) { tree[x].sum = 1; } else { tree[x].sum = 0; } if(x != y) if (tree[y].val) { tree[y].sum = 1; } else { tree[y].sum = 0; } int gs = 1; if(x != y) while (x / 2 != y / 2) { if (x % 2 == 0) { tree[x + 1].val = !tree[x + 1].val; tree[x + 1].sum = gs - tree[x + 1].sum; } if (y % 2 == 1) { tree[y - 1].val = !tree[y - 1].val; tree[y - 1].sum = gs - tree[y - 1].sum; } tree[x/2].sum = tree[(x / 2) * 2].sum + tree[(x / 2) * 2 + 1].sum; if (tree[x / 2].val) tree[x / 2].sum = gs * 2 - tree[x / 2].sum; tree[y / 2].sum = tree[(y / 2) * 2].sum + tree[(y / 2) * 2 + 1].sum; if (tree[y / 2].val) tree[y / 2].sum = gs * 2 - tree[y / 2].sum; x /= 2; y /= 2; gs *= 2; } while (x != 1) { x /= 2; tree[x].sum = tree[x * 2].sum + tree[x * 2 + 1].sum; if (tree[x].val) tree[x].sum = gs * 2 - tree[x].sum; gs *= 2; } } bool get_tree_val(int x) { x += n; bool v = tree[x].val; while (x != 1) { x /= 2; if (tree[x].val) v = !v; } return v; } int get_tree_sum() { return tree[1].sum; } void swep_col(int t) { clear_tree(); set_rqes(); sort(rqes.begin(), rqes.end()); vector<RangeQueryEnd>::iterator it = rqes.begin(); for (int i = 0;i < n;i++) { while (it != rqes.end() && it->x == i) { set_range(it->y1, it->y2); it++; } int s = get_tree_sum(); met[t][i] = s; if (s == 0) count_col[t][0]++; if (s == n) count_col[t][1]++; } } void swep_co2() { clear_tree(); sort_eqis(); vector<RangeQueryEnd>::iterator it = rqes.begin(); vector<int>::iterator it_el = eqis.begin(); for (int i = 0;i < n;i++) { while (it != rqes.end() && it->x == i) { set_range(it->y1, it->y2); it++; } while (it_el != eqis.end() && eqs[*it_el].x == i) { eqs[*it_el].val = get_tree_val(eqs[*it_el].y); it_el++; } } } void element_query_reappear() { map<tuple<int, int>, bool> rapp; for (vector<ElementQuery>::iterator it = eqs.begin();it != eqs.end();it++) { map<tuple<int, int>, bool>::iterator f = rapp.find({it->x, it->y}); if (f == rapp.end()) rapp[{it->x, it->y}] = it->val; else { f->second = !f->second; it->val = f->second; } } } void sum_gen() { for (int i = 0;i < n;i++) sum += met[0][i]; } void print_sol() { int sol = min(sum, n*n - sum); sol -= count_col[0][0] * count_col[1][0]; sol -= count_col[1][1] * count_col[0][1]; cout << sol << "\n"; } void update(ElementQuery& e) { if (e.val) { sum--; met[0][e.x]--; met[1][e.y]--; if (met[0][e.x] == n - 1) count_col[0][1]--; if (met[0][e.x] == 0) count_col[0][0]++; if (met[1][e.y] == n - 1) count_col[1][1]--; if (met[1][e.y] == 0) count_col[1][0]++; } else { sum++; met[0][e.x]++; met[1][e.y]++; if (met[0][e.x] == n) count_col[0][1]++; if (met[0][e.x] == 1) count_col[0][0]--; if (met[1][e.y] == n) count_col[1][1]++; if (met[1][e.y] == 1) count_col[1][0]--; } } void print_sols() { print_sol(); for (vector<ElementQuery>::iterator it = eqs.begin(); it != eqs.end();it++) { update(*it); print_sol(); } } int main() { read_input(); set_n2(); swep_col(0); swep_co2(); swap_rqs(); swep_col(1); element_query_reappear(); sum_gen(); print_sols(); return 0; } |