#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 3e5 + 7; const int T = 1 << 19; int n, m, q; PII Queries[N]; pair <PII, PII> Rectangle[N]; vector <int> Q[N]; map <PII, int> QueryPoints; namespace Tree { bool lazy[T + T]; int tree[T + T][2]; void clear() { for(int i = 0; i < T + T; ++i) { tree[i][0] = tree[i][1] = 0; lazy[i] = false; } } inline void update(const int &cur) { tree[cur][0] = tree[cur + cur][0] + tree[cur + cur + 1][0]; tree[cur][1] = tree[cur + cur][1] + tree[cur + cur + 1][1]; } inline void propagate(const int &cur) { if(lazy[cur] && cur < T) { lazy[cur] = false; lazy[cur + cur] ^= 1; lazy[cur + cur + 1] ^= 1; swap(tree[cur + cur][0], tree[cur + cur][1]); swap(tree[cur + cur + 1][0], tree[cur + cur + 1][1]); } } void init() { clear(); for(int i = 1; i <= n; ++i) tree[T + i][0] = 1; for(int i = T - 1; i >= 1; --i) update(i); } void push(int s, int e, int cur = 1, int from = 0, int to = T - 1) { if(s <= from && to <= e) { lazy[cur] ^= 1; swap(tree[cur][0], tree[cur][1]); return; } int mid = (from + to) / 2; propagate(cur); if(s <= mid) push(s, e, cur + cur, from, mid); if(mid < e) push(s, e, cur + cur + 1, mid + 1, to); update(cur); } int getVal(int p, int cur = 1, int from = 0, int to = T - 1) { if(from == to) return tree[cur][0] ? 0 : 1; int mid = (from + to) / 2; propagate(cur); if(p <= mid) return getVal(p, cur + cur, from, mid); return getVal(p, cur + cur + 1, mid + 1, to); } } namespace Solve { LL all = 0; PLL tree[T + T]; int rows[N]; int cols[N]; int cnt[N]; int order[T + T]; void add_tree(int p, int v) { p += T; while(p) { order[p] += v; p >>= 1; } } int ask_tree(int from, int to) { int ret = 0; from += T, to += T; while(from <= to) { if(from & 1) ret += order[from]; if(!(to & 1)) ret += order[to]; from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void push(int s, int e, LL v, int cur = 1, int from = 0, int to = T - 1) { if(s <= from && to <= e) { tree[cur].st += v; tree[cur].nd += v; return; } int mid = (from + to) / 2; if(s <= mid) push(s, e, v, cur + cur, from, mid); if(mid < e) push(s, e, v, cur + cur + 1, mid + 1, to); tree[cur].st = max(tree[cur + cur].st, tree[cur + cur + 1].st) + tree[cur].nd; } void sub(int x, int y) { ++all; /* update rows */ add_tree(rows[x]--, -1); push(ask_tree(0, rows[x]) + 1, n, 1); add_tree(rows[x], 1); /* update columns */ push(cols[y]--, n, 1); } void add(int x, int y) { --all; /* update rows */ push(ask_tree(0, rows[x]), n, -1); add_tree(rows[x]++, -1); add_tree(rows[x], 1); /* update columns */ push(++cols[y], n, -1); } void update(int x, int y) { int cur = QueryPoints[{x, y}]; QueryPoints[{x, y}] ^= 1; if(cur) add(x, y); else sub(x, y); } LL get_ans() { return all - tree[1].st; } void init() { vector <int> sorted_rows(rows + 1, rows + n + 1); sort(sorted_rows.begin(), sorted_rows.end()); all = 1LL * n * n; for(int i = 1; i <= n; ++i) { push(i, n, -sorted_rows[i - 1]); all -= rows[i]; add_tree(rows[i], 1); } for(int i = 1; i <= n; ++i) cnt[cols[i]]++; int nr = 0; LL sum = 0; for(int i = 0; i <= n; ++i) { nr += cnt[i]; sum += 1LL * i * cnt[i]; push(i, i, 1LL * nr * i - sum); } } } void prepare(int *ans, bool answer_queries = false) { vector <vector <PII> > Order(n + 2); for(int i = 1; i <= m; ++i) { auto [s, e] = Rectangle[i].st; Order[s].push_back(Rectangle[i].nd); Order[e + 1].push_back(Rectangle[i].nd); } Tree::init(); for(int i = 1; i <= n; ++i) { for(auto [s, e]: Order[i]) Tree::push(s, e); ans[i] = Tree::tree[1][0]; if(answer_queries) { for(auto v: Q[i]) QueryPoints[{i, v}] = Tree::getVal(v); } } /* printf("we computes ans = ["); for(int i = 1; i <= n; ++i) printf("%d%c ", ans[i], ",]"[i == n]); puts("");*/ } int main() { scanf("%d %d %d", &n, &m, &q); for(int i = 1; i <= m; ++i) { int sx, sy, ex, ey; scanf("%d %d %d %d", &sx, &sy, &ex, &ey); Rectangle[i] = {{sx, ex}, {sy, ey}}; } for(int i = 1; i <= q; ++i) { auto &[x, y] = Queries[i]; scanf("%d %d", &x, &y); Q[x].push_back(y); } prepare(Solve::rows, true); for(int i = 1; i <= m; ++i) swap(Rectangle[i].st, Rectangle[i].nd); prepare(Solve::cols); Solve::init(); printf("%lld\n", Solve::get_ans()); for(int i = 1; i <= q; ++i) { auto &[x, y] = Queries[i]; Solve::update(x, y); printf("%lld\n", Solve::get_ans()); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 3e5 + 7; const int T = 1 << 19; int n, m, q; PII Queries[N]; pair <PII, PII> Rectangle[N]; vector <int> Q[N]; map <PII, int> QueryPoints; namespace Tree { bool lazy[T + T]; int tree[T + T][2]; void clear() { for(int i = 0; i < T + T; ++i) { tree[i][0] = tree[i][1] = 0; lazy[i] = false; } } inline void update(const int &cur) { tree[cur][0] = tree[cur + cur][0] + tree[cur + cur + 1][0]; tree[cur][1] = tree[cur + cur][1] + tree[cur + cur + 1][1]; } inline void propagate(const int &cur) { if(lazy[cur] && cur < T) { lazy[cur] = false; lazy[cur + cur] ^= 1; lazy[cur + cur + 1] ^= 1; swap(tree[cur + cur][0], tree[cur + cur][1]); swap(tree[cur + cur + 1][0], tree[cur + cur + 1][1]); } } void init() { clear(); for(int i = 1; i <= n; ++i) tree[T + i][0] = 1; for(int i = T - 1; i >= 1; --i) update(i); } void push(int s, int e, int cur = 1, int from = 0, int to = T - 1) { if(s <= from && to <= e) { lazy[cur] ^= 1; swap(tree[cur][0], tree[cur][1]); return; } int mid = (from + to) / 2; propagate(cur); if(s <= mid) push(s, e, cur + cur, from, mid); if(mid < e) push(s, e, cur + cur + 1, mid + 1, to); update(cur); } int getVal(int p, int cur = 1, int from = 0, int to = T - 1) { if(from == to) return tree[cur][0] ? 0 : 1; int mid = (from + to) / 2; propagate(cur); if(p <= mid) return getVal(p, cur + cur, from, mid); return getVal(p, cur + cur + 1, mid + 1, to); } } namespace Solve { LL all = 0; PLL tree[T + T]; int rows[N]; int cols[N]; int cnt[N]; int order[T + T]; void add_tree(int p, int v) { p += T; while(p) { order[p] += v; p >>= 1; } } int ask_tree(int from, int to) { int ret = 0; from += T, to += T; while(from <= to) { if(from & 1) ret += order[from]; if(!(to & 1)) ret += order[to]; from = (from + 1) >> 1; to = (to - 1) >> 1; } return ret; } void push(int s, int e, LL v, int cur = 1, int from = 0, int to = T - 1) { if(s <= from && to <= e) { tree[cur].st += v; tree[cur].nd += v; return; } int mid = (from + to) / 2; if(s <= mid) push(s, e, v, cur + cur, from, mid); if(mid < e) push(s, e, v, cur + cur + 1, mid + 1, to); tree[cur].st = max(tree[cur + cur].st, tree[cur + cur + 1].st) + tree[cur].nd; } void sub(int x, int y) { ++all; /* update rows */ add_tree(rows[x]--, -1); push(ask_tree(0, rows[x]) + 1, n, 1); add_tree(rows[x], 1); /* update columns */ push(cols[y]--, n, 1); } void add(int x, int y) { --all; /* update rows */ push(ask_tree(0, rows[x]), n, -1); add_tree(rows[x]++, -1); add_tree(rows[x], 1); /* update columns */ push(++cols[y], n, -1); } void update(int x, int y) { int cur = QueryPoints[{x, y}]; QueryPoints[{x, y}] ^= 1; if(cur) add(x, y); else sub(x, y); } LL get_ans() { return all - tree[1].st; } void init() { vector <int> sorted_rows(rows + 1, rows + n + 1); sort(sorted_rows.begin(), sorted_rows.end()); all = 1LL * n * n; for(int i = 1; i <= n; ++i) { push(i, n, -sorted_rows[i - 1]); all -= rows[i]; add_tree(rows[i], 1); } for(int i = 1; i <= n; ++i) cnt[cols[i]]++; int nr = 0; LL sum = 0; for(int i = 0; i <= n; ++i) { nr += cnt[i]; sum += 1LL * i * cnt[i]; push(i, i, 1LL * nr * i - sum); } } } void prepare(int *ans, bool answer_queries = false) { vector <vector <PII> > Order(n + 2); for(int i = 1; i <= m; ++i) { auto [s, e] = Rectangle[i].st; Order[s].push_back(Rectangle[i].nd); Order[e + 1].push_back(Rectangle[i].nd); } Tree::init(); for(int i = 1; i <= n; ++i) { for(auto [s, e]: Order[i]) Tree::push(s, e); ans[i] = Tree::tree[1][0]; if(answer_queries) { for(auto v: Q[i]) QueryPoints[{i, v}] = Tree::getVal(v); } } /* printf("we computes ans = ["); for(int i = 1; i <= n; ++i) printf("%d%c ", ans[i], ",]"[i == n]); puts("");*/ } int main() { scanf("%d %d %d", &n, &m, &q); for(int i = 1; i <= m; ++i) { int sx, sy, ex, ey; scanf("%d %d %d %d", &sx, &sy, &ex, &ey); Rectangle[i] = {{sx, ex}, {sy, ey}}; } for(int i = 1; i <= q; ++i) { auto &[x, y] = Queries[i]; scanf("%d %d", &x, &y); Q[x].push_back(y); } prepare(Solve::rows, true); for(int i = 1; i <= m; ++i) swap(Rectangle[i].st, Rectangle[i].nd); prepare(Solve::cols); Solve::init(); printf("%lld\n", Solve::get_ans()); for(int i = 1; i <= q; ++i) { auto &[x, y] = Queries[i]; Solve::update(x, y); printf("%lld\n", Solve::get_ans()); } return 0; } |