#include <iostream> #include <type_traits> #include <vector> constexpr int next_pow2(int v) { --v; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; ++v; return v; } struct FlipSumSegTree1D { std::vector<int> tree_; std::vector<bool> lazy_; int N_; FlipSumSegTree1D(int num_nodes) : N_(next_pow2(num_nodes)) , tree_(2 * next_pow2(num_nodes), 0) , lazy_(2 * next_pow2(num_nodes), false) {} constexpr inline int _L(int x) { return x * 2; } constexpr inline int _R(int x) { return x * 2 + 1; } void _flip_impl(int node, int l, int r, int ql, int qr) { if (lazy_[node]) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } lazy_[node] = false; } if (ql <= l && r <= qr) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } } else if (ql <= r && l <= qr) { int mid = (l + r) / 2; _flip_impl(_L(node), l, mid, ql, qr); _flip_impl(_R(node), mid + 1, r, ql, qr); tree_[node] = tree_[_L(node)] + tree_[_R(node)]; } } void flip(int ql, int qr) { _flip_impl(1, 1, N_, ql, qr); } int _count_impl(int node, int l, int r, int ql, int qr) { if (lazy_[node]) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } lazy_[node] = false; } if (ql <= l && r <= qr) { return tree_[node]; } else if (ql <= r && l <= qr) { int mid = (l + r) / 2; return _count_impl(_L(node), l, mid, ql, qr) + _count_impl(_R(node), mid + 1, r, ql, qr); } return 0; } int count(int ql, int qr) { return _count_impl(1, 1, N_, ql, qr); } }; struct FlipSumSegTree2D { std::vector<int> tree_; std::vector<bool> lazy_; std::vector<int> all_xs_; std::vector<int> all_ys_; FlipSumSegTree1D tree_max_xs_; FlipSumSegTree1D tree_max_ys_; FlipSumSegTree1D tree_min_xs_; FlipSumSegTree1D tree_min_ys_; int N_; int num_nodes_; FlipSumSegTree2D(int num_nodes) : num_nodes_(num_nodes) , N_(next_pow2(num_nodes)) , tree_(2 * next_pow2(num_nodes) * 2 * next_pow2(num_nodes), 0) , lazy_(2 * next_pow2(num_nodes) * 2 * next_pow2(num_nodes), false) , all_xs_(num_nodes + 1, 0) , all_ys_(num_nodes + 1, 0) , tree_max_xs_(num_nodes) , tree_max_ys_(num_nodes) , tree_min_xs_(num_nodes) , tree_min_ys_(num_nodes) {} constexpr inline int _LL(int n) { return n * 4 + 0; } constexpr inline int _LR(int n) { return n * 4 + 1; } constexpr inline int _RL(int n) { return n * 4 + 2; } constexpr inline int _RR(int n) { return n * 4 + 3; } void _flip_impl(int node, int xl, int xr, int yl, int yr, int qxl, int qxr, int qyl, int qyr) { if (lazy_[node]) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } lazy_[node] = false; } if (qxl <= xl && xr <= qxr && qyl <= yl && yr <= qyr) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } } else if (qxl <= xr && xl <= qxr && qyl <= yr && yl <= qyr) { int mx = (xl + xr) / 2; int my = (yl + yr) / 2; _flip_impl(_LL(node), xl, mx, yl, my, qxl, qxr, qyl, qyr); _flip_impl(_LR(node), xl, mx, my + 1, yr, qxl, qxr, qyl, qyr); _flip_impl(_RL(node), mx + 1, xr, yl, my, qxl, qxr, qyl, qyr); _flip_impl(_RR(node), mx + 1, xr, my + 1, yr, qxl, qxr, qyl, qyr); tree_[node] = tree_[_LL(node)] + tree_[_LR(node)] + tree_[_RL(node)] + tree_[_RR(node)]; } } void flip(int qxl, int qxr, int qyl, int qyr) { _flip_impl(1, 1, N_, 1, N_, qxl, qxr, qyl, qyr); } int _count_impl(int node, int xl, int xr, int yl, int yr, int qxl, int qxr, int qyl, int qyr) { if (lazy_[node]) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } lazy_[node] = false; } if (qxl <= xl && xr <= qxr && qyl <= yl && yr <= qyr) { return tree_[node]; } else if (qxl <= xr && xl <= qxr && qyl <= yr && yl <= qyr) { int mx = (xl + xr) / 2; int my = (yl + yr) / 2; return _count_impl(_LL(node), xl, mx, yl, my, qxl, qxr, qyl, qyr) + _count_impl(_LR(node), xl, mx, my + 1, yr, qxl, qxr, qyl, qyr) + _count_impl(_RL(node), mx + 1, xr, yl, my, qxl, qxr, qyl, qyr) + _count_impl(_RR(node), mx + 1, xr, my + 1, yr, qxl, qxr, qyl, qyr); } return 0; } int count(int qxl, int qxr, int qyl, int qyr) { return _count_impl(1, 1, N_, 1, N_, qxl, qxr, qyl, qyr); } void prepare() { for (int i = 1; i <= num_nodes_; ++i) { all_xs_[i] = count(i, i, 1, N_); all_ys_[i] = count(1, N_, i, i); if (all_xs_[i] == num_nodes_) { tree_max_xs_.flip(i, i); } if (all_ys_[i] == num_nodes_) { tree_max_ys_.flip(i, i); } if (all_xs_[i] == 0) { tree_min_xs_.flip(i, i); } if (all_ys_[i] == 0) { tree_min_ys_.flip(i, i); } } } void flip_single(int qx, int qy) { _flip_impl(1, 1, N_, 1, N_, qx, qx, qy, qy); int new_all_xs = count(qx, qx, 1, N_); int new_all_ys = count(1, N_, qy, qy); if (new_all_xs != all_xs_[qx]) { if (all_xs_[qx] == num_nodes_ || new_all_xs == num_nodes_) { // was maximum and now it's not or wasn't maximum and now it is tree_max_xs_.flip(qx, qx); } if (all_xs_[qx] == 0 || new_all_xs == 0) { tree_min_xs_.flip(qx, qx); } all_xs_[qx] = new_all_xs; } if (new_all_ys != all_ys_[qy]) { if (all_ys_[qy] == num_nodes_ || new_all_ys == num_nodes_) { // was maximum and now it's not or wasn't maximum and now it is tree_max_ys_.flip(qy, qy); } if (all_ys_[qy] == 0 || new_all_ys == 0) { tree_min_ys_.flip(qy, qy); } all_ys_[qy] = new_all_ys; } } int current_ans() { int num_of_xn = tree_max_xs_.count(1, num_nodes_); int num_of_yn = tree_max_ys_.count(1, num_nodes_); int num_of_x0 = tree_min_xs_.count(1, num_nodes_); int num_of_y0 = tree_min_ys_.count(1, num_nodes_); int unreachable = num_of_x0 * num_of_y0; int cant_throw = num_of_xn * num_of_yn; int spaces = num_nodes_ * num_nodes_ - unreachable; int possible = count(1, num_nodes_, 1, num_nodes_) - cant_throw; return std::min(spaces - possible, possible); } }; int main() { std::ios::sync_with_stdio(false); int n, m, q; std::cin >> n >> m >> q; FlipSumSegTree2D tree(n); for (int i = 0; i < m; ++i) { int x1, y1, x2, y2; std::cin >> x1 >> y1 >> x2 >> y2; tree.flip(x1, x2, y1, y2); } tree.prepare(); std::cout << tree.current_ans() << std::endl; for (int i = 0; i < q; ++i) { int x, y; std::cin >> x >> y; tree.flip_single(x, y); std::cout << tree.current_ans() << std::endl; } 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 286 287 288 289 290 | #include <iostream> #include <type_traits> #include <vector> constexpr int next_pow2(int v) { --v; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; ++v; return v; } struct FlipSumSegTree1D { std::vector<int> tree_; std::vector<bool> lazy_; int N_; FlipSumSegTree1D(int num_nodes) : N_(next_pow2(num_nodes)) , tree_(2 * next_pow2(num_nodes), 0) , lazy_(2 * next_pow2(num_nodes), false) {} constexpr inline int _L(int x) { return x * 2; } constexpr inline int _R(int x) { return x * 2 + 1; } void _flip_impl(int node, int l, int r, int ql, int qr) { if (lazy_[node]) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } lazy_[node] = false; } if (ql <= l && r <= qr) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } } else if (ql <= r && l <= qr) { int mid = (l + r) / 2; _flip_impl(_L(node), l, mid, ql, qr); _flip_impl(_R(node), mid + 1, r, ql, qr); tree_[node] = tree_[_L(node)] + tree_[_R(node)]; } } void flip(int ql, int qr) { _flip_impl(1, 1, N_, ql, qr); } int _count_impl(int node, int l, int r, int ql, int qr) { if (lazy_[node]) { tree_[node] = (r - l + 1) - tree_[node]; if (l != r) { lazy_[_L(node)] = !lazy_[_L(node)]; lazy_[_R(node)] = !lazy_[_R(node)]; } lazy_[node] = false; } if (ql <= l && r <= qr) { return tree_[node]; } else if (ql <= r && l <= qr) { int mid = (l + r) / 2; return _count_impl(_L(node), l, mid, ql, qr) + _count_impl(_R(node), mid + 1, r, ql, qr); } return 0; } int count(int ql, int qr) { return _count_impl(1, 1, N_, ql, qr); } }; struct FlipSumSegTree2D { std::vector<int> tree_; std::vector<bool> lazy_; std::vector<int> all_xs_; std::vector<int> all_ys_; FlipSumSegTree1D tree_max_xs_; FlipSumSegTree1D tree_max_ys_; FlipSumSegTree1D tree_min_xs_; FlipSumSegTree1D tree_min_ys_; int N_; int num_nodes_; FlipSumSegTree2D(int num_nodes) : num_nodes_(num_nodes) , N_(next_pow2(num_nodes)) , tree_(2 * next_pow2(num_nodes) * 2 * next_pow2(num_nodes), 0) , lazy_(2 * next_pow2(num_nodes) * 2 * next_pow2(num_nodes), false) , all_xs_(num_nodes + 1, 0) , all_ys_(num_nodes + 1, 0) , tree_max_xs_(num_nodes) , tree_max_ys_(num_nodes) , tree_min_xs_(num_nodes) , tree_min_ys_(num_nodes) {} constexpr inline int _LL(int n) { return n * 4 + 0; } constexpr inline int _LR(int n) { return n * 4 + 1; } constexpr inline int _RL(int n) { return n * 4 + 2; } constexpr inline int _RR(int n) { return n * 4 + 3; } void _flip_impl(int node, int xl, int xr, int yl, int yr, int qxl, int qxr, int qyl, int qyr) { if (lazy_[node]) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } lazy_[node] = false; } if (qxl <= xl && xr <= qxr && qyl <= yl && yr <= qyr) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } } else if (qxl <= xr && xl <= qxr && qyl <= yr && yl <= qyr) { int mx = (xl + xr) / 2; int my = (yl + yr) / 2; _flip_impl(_LL(node), xl, mx, yl, my, qxl, qxr, qyl, qyr); _flip_impl(_LR(node), xl, mx, my + 1, yr, qxl, qxr, qyl, qyr); _flip_impl(_RL(node), mx + 1, xr, yl, my, qxl, qxr, qyl, qyr); _flip_impl(_RR(node), mx + 1, xr, my + 1, yr, qxl, qxr, qyl, qyr); tree_[node] = tree_[_LL(node)] + tree_[_LR(node)] + tree_[_RL(node)] + tree_[_RR(node)]; } } void flip(int qxl, int qxr, int qyl, int qyr) { _flip_impl(1, 1, N_, 1, N_, qxl, qxr, qyl, qyr); } int _count_impl(int node, int xl, int xr, int yl, int yr, int qxl, int qxr, int qyl, int qyr) { if (lazy_[node]) { tree_[node] = (xr - xl + 1) * (yr - yl + 1) - tree_[node]; if (xl != xr || yl != yr) { lazy_[_LL(node)] = !lazy_[_LL(node)]; lazy_[_LR(node)] = !lazy_[_LR(node)]; lazy_[_RL(node)] = !lazy_[_RL(node)]; lazy_[_RR(node)] = !lazy_[_RR(node)]; } lazy_[node] = false; } if (qxl <= xl && xr <= qxr && qyl <= yl && yr <= qyr) { return tree_[node]; } else if (qxl <= xr && xl <= qxr && qyl <= yr && yl <= qyr) { int mx = (xl + xr) / 2; int my = (yl + yr) / 2; return _count_impl(_LL(node), xl, mx, yl, my, qxl, qxr, qyl, qyr) + _count_impl(_LR(node), xl, mx, my + 1, yr, qxl, qxr, qyl, qyr) + _count_impl(_RL(node), mx + 1, xr, yl, my, qxl, qxr, qyl, qyr) + _count_impl(_RR(node), mx + 1, xr, my + 1, yr, qxl, qxr, qyl, qyr); } return 0; } int count(int qxl, int qxr, int qyl, int qyr) { return _count_impl(1, 1, N_, 1, N_, qxl, qxr, qyl, qyr); } void prepare() { for (int i = 1; i <= num_nodes_; ++i) { all_xs_[i] = count(i, i, 1, N_); all_ys_[i] = count(1, N_, i, i); if (all_xs_[i] == num_nodes_) { tree_max_xs_.flip(i, i); } if (all_ys_[i] == num_nodes_) { tree_max_ys_.flip(i, i); } if (all_xs_[i] == 0) { tree_min_xs_.flip(i, i); } if (all_ys_[i] == 0) { tree_min_ys_.flip(i, i); } } } void flip_single(int qx, int qy) { _flip_impl(1, 1, N_, 1, N_, qx, qx, qy, qy); int new_all_xs = count(qx, qx, 1, N_); int new_all_ys = count(1, N_, qy, qy); if (new_all_xs != all_xs_[qx]) { if (all_xs_[qx] == num_nodes_ || new_all_xs == num_nodes_) { // was maximum and now it's not or wasn't maximum and now it is tree_max_xs_.flip(qx, qx); } if (all_xs_[qx] == 0 || new_all_xs == 0) { tree_min_xs_.flip(qx, qx); } all_xs_[qx] = new_all_xs; } if (new_all_ys != all_ys_[qy]) { if (all_ys_[qy] == num_nodes_ || new_all_ys == num_nodes_) { // was maximum and now it's not or wasn't maximum and now it is tree_max_ys_.flip(qy, qy); } if (all_ys_[qy] == 0 || new_all_ys == 0) { tree_min_ys_.flip(qy, qy); } all_ys_[qy] = new_all_ys; } } int current_ans() { int num_of_xn = tree_max_xs_.count(1, num_nodes_); int num_of_yn = tree_max_ys_.count(1, num_nodes_); int num_of_x0 = tree_min_xs_.count(1, num_nodes_); int num_of_y0 = tree_min_ys_.count(1, num_nodes_); int unreachable = num_of_x0 * num_of_y0; int cant_throw = num_of_xn * num_of_yn; int spaces = num_nodes_ * num_nodes_ - unreachable; int possible = count(1, num_nodes_, 1, num_nodes_) - cant_throw; return std::min(spaces - possible, possible); } }; int main() { std::ios::sync_with_stdio(false); int n, m, q; std::cin >> n >> m >> q; FlipSumSegTree2D tree(n); for (int i = 0; i < m; ++i) { int x1, y1, x2, y2; std::cin >> x1 >> y1 >> x2 >> y2; tree.flip(x1, x2, y1, y2); } tree.prepare(); std::cout << tree.current_ans() << std::endl; for (int i = 0; i < q; ++i) { int x, y; std::cin >> x >> y; tree.flip_single(x, y); std::cout << tree.current_ans() << std::endl; } return 0; } |