#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,abm,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long //#define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) #define cerr if(0)cout #endif const int kDontSetX = 0x77667766; // Splay z biblioteczki ACM-kowej struct node{ node *l, *r, *p; int x, idx, set_x; node(int x_, int idx_): l(0), r(0), p(0), x(x_), idx(idx_), set_x(kDontSetX) {} void update(){} void touch() { if (set_x != kDontSetX) { x = set_x; if (l) { l->x = set_x; l->set_x = set_x; } if (r) { r->x = set_x; r->set_x = set_x; } set_x = kDontSetX; } } node*& get_child(bool right){ return right ? r : l; } static void add_child(node* x, node* q, bool right){ if(x) x->get_child(right) = q; if(q) q->p = x; } inline bool is_right(){ return p && p->r == this; } void rotate(){ if(!p) return; node *oldp = p; bool right = is_right(); add_child(p->p, this, p->is_right()); add_child(oldp, get_child(!right), right); add_child(this, oldp, !right); oldp->update(); update(); } void splay(){ while(p){ if(is_right() ^ p->is_right()) rotate(); else p->rotate(); rotate(); } } void splay_() { splay(); } node* remove() { if(l) l->p = nullptr; if(r) r->p = nullptr; node* root = join(l, r); l = r = nullptr; return root; } static node* join(node* a, node* b){ if(!a) return b; while(1){ a->touch(); if(!a->r) break; a = a->r; } a->splay_(); add_child(a, b, true); a->update(); return a; } vector<PII> traverse() { vector<PII> answer; function<void(node*)> run = [&](node *cur) { cur->touch(); if (cur->l) { run(cur->l); } answer.emplace_back(cur->idx, cur->x); if (cur->r) { run(cur->r); } }; run(this); return answer; } pair<node*, node*> split_by_x(int bound_x) { // get (<x, >=x) node* geq = nullptr; node* ptr = this; node* last = this; // Do lower bound first while (ptr) { ptr->touch(); last = ptr; if (ptr->x >= bound_x) { geq = ptr; ptr = ptr->l; } else { ptr = ptr->r; } } last->splay(); if (!geq) { return {last, nullptr}; } geq->splay(); node *left = geq->l; if (left) { left->p = nullptr; geq->l = nullptr; } return {left, geq}; } ~node(){ delete l; delete r; } }; struct LineTrees { node *tree_vert, *tree_horiz; int A, B; LineTrees(int L, int R, int idx) : tree_vert(0), tree_horiz(0), A(L), B(R) { if (idx != -1) { tree_vert = new node(R, idx); } } void Add(int idx) { tree_vert = node::join(tree_vert, new node(B, idx)); } void SplitBy(int L, int R, int v) { debug("SplitBy", L, R, v, "", A, B, GetFinalIntvs()); node *next_tree_vert = nullptr; node *next_tree_horiz = nullptr; if (B < v) { debug("B<v", A); if (tree_vert) { tree_vert->set_x = A; } debug(tree_vert->traverse()); next_tree_vert = node::join(tree_vert, tree_horiz); } else if (A >= v) { if (tree_horiz) { tree_horiz->set_x = B; } next_tree_horiz = node::join(tree_vert, tree_horiz); } else { debug("inside"); node *vert_low = 0, *vert_high = 0, *horiz_left = 0, *horiz_right = 0; if (tree_vert) { tie(vert_low, vert_high) = tree_vert->split_by_x(v); } if (tree_horiz) { tie(horiz_left, horiz_right) = tree_horiz->split_by_x(v); } if (vert_low) { vert_low->set_x = A; } if (vert_high) { vert_high->set_x = R; } if (horiz_left) { horiz_left->set_x = L; } if (horiz_right) { horiz_right->set_x = B; } next_tree_vert = node::join(vert_low, vert_high); next_tree_horiz = node::join(horiz_left, horiz_right); } tree_vert = next_tree_vert; tree_horiz = next_tree_horiz; A = L; B = R; } void MergeWith(LineTrees &right) { assert(A == right.A); assert(B == right.B); tree_vert = node::join(tree_vert, right.tree_vert); tree_horiz = node::join(tree_horiz, right.tree_horiz); } vector<VI> GetFinalIntvs() { vector<VI> answer; auto verts = tree_vert ? tree_vert->traverse() : vector<PII>(); auto horizs = tree_horiz ? tree_horiz->traverse() : vector<PII>(); for (auto &vert : verts) answer.PB({vert.st, A, vert.nd}); for (auto &horiz : horizs) answer.PB({horiz.st, horiz.nd, B}); return answer; } }; struct FindUnion { private: vector<int> parent, min_elem, max_elem; vector<bool> is_active; int bias, N; public: FindUnion(int min_x, int max_x) { N = max_x - min_x + 3; bias = -min_x + 1; is_active.resize(N, true); parent.resize(N); iota(ALL(parent), 0); min_elem = max_elem = parent; } int Find(int v) { return FindInternal(v + bias) - bias; } void Union(int a, int b) { return UnionInternal(a + bias, b + bias); } bool IsActive(int v) const { return is_active[v + bias]; } void MarkInactive(int v) { return MarkInactiveInternal(v + bias); } PII GetMarkedIntv(int v) { auto res = GetMarkedIntvInternal(v + bias); return {res.st - bias, res.nd - bias}; } private: int FindInternal(int v) { if (v == parent[v]) return v; return parent[v] = FindInternal(parent[v]); } void UnionInternal(int a, int b) { debug("Union", a-bias, b-bias); a = FindInternal(a); b = FindInternal(b); if (a != b) { parent[a] = b; mini(min_elem[b], min_elem[a]); maxi(max_elem[b], max_elem[a]); } } void MarkInactiveInternal(int v) { is_active[v] = false; for (int s : {v - 1, v + 1}) { if (!is_active[s]) { UnionInternal(s, v); } } } PII GetMarkedIntvInternal(int v) { v = FindInternal(v); return {min_elem[v], max_elem[v]}; } }; vector<VI> SolveExact1D(VI xs) { debug(xs); const int N = SZ(xs); const int max_x = *max_element(ALL(xs)); FindUnion fu(-N, max_x + N); map<int, LineTrees> line_trees; for (int i = 0; i < SZ(xs); ++i) { const int x = xs[i]; int L, R; R = fu.IsActive(x) ? x : fu.GetMarkedIntv(x).nd + 1; L = fu.IsActive(x - 1) ? x - 1 : fu.GetMarkedIntv(x - 1).st - 1; fu.MarkInactive(L); fu.MarkInactive(R); debug(x, L, R); LineTrees new_lines(L, R, -1); auto lt_iter = line_trees.lower_bound(L); vector<map<int, LineTrees>::iterator> to_erase; while (lt_iter != line_trees.end() && lt_iter->st < R) { LineTrees lt = lt_iter->nd; lt.SplitBy(L, R, x); new_lines.MergeWith(lt); to_erase.PB(lt_iter); ++lt_iter; } for (auto del_lt_iter : to_erase) line_trees.erase(del_lt_iter); new_lines.Add(i); line_trees.emplace(L, new_lines); } vector<VI> final_intvs; for (auto &line_tree : line_trees) { debug(line_tree.second.GetFinalIntvs()); for (auto &v : line_tree.second.GetFinalIntvs()) { final_intvs.PB(v); } delete line_tree.nd.tree_vert; delete line_tree.nd.tree_horiz; } vector<int> remaining_links; for (int i = -N; i <= max_x; ++i) if (fu.IsActive(i)) remaining_links.PB(i); debug(remaining_links); vector<VI> bear_info(N); for (int i = 0; i < N; ++i) { const int x = xs[i]; const int dist_minus_n = lower_bound(ALL(remaining_links), x) - remaining_links.begin(); bear_info[i] = VI{dist_minus_n, 0, 0}; } for (auto &final_intv : final_intvs) { const int who = final_intv[0]; bear_info[who][1] = final_intv[1]; bear_info[who][2] = final_intv[2]; } return bear_info; } template <typename T> struct SumTree { vector<T> data; int Base; SumTree(int N) : Base(1) { while (Base < N + 3) { Base *= 2; } data.resize(Base * 2); } void Add(int p, T v) { debug("Add", p, v); p += Base; while (p) { data[p] += v; p /= 2; } } T GetSum(int L, int R) const { maxi<int>(L, 0); mini(R, Base - 1); if (L > R) { return 0; } L += Base; R += Base; if (L == R) { return data[L]; } T ans = data[L] + data[R]; while (L / 2 != R / 2) { if (L % 2 == 0) { ans += data[L + 1]; } if (R % 2 == 1) { ans += data[R - 1]; } L /= 2; R /= 2; } debug("GetSum", L, R, ans); return ans; } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int N = 250 * 1000; cin >> N; vector<int> xs(N), ys(N); /*srand(2500); for (int i = 0; i < N; ++i) { xs[i] = rand() % (1000 * 1000) + 1; ys[i] = 1; //ys[i] = rand() % (1000 * 1000) + 1; }*/ for (int i = 0; i < N; ++i) { cin >> xs[i] >> ys[i]; } auto x_info = SolveExact1D(xs); auto y_info = SolveExact1D(ys); const int max_x = *max_element(ALL(xs)); const int max_y = *max_element(ALL(ys)); debug(x_info, y_info); SumTree<LL> x_final_tree(max_x + 2); SumTree<LL> y_final_tree(max_y + 2); LL sum_base = 0; for (int i = 0; i < N; ++i) { sum_base += (LL)x_info[i][0] * y_info[i][0]; x_final_tree.Add(xs[i], y_info[i][0]); y_final_tree.Add(ys[i], x_info[i][0]); } vector<LL> answers(N, sum_base); for (int i = 0; i < N; ++i) { answers[i] -= (LL)x_info[i][0] * y_info[i][0]; answers[i] += (LL)xs[i] * ys[i]; } debug(answers); for (int i = 0; i < N; ++i) { const int x1 = x_info[i][1]; const int x2 = x_info[i][2]; x_final_tree.Add(xs[i], -y_info[i][0]); answers[i] += x_final_tree.GetSum(x2, max_x); answers[i] -= x_final_tree.GetSum(0, x1 - 1); x_final_tree.Add(xs[i], y_info[i][0]); const int y1 = y_info[i][1]; const int y2 = y_info[i][2]; y_final_tree.Add(ys[i], -x_info[i][0]); answers[i] += y_final_tree.GetSum(y2, max_y); answers[i] -= y_final_tree.GetSum(0, y1 - 1); y_final_tree.Add(ys[i], x_info[i][0]); } debug(answers); SumTree<int> y_pref_count_tree(max_y + 1), y_suf_count_tree(max_y + 1); vector<VI> points_for_x(max_x + 2); vector<VI> x1_for_x(max_x + 2); vector<VI> x2_for_x(max_x + 2); for (int i = 0; i < N; ++i) { y_suf_count_tree.Add(ys[i], 1); points_for_x[xs[i]].PB(i); const int x1 = max<int>(0, min(max_x + 1, x_info[i][1])); const int x2 = max<int>(0, min(max_x + 1, x_info[i][2])); x1_for_x[x1].PB(i); x2_for_x[x2].PB(i); } for (int x = 0; x <= max_x + 1; ++x) { debug(x); for (int bear : x1_for_x[x]) { debug(bear, "x1"); const int y1 = y_info[bear][1]; const int y2 = y_info[bear][2]; answers[bear] -= y_pref_count_tree.GetSum(y2, max_y); answers[bear] += y_pref_count_tree.GetSum(0, y1 - 1); } for (int bear : x2_for_x[x]) { debug(bear, "x2"); const int y1 = y_info[bear][1]; const int y2 = y_info[bear][2]; answers[bear] += y_suf_count_tree.GetSum(y2, max_y); answers[bear] -= y_suf_count_tree.GetSum(0, y1 - 1); } for (int bear : points_for_x[x]) { debug(bear, "point"); y_pref_count_tree.Add(ys[bear], 1); y_suf_count_tree.Add(ys[bear], -1); } } for (int i = 0; i < N; ++i) { const bool is_right = xs[i] >= x_info[i][2]; const bool is_left = xs[i] < x_info[i][1]; const bool is_top = ys[i] >= y_info[i][2]; const bool is_bottom = ys[i] < y_info[i][1]; if (is_right + is_left + is_top + is_bottom == 2) { const bool was_added = is_right ^ is_bottom; answers[i] -= was_added ? 1 : -1; } } for (int i = 0; i < N; ++i) { cout << answers[i] << "\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 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,abm,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long //#define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) #define cerr if(0)cout #endif const int kDontSetX = 0x77667766; // Splay z biblioteczki ACM-kowej struct node{ node *l, *r, *p; int x, idx, set_x; node(int x_, int idx_): l(0), r(0), p(0), x(x_), idx(idx_), set_x(kDontSetX) {} void update(){} void touch() { if (set_x != kDontSetX) { x = set_x; if (l) { l->x = set_x; l->set_x = set_x; } if (r) { r->x = set_x; r->set_x = set_x; } set_x = kDontSetX; } } node*& get_child(bool right){ return right ? r : l; } static void add_child(node* x, node* q, bool right){ if(x) x->get_child(right) = q; if(q) q->p = x; } inline bool is_right(){ return p && p->r == this; } void rotate(){ if(!p) return; node *oldp = p; bool right = is_right(); add_child(p->p, this, p->is_right()); add_child(oldp, get_child(!right), right); add_child(this, oldp, !right); oldp->update(); update(); } void splay(){ while(p){ if(is_right() ^ p->is_right()) rotate(); else p->rotate(); rotate(); } } void splay_() { splay(); } node* remove() { if(l) l->p = nullptr; if(r) r->p = nullptr; node* root = join(l, r); l = r = nullptr; return root; } static node* join(node* a, node* b){ if(!a) return b; while(1){ a->touch(); if(!a->r) break; a = a->r; } a->splay_(); add_child(a, b, true); a->update(); return a; } vector<PII> traverse() { vector<PII> answer; function<void(node*)> run = [&](node *cur) { cur->touch(); if (cur->l) { run(cur->l); } answer.emplace_back(cur->idx, cur->x); if (cur->r) { run(cur->r); } }; run(this); return answer; } pair<node*, node*> split_by_x(int bound_x) { // get (<x, >=x) node* geq = nullptr; node* ptr = this; node* last = this; // Do lower bound first while (ptr) { ptr->touch(); last = ptr; if (ptr->x >= bound_x) { geq = ptr; ptr = ptr->l; } else { ptr = ptr->r; } } last->splay(); if (!geq) { return {last, nullptr}; } geq->splay(); node *left = geq->l; if (left) { left->p = nullptr; geq->l = nullptr; } return {left, geq}; } ~node(){ delete l; delete r; } }; struct LineTrees { node *tree_vert, *tree_horiz; int A, B; LineTrees(int L, int R, int idx) : tree_vert(0), tree_horiz(0), A(L), B(R) { if (idx != -1) { tree_vert = new node(R, idx); } } void Add(int idx) { tree_vert = node::join(tree_vert, new node(B, idx)); } void SplitBy(int L, int R, int v) { debug("SplitBy", L, R, v, "", A, B, GetFinalIntvs()); node *next_tree_vert = nullptr; node *next_tree_horiz = nullptr; if (B < v) { debug("B<v", A); if (tree_vert) { tree_vert->set_x = A; } debug(tree_vert->traverse()); next_tree_vert = node::join(tree_vert, tree_horiz); } else if (A >= v) { if (tree_horiz) { tree_horiz->set_x = B; } next_tree_horiz = node::join(tree_vert, tree_horiz); } else { debug("inside"); node *vert_low = 0, *vert_high = 0, *horiz_left = 0, *horiz_right = 0; if (tree_vert) { tie(vert_low, vert_high) = tree_vert->split_by_x(v); } if (tree_horiz) { tie(horiz_left, horiz_right) = tree_horiz->split_by_x(v); } if (vert_low) { vert_low->set_x = A; } if (vert_high) { vert_high->set_x = R; } if (horiz_left) { horiz_left->set_x = L; } if (horiz_right) { horiz_right->set_x = B; } next_tree_vert = node::join(vert_low, vert_high); next_tree_horiz = node::join(horiz_left, horiz_right); } tree_vert = next_tree_vert; tree_horiz = next_tree_horiz; A = L; B = R; } void MergeWith(LineTrees &right) { assert(A == right.A); assert(B == right.B); tree_vert = node::join(tree_vert, right.tree_vert); tree_horiz = node::join(tree_horiz, right.tree_horiz); } vector<VI> GetFinalIntvs() { vector<VI> answer; auto verts = tree_vert ? tree_vert->traverse() : vector<PII>(); auto horizs = tree_horiz ? tree_horiz->traverse() : vector<PII>(); for (auto &vert : verts) answer.PB({vert.st, A, vert.nd}); for (auto &horiz : horizs) answer.PB({horiz.st, horiz.nd, B}); return answer; } }; struct FindUnion { private: vector<int> parent, min_elem, max_elem; vector<bool> is_active; int bias, N; public: FindUnion(int min_x, int max_x) { N = max_x - min_x + 3; bias = -min_x + 1; is_active.resize(N, true); parent.resize(N); iota(ALL(parent), 0); min_elem = max_elem = parent; } int Find(int v) { return FindInternal(v + bias) - bias; } void Union(int a, int b) { return UnionInternal(a + bias, b + bias); } bool IsActive(int v) const { return is_active[v + bias]; } void MarkInactive(int v) { return MarkInactiveInternal(v + bias); } PII GetMarkedIntv(int v) { auto res = GetMarkedIntvInternal(v + bias); return {res.st - bias, res.nd - bias}; } private: int FindInternal(int v) { if (v == parent[v]) return v; return parent[v] = FindInternal(parent[v]); } void UnionInternal(int a, int b) { debug("Union", a-bias, b-bias); a = FindInternal(a); b = FindInternal(b); if (a != b) { parent[a] = b; mini(min_elem[b], min_elem[a]); maxi(max_elem[b], max_elem[a]); } } void MarkInactiveInternal(int v) { is_active[v] = false; for (int s : {v - 1, v + 1}) { if (!is_active[s]) { UnionInternal(s, v); } } } PII GetMarkedIntvInternal(int v) { v = FindInternal(v); return {min_elem[v], max_elem[v]}; } }; vector<VI> SolveExact1D(VI xs) { debug(xs); const int N = SZ(xs); const int max_x = *max_element(ALL(xs)); FindUnion fu(-N, max_x + N); map<int, LineTrees> line_trees; for (int i = 0; i < SZ(xs); ++i) { const int x = xs[i]; int L, R; R = fu.IsActive(x) ? x : fu.GetMarkedIntv(x).nd + 1; L = fu.IsActive(x - 1) ? x - 1 : fu.GetMarkedIntv(x - 1).st - 1; fu.MarkInactive(L); fu.MarkInactive(R); debug(x, L, R); LineTrees new_lines(L, R, -1); auto lt_iter = line_trees.lower_bound(L); vector<map<int, LineTrees>::iterator> to_erase; while (lt_iter != line_trees.end() && lt_iter->st < R) { LineTrees lt = lt_iter->nd; lt.SplitBy(L, R, x); new_lines.MergeWith(lt); to_erase.PB(lt_iter); ++lt_iter; } for (auto del_lt_iter : to_erase) line_trees.erase(del_lt_iter); new_lines.Add(i); line_trees.emplace(L, new_lines); } vector<VI> final_intvs; for (auto &line_tree : line_trees) { debug(line_tree.second.GetFinalIntvs()); for (auto &v : line_tree.second.GetFinalIntvs()) { final_intvs.PB(v); } delete line_tree.nd.tree_vert; delete line_tree.nd.tree_horiz; } vector<int> remaining_links; for (int i = -N; i <= max_x; ++i) if (fu.IsActive(i)) remaining_links.PB(i); debug(remaining_links); vector<VI> bear_info(N); for (int i = 0; i < N; ++i) { const int x = xs[i]; const int dist_minus_n = lower_bound(ALL(remaining_links), x) - remaining_links.begin(); bear_info[i] = VI{dist_minus_n, 0, 0}; } for (auto &final_intv : final_intvs) { const int who = final_intv[0]; bear_info[who][1] = final_intv[1]; bear_info[who][2] = final_intv[2]; } return bear_info; } template <typename T> struct SumTree { vector<T> data; int Base; SumTree(int N) : Base(1) { while (Base < N + 3) { Base *= 2; } data.resize(Base * 2); } void Add(int p, T v) { debug("Add", p, v); p += Base; while (p) { data[p] += v; p /= 2; } } T GetSum(int L, int R) const { maxi<int>(L, 0); mini(R, Base - 1); if (L > R) { return 0; } L += Base; R += Base; if (L == R) { return data[L]; } T ans = data[L] + data[R]; while (L / 2 != R / 2) { if (L % 2 == 0) { ans += data[L + 1]; } if (R % 2 == 1) { ans += data[R - 1]; } L /= 2; R /= 2; } debug("GetSum", L, R, ans); return ans; } }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int N = 250 * 1000; cin >> N; vector<int> xs(N), ys(N); /*srand(2500); for (int i = 0; i < N; ++i) { xs[i] = rand() % (1000 * 1000) + 1; ys[i] = 1; //ys[i] = rand() % (1000 * 1000) + 1; }*/ for (int i = 0; i < N; ++i) { cin >> xs[i] >> ys[i]; } auto x_info = SolveExact1D(xs); auto y_info = SolveExact1D(ys); const int max_x = *max_element(ALL(xs)); const int max_y = *max_element(ALL(ys)); debug(x_info, y_info); SumTree<LL> x_final_tree(max_x + 2); SumTree<LL> y_final_tree(max_y + 2); LL sum_base = 0; for (int i = 0; i < N; ++i) { sum_base += (LL)x_info[i][0] * y_info[i][0]; x_final_tree.Add(xs[i], y_info[i][0]); y_final_tree.Add(ys[i], x_info[i][0]); } vector<LL> answers(N, sum_base); for (int i = 0; i < N; ++i) { answers[i] -= (LL)x_info[i][0] * y_info[i][0]; answers[i] += (LL)xs[i] * ys[i]; } debug(answers); for (int i = 0; i < N; ++i) { const int x1 = x_info[i][1]; const int x2 = x_info[i][2]; x_final_tree.Add(xs[i], -y_info[i][0]); answers[i] += x_final_tree.GetSum(x2, max_x); answers[i] -= x_final_tree.GetSum(0, x1 - 1); x_final_tree.Add(xs[i], y_info[i][0]); const int y1 = y_info[i][1]; const int y2 = y_info[i][2]; y_final_tree.Add(ys[i], -x_info[i][0]); answers[i] += y_final_tree.GetSum(y2, max_y); answers[i] -= y_final_tree.GetSum(0, y1 - 1); y_final_tree.Add(ys[i], x_info[i][0]); } debug(answers); SumTree<int> y_pref_count_tree(max_y + 1), y_suf_count_tree(max_y + 1); vector<VI> points_for_x(max_x + 2); vector<VI> x1_for_x(max_x + 2); vector<VI> x2_for_x(max_x + 2); for (int i = 0; i < N; ++i) { y_suf_count_tree.Add(ys[i], 1); points_for_x[xs[i]].PB(i); const int x1 = max<int>(0, min(max_x + 1, x_info[i][1])); const int x2 = max<int>(0, min(max_x + 1, x_info[i][2])); x1_for_x[x1].PB(i); x2_for_x[x2].PB(i); } for (int x = 0; x <= max_x + 1; ++x) { debug(x); for (int bear : x1_for_x[x]) { debug(bear, "x1"); const int y1 = y_info[bear][1]; const int y2 = y_info[bear][2]; answers[bear] -= y_pref_count_tree.GetSum(y2, max_y); answers[bear] += y_pref_count_tree.GetSum(0, y1 - 1); } for (int bear : x2_for_x[x]) { debug(bear, "x2"); const int y1 = y_info[bear][1]; const int y2 = y_info[bear][2]; answers[bear] += y_suf_count_tree.GetSum(y2, max_y); answers[bear] -= y_suf_count_tree.GetSum(0, y1 - 1); } for (int bear : points_for_x[x]) { debug(bear, "point"); y_pref_count_tree.Add(ys[bear], 1); y_suf_count_tree.Add(ys[bear], -1); } } for (int i = 0; i < N; ++i) { const bool is_right = xs[i] >= x_info[i][2]; const bool is_left = xs[i] < x_info[i][1]; const bool is_top = ys[i] >= y_info[i][2]; const bool is_bottom = ys[i] < y_info[i][1]; if (is_right + is_left + is_top + is_bottom == 2) { const bool was_added = is_right ^ is_bottom; answers[i] -= was_added ? 1 : -1; } } for (int i = 0; i < N; ++i) { cout << answers[i] << "\n"; } } |