├── .gitignore ├── README.md └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | /Originals 2 | .vscode 3 | .idea 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CP-Template 2 | 3 | C++ Templates for Competitive Programming 4 | 5 | Main file is `main.cpp`. 6 | 7 | See also: [zscoder's template](https://github.com/zscoder/CompetitiveProgramming/blob/master/Data%20Structures%20Class%20Template.cpp) 8 | 9 | List of things (in order): 10 | 11 | 1. Segment/Fenwick tree 12 | - Segment tree (all ranges are closed i.e. l,r inclusive) 13 | - Point update 14 | - Range update (Lazy propagation) 15 | - Short iterative ver. [[source](https://codeforces.com/blog/entry/18051)] 16 | - 2D segment tree 17 | - Persistent segment tree 18 | - Segment tree beats (e.g. range min/max update) [[source](https://tjkendev.github.io/procon-library/cpp/range_query/segment_tree_beats_2.html)] 19 | - Fenwick tree: point and range update 20 | 21 | 2. String algorithms 22 | - Prefix function (KMP) 23 | - Z-algorithm 24 | - Trie 25 | - Aho-Corasick 26 | - Suffix Array 27 | 28 | 3. Graph theory 29 | - Algorithms/DS 30 | - DSU (Disjoint-set union) 31 | - Kruskal 32 | - Dijkstra 33 | - Floyd-Warshall 34 | - SPFA (Shortest path faster algorithm)/Bellman-Ford 35 | - Dinic Flow O(V^2E) 36 | - Edmonds-Karp: Min Cost Max Flow 37 | - Hopcroft-Karp matching (max-cardinality bipartite matching/MCBM) 38 | - Strongly connected component (SCC): Tarjan's algorithm 39 | - Common Techniques 40 | - Euler tour compression 41 | - Heavy-light decomposition (HLD) 42 | - Lowest Common Ancestor (LCA) 43 | - Euler tour method: O(log n) query 44 | - Depth method: O(log n) query 45 | - Sparse table: O(1) query but long 46 | - Virtual tree 47 | - Centroid decomposition: solving for all paths crossing current centroid 48 | 49 | 4. Data structures 50 | - Sparse table 51 | - Convex hull trick (CHT) 52 | - Dynamic version (LineContainer): O(log n) query 53 | - Offline version: O(1) query 54 | - Li Chao Tree \[untested\] 55 | 56 | 5. Maths 57 | - Combinatorics 58 | - Modular operations: Add, mult, inverse, binary exponentiation, binomial coefficients, factorials 59 | - getpf(): O(sqrt(n)) prime factorization 60 | - Matrix exponentiation 61 | - Number theory 62 | 63 | 6. Square root decomposition/Mo's algorithm 64 | 65 | 7. Convolutions 66 | - Fast Fourier Transform (FFT) 67 | - Number Theoretic Transform (NTT) 68 | - FFT Mod 69 | - Fast Walsh-Hadamard Transform (FWHT): AND, OR, XOR convolution 70 | 71 | 8. Geometry \[untested\] 72 | 73 | 9. Miscellaneous 74 | - Randomizer (Mersenne prime twister, mt19937) 75 | - unordered_map/hash map custom hash (http://xorshift.di.unimi.it/splitmix64.c) 76 | - Binary converter: print numbers in binary 77 | - Grid movement: 4/8 directions 78 | - Nearest pair of points 79 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | //#include 4 | using namespace std; 5 | //using namespace __gnu_pbds; 6 | 7 | #pragma GCC optimize("O3,unroll-loops") 8 | #pragma GCC target("avx2") 9 | #define setp(x) cout< ii; 22 | typedef vector vi; 23 | typedef vector vii; 24 | //template 25 | //using pbds = tree, rb_tree_tag, tree_order_statistics_node_update>; 26 | void SD(int t=0){ cout<<"PASSED "< v, lazy; 43 | 44 | void update(int s, int e, ll val, int k, int l, int r) { 45 | if (r < s || e < l) return; 46 | if (s <= l && r <= e) { 47 | lazy[k] += val; 48 | apply(k, l, r, val); 49 | return; 50 | } 51 | push(k, l, r); 52 | update(s, e, val, k * 2, l, (l + r) >> 1); 53 | update(s, e, val, k * 2 + 1, ((l + r) >> 1) + 1, r); 54 | v[k] = merge(v[k * 2], v[k * 2 + 1]); 55 | } 56 | 57 | ll query(int s, int e, int k, int l, int r) { 58 | if (r < s || e < l) return kDum; 59 | if (s <= l && r <= e) return v[k]; 60 | push(k, l, r); 61 | ll lc = query(s, e, k * 2, l, (l + r) >> 1); 62 | ll rc = query(s, e, k * 2 + 1, ((l + r) >> 1) + 1, r); 63 | return merge(lc, rc); 64 | } 65 | 66 | public: 67 | LazySegmentTree() : v{}, lazy{} {} 68 | LazySegmentTree(int n) { 69 | for (size_ = 1; size_ < n;) size_ <<= 1; 70 | v.resize(size_ * 4, kDef); 71 | lazy.resize(size_ * 4, kDefLazy); 72 | } 73 | inline void push(int k, int l, int r) { 74 | if (lazy[k] == kDefLazy) return; 75 | if (l != r) { 76 | int mid = (l + r) >> 1; 77 | lazy[k * 2] += lazy[k]; 78 | apply(k * 2, l, mid, lazy[k]); 79 | lazy[k * 2 + 1] += lazy[k]; 80 | apply(k * 2 + 1, mid + 1, r, lazy[k]); 81 | } 82 | lazy[k] = kDefLazy; 83 | } 84 | void apply(int k, int l, int r, ll val) { 85 | v[k] += (r - l + 1) * val; // remember to consider the range! 86 | } 87 | inline ll merge(ll x, ll y) { 88 | return x + y; 89 | } 90 | inline void update(int l, int r, ll val) { 91 | update(l, r, val, 1, 0, size_ - 1); 92 | } 93 | inline ll query(int l, int r) { 94 | return query(l, r, 1, 0, size_ - 1); 95 | } 96 | }; 97 | // Lazy recursive ST end 98 | 99 | // Lazy recursive ST with struct start 100 | struct Node{ 101 | ll sum,mn,mx; 102 | Node(){sum=mn=mx=0;} 103 | Node(ll _s,ll _mn,ll _mx){ sum=_s; mn=_mn; mx=_mx; } 104 | }; 105 | 106 | class LazySegmentTreeNode{ 107 | private: 108 | int size_; 109 | vector v,lazy; 110 | 111 | void update(int s, int e, const Node &val, int k, int l, int r){ 112 | push(k, l, r); 113 | if(r < s || e < l) return; 114 | if(s <= l && r <= e){ 115 | lazy[k] = val; 116 | push(k, l, r); 117 | } 118 | else{ 119 | update(s, e, val, k*2, l, (l+r)>>1); 120 | update(s, e, val, k*2+1, ((l+r)>>1)+1, r); 121 | v[k] = merge(v[k*2], v[k*2+1]); 122 | } 123 | } 124 | 125 | Node query(int s, int e, int k, int l, int r){ 126 | push(k, l, r); 127 | if(r < s || e < l) return Node(0,INF,-1); //dummy value 128 | if(s <= l && r <= e) return v[k]; 129 | Node lc = query(s, e, k*2, l, (l+r)>>1); 130 | Node rc = query(s, e, k*2+1, ((l+r)>>1)+1, r); 131 | return merge(lc, rc); 132 | } 133 | 134 | public: 135 | LazySegmentTreeNode(): v(vector()), lazy(vector()) {} 136 | LazySegmentTreeNode(int n){ 137 | for(size_=1;size_ v; 176 | void update(int p, ll val, int k, int l, int r) 177 | { 178 | if(p < l || r < p) return; 179 | if(l == r){ 180 | v[k]=val; //modification 181 | return; 182 | } 183 | int mid = (l+r)>>1; 184 | update(p, val, k*2, l, mid); 185 | update(p, val, k*2+1, mid+1, r); 186 | v[k] = merge(v[k*2], v[k*2+1]); 187 | } 188 | ll query(int s, int e, int k, int l, int r) 189 | { 190 | if(e < l || r < s) return 0; //dummy value 191 | if(s <= l && r <= e) return v[k]; 192 | int mid = (l+r)>>1; 193 | ll lc = query(s, e, k*2, l, mid); 194 | ll rc = query(s, e, k*2+1, mid+1, r); 195 | return merge(lc, rc); 196 | } 197 | 198 | public: 199 | PointSegmentTree(): v(vector()) {} 200 | PointSegmentTree(int n){ 201 | for(size_=1;size_ v; 228 | 229 | void update(int p, const Node &val, int k, int l, int r) 230 | { 231 | if(p < l || r < p) return; 232 | if(l == r){ 233 | v[k].sum += val.sum; //modifications 234 | v[k].mx = val.mx; 235 | v[k].mn = val.mn; 236 | return; 237 | } 238 | int mid = (l+r)>>1; 239 | update(p, val, k*2, l, mid); 240 | update(p, val, k*2+1, mid+1, r); 241 | v[k] = merge(v[k*2], v[k*2+1]); 242 | } 243 | 244 | Node query(int s, int e, int k, int l, int r) 245 | { 246 | if(e < l || r < s) return Node(0,INF,-1); //dummy value 247 | if(s <= l && r <= e) return v[k]; 248 | int mid = (l+r)>>1; 249 | Node lc = query(s, e, k*2, l, mid); 250 | Node rc = query(s, e, k*2+1, mid+1, r); 251 | return merge(lc, rc); 252 | } 253 | 254 | public: 255 | PointSegmentTreeNode(): v(vector()) {} 256 | PointSegmentTreeNode(int n){ 257 | for(size_=1;size_ t; 280 | 281 | IterSegmentTree: t(vector()) {} 282 | IterSegmentTree(int _n){ 283 | t.resize(_n*2); 284 | } 285 | 286 | void build(){ 287 | for(int i=n-1; i>0; i--) t[i]=t[i<<1]+t[i<<1|1]; 288 | } 289 | 290 | void update(int p, int val){ 291 | for(t[p+=n]=value; p>1; p>>=1) t[p>>1]=t[p]+t[p^1]; 292 | } 293 | 294 | int query(int l, int r){ 295 | int res=0; 296 | for(l+=n,r+=n; l>=1,r>>=1){ 297 | if(l&1) res+=t[l++]; 298 | if(r&1) res+=t[--r]; 299 | } 300 | return res; 301 | } 302 | }; 303 | 304 | forn(i,0,n) cin>>t[n+i]; 305 | // Point iterative ST end 306 | 307 | // 2D Segment Tree start 308 | class SegmentTree2D { 309 | private: 310 | int size_n, size_m; 311 | vector> v; 312 | void build(const vector> &a, int k, int l, int r) 313 | { 314 | if(r >= size_n) return; 315 | if(l != r){ 316 | int mid = (l+r)>>1; 317 | build(a, k*2, l, mid); 318 | build(a, k*2+1, mid+1, r); 319 | } 320 | build2(a, k, l, r, 1, 0, size_m-1); 321 | } 322 | void build2(const vector> &a, int k, int l, int r, int k2, int l2, int r2) 323 | { 324 | if(l2 == r2){ 325 | if(l >= a.size() || l2 >= a[0].size()) return; 326 | if(l == r) 327 | v[k][k2] = a[l][l2]; 328 | else 329 | v[k][k2] = merge(v[k*2][k2], v[k*2+1][k2]); 330 | return; 331 | } 332 | int mid2 = (l2+r2)>>1; 333 | build2(a, k, l, r, k2*2, l2, mid2); 334 | build2(a, k, l, r, k2*2+1, mid2+1, r2); 335 | v[k][k2] = merge(v[k][k2*2], v[k][k2*2+1]); 336 | } 337 | void update(int p1, int p2, ll val, int k, int l, int r) 338 | { 339 | if(p1 < l || r < p1) return; 340 | if(l != r){ 341 | int mid = (l+r)>>1; 342 | update(p1, p2, val, k*2, l, mid); 343 | update(p1, p2, val, k*2+1, mid+1, r); 344 | } 345 | update2(p1, p2, val, k, l, r, 1, 0, size_m-1); 346 | } 347 | void update2(int p1, int p2, ll val, int k, int l, int r, int k2, int l2, int r2) 348 | { 349 | if(p2 < l2 || r2 < p2) return; 350 | if(l2 == r2){ 351 | if(l == r) 352 | v[k][k2] ^= val; //modification 353 | else 354 | v[k][k2] = merge(v[k*2][k2], v[k*2+1][k2]); 355 | return; 356 | } 357 | int mid2 = (l2+r2)>>1; 358 | update2(p1, p2, val, k, l, r, k2*2, l2, mid2); 359 | update2(p1, p2, val, k, l, r, k2*2+1, mid2+1, r2); 360 | v[k][k2] = merge(v[k][k2*2], v[k][k2*2+1]); 361 | } 362 | ll query(int s, int e, int s2, int e2, int k, int l, int r) 363 | { 364 | if(e < l || r < s) return 0; //dummy value 365 | if(s <= l && r <= e) return query2(s2, e2, k, 1, 0, size_m-1); 366 | int mid = (l+r)>>1; 367 | ll lc = query(s, e, s2, e2, k*2, l, mid); 368 | ll rc = query(s, e, s2, e2, k*2+1, mid+1, r); 369 | return merge(lc, rc); 370 | } 371 | ll query2(int s2, int e2, int k, int k2, int l2, int r2) 372 | { 373 | if(e2 < l2 || r2 < s2) return 0; //dummy value 374 | if(s2 <= l2 && r2 <= e2) return v[k][k2]; 375 | int mid2 = (l2+r2)>>1; 376 | ll lc = query2(s2, e2, k, k2*2, l2, mid2); 377 | ll rc = query2(s2, e2, k, k2*2+1, mid2+1, r2); 378 | return merge(lc, rc); 379 | } 380 | 381 | public: 382 | SegmentTree2D(): v(vector>()) {} 383 | SegmentTree2D(int n, int m){ 384 | for(size_n=1;size_n(4*size_m)); 387 | } 388 | inline ll merge(ll x, ll y){ 389 | return x+y; 390 | } 391 | inline void build(const vector> &a){ 392 | build(a, 1, 0, size_n-1); 393 | } 394 | inline void update(int p1, int p2, ll val){ 395 | update(p1, p2, val, 1, 0, size_n-1); 396 | } 397 | inline ll query(int l, int r, int l2, int r2){ 398 | return query(l, r, l2, r2, 1, 0, size_n-1); 399 | } 400 | }; 401 | // 2D Segment Tree end 402 | 403 | // Persistent Segment Tree start 404 | template 405 | struct Node 406 | { 407 | T val; 408 | int l = 0, r = 0; 409 | }; 410 | 411 | template 412 | class PersistSegmentTree 413 | { 414 | private: 415 | int update(int pos, T val, int k, int l, int r) { 416 | int k1 = createNode(); 417 | v[k1] = v[k]; 418 | if (l == r) { 419 | v[k1].val += val; // change this update 420 | return k1; 421 | } 422 | int mid = (l + r) >> 1; 423 | int cl = v[k1].l; 424 | int cr = v[k1].r; 425 | if (pos <= mid) { 426 | v[k1].l = update(pos, val, cl, l, mid); 427 | cl = v[k1].l; 428 | } else { 429 | v[k1].r = update(pos, val, cr, mid + 1, r); 430 | cr = v[k1].r; 431 | } 432 | v[k1].val = merge(v[cl].val, v[cr].val); 433 | return k1; 434 | } 435 | T query(int s, int e, int k, int l, int r) { 436 | if (r < s || e < l) return invVal; 437 | if (s <= l && r <= e) return v[k].val; 438 | int mid = (l + r) >> 1; 439 | T cl = query(s, e, v[k].l, l, mid); 440 | T cr = query(s, e, v[k].r, mid + 1, r); 441 | return merge(cl, cr); 442 | } 443 | 444 | public: 445 | int siz; 446 | vector> v; 447 | PersistSegmentTree(int n) : siz(1), v(1, {invVal}) { 448 | while (siz < n) siz <<= 1; 449 | } 450 | int createNode() { 451 | v.push_back({T{}, 0, 0}); 452 | return sz(v) - 1; 453 | } 454 | T merge(T x, T y) { 455 | return x + y; 456 | } 457 | int update(int pos, T val, int node) { 458 | return update(pos, val, node, 0, siz - 1); 459 | } 460 | T query(int l, int r, int node) { 461 | return query(l, r, node, 0, siz - 1); 462 | } 463 | }; 464 | // Persistent Segment Tree end 465 | 466 | // Segment Tree Beats start (by yaketake08/tjake) 467 | // https://tjkendev.github.io/procon-library/cpp/range_query/segment_tree_beats_2.html 468 | // All intervals are [L,R) 469 | 470 | #define N MAXN 471 | class SegmentTree { 472 | const ll inf = 1e18; 473 | int n, n0; 474 | ll max_v[4*N], smax_v[4*N], max_c[4*N]; 475 | ll min_v[4*N], smin_v[4*N], min_c[4*N]; 476 | ll sum[4*N]; 477 | ll len[4*N], ladd[4*N], lval[4*N]; 478 | 479 | void update_node_max(int k, ll x) { 480 | sum[k] += (x - max_v[k]) * max_c[k]; 481 | 482 | if(max_v[k] == min_v[k]) { 483 | max_v[k] = min_v[k] = x; 484 | } else if(max_v[k] == smin_v[k]) { 485 | max_v[k] = smin_v[k] = x; 486 | } else { 487 | max_v[k] = x; 488 | } 489 | 490 | if(lval[k] != inf && x < lval[k]) { 491 | lval[k] = x; 492 | } 493 | } 494 | void update_node_min(int k, ll x) { 495 | sum[k] += (x - min_v[k]) * min_c[k]; 496 | 497 | if(max_v[k] == min_v[k]) { 498 | max_v[k] = min_v[k] = x; 499 | } else if(smax_v[k] == min_v[k]) { 500 | min_v[k] = smax_v[k] = x; 501 | } else { 502 | min_v[k] = x; 503 | } 504 | 505 | if(lval[k] != inf && lval[k] < x) { 506 | lval[k] = x; 507 | } 508 | } 509 | 510 | void push(int k) { 511 | if(n0-1 <= k) return; 512 | 513 | if(lval[k] != inf) { 514 | updateall(2*k+1, lval[k]); 515 | updateall(2*k+2, lval[k]); 516 | lval[k] = inf; 517 | return; 518 | } 519 | 520 | if(ladd[k] != 0) { 521 | addall(2*k+1, ladd[k]); 522 | addall(2*k+2, ladd[k]); 523 | ladd[k] = 0; 524 | } 525 | 526 | if(max_v[k] < max_v[2*k+1]) { 527 | update_node_max(2*k+1, max_v[k]); 528 | } 529 | if(min_v[2*k+1] < min_v[k]) { 530 | update_node_min(2*k+1, min_v[k]); 531 | } 532 | 533 | if(max_v[k] < max_v[2*k+2]) { 534 | update_node_max(2*k+2, max_v[k]); 535 | } 536 | if(min_v[2*k+2] < min_v[k]) { 537 | update_node_min(2*k+2, min_v[k]); 538 | } 539 | } 540 | 541 | void update(int k) { 542 | sum[k] = sum[2*k+1] + sum[2*k+2]; 543 | 544 | if(max_v[2*k+1] < max_v[2*k+2]) { 545 | max_v[k] = max_v[2*k+2]; 546 | max_c[k] = max_c[2*k+2]; 547 | smax_v[k] = max(max_v[2*k+1], smax_v[2*k+2]); 548 | } else if(max_v[2*k+1] > max_v[2*k+2]) { 549 | max_v[k] = max_v[2*k+1]; 550 | max_c[k] = max_c[2*k+1]; 551 | smax_v[k] = max(smax_v[2*k+1], max_v[2*k+2]); 552 | } else { 553 | max_v[k] = max_v[2*k+1]; 554 | max_c[k] = max_c[2*k+1] + max_c[2*k+2]; 555 | smax_v[k] = max(smax_v[2*k+1], smax_v[2*k+2]); 556 | } 557 | 558 | if(min_v[2*k+1] < min_v[2*k+2]) { 559 | min_v[k] = min_v[2*k+1]; 560 | min_c[k] = min_c[2*k+1]; 561 | smin_v[k] = min(smin_v[2*k+1], min_v[2*k+2]); 562 | } else if(min_v[2*k+1] > min_v[2*k+2]) { 563 | min_v[k] = min_v[2*k+2]; 564 | min_c[k] = min_c[2*k+2]; 565 | smin_v[k] = min(min_v[2*k+1], smin_v[2*k+2]); 566 | } else { 567 | min_v[k] = min_v[2*k+1]; 568 | min_c[k] = min_c[2*k+1] + min_c[2*k+2]; 569 | smin_v[k] = min(smin_v[2*k+1], smin_v[2*k+2]); 570 | } 571 | } 572 | 573 | void _update_min(ll x, int a, int b, int k, int l, int r) { 574 | if(b <= l || r <= a || max_v[k] <= x) { 575 | return; 576 | } 577 | if(a <= l && r <= b && smax_v[k] < x) { 578 | update_node_max(k, x); 579 | return; 580 | } 581 | 582 | push(k); 583 | _update_min(x, a, b, 2*k+1, l, (l+r)/2); 584 | _update_min(x, a, b, 2*k+2, (l+r)/2, r); 585 | update(k); 586 | } 587 | 588 | void _update_max(ll x, int a, int b, int k, int l, int r) { 589 | if(b <= l || r <= a || x <= min_v[k]) { 590 | return; 591 | } 592 | if(a <= l && r <= b && x < smin_v[k]) { 593 | update_node_min(k, x); 594 | return; 595 | } 596 | 597 | push(k); 598 | _update_max(x, a, b, 2*k+1, l, (l+r)/2); 599 | _update_max(x, a, b, 2*k+2, (l+r)/2, r); 600 | update(k); 601 | } 602 | 603 | void addall(int k, ll x) { 604 | max_v[k] += x; 605 | if(smax_v[k] != -inf) smax_v[k] += x; 606 | min_v[k] += x; 607 | if(smin_v[k] != inf) smin_v[k] += x; 608 | 609 | sum[k] += len[k] * x; 610 | if(lval[k] != inf) { 611 | lval[k] += x; 612 | } else { 613 | ladd[k] += x; 614 | } 615 | } 616 | 617 | void updateall(int k, ll x) { 618 | max_v[k] = x; smax_v[k] = -inf; 619 | min_v[k] = x; smin_v[k] = inf; 620 | max_c[k] = min_c[k] = len[k]; 621 | 622 | sum[k] = x * len[k]; 623 | lval[k] = x; ladd[k] = 0; 624 | } 625 | 626 | void _add_val(ll x, int a, int b, int k, int l, int r) { 627 | if(b <= l || r <= a) { 628 | return; 629 | } 630 | if(a <= l && r <= b) { 631 | addall(k, x); 632 | return; 633 | } 634 | 635 | push(k); 636 | _add_val(x, a, b, 2*k+1, l, (l+r)/2); 637 | _add_val(x, a, b, 2*k+2, (l+r)/2, r); 638 | update(k); 639 | } 640 | 641 | void _update_val(ll x, int a, int b, int k, int l, int r) { 642 | if(b <= l || r <= a) { 643 | return; 644 | } 645 | if(a <= l && r <= b) { 646 | updateall(k, x); 647 | return; 648 | } 649 | 650 | push(k); 651 | _update_val(x, a, b, 2*k+1, l, (l+r)/2); 652 | _update_val(x, a, b, 2*k+2, (l+r)/2, r); 653 | update(k); 654 | } 655 | 656 | ll _query_max(int a, int b, int k, int l, int r) { 657 | if(b <= l || r <= a) { 658 | return -inf; 659 | } 660 | if(a <= l && r <= b) { 661 | return max_v[k]; 662 | } 663 | push(k); 664 | ll lv = _query_max(a, b, 2*k+1, l, (l+r)/2); 665 | ll rv = _query_max(a, b, 2*k+2, (l+r)/2, r); 666 | return max(lv, rv); 667 | } 668 | 669 | ll _query_min(int a, int b, int k, int l, int r) { 670 | if(b <= l || r <= a) { 671 | return inf; 672 | } 673 | if(a <= l && r <= b) { 674 | return min_v[k]; 675 | } 676 | push(k); 677 | ll lv = _query_min(a, b, 2*k+1, l, (l+r)/2); 678 | ll rv = _query_min(a, b, 2*k+2, (l+r)/2, r); 679 | return min(lv, rv); 680 | } 681 | 682 | ll _query_sum(int a, int b, int k, int l, int r) { 683 | if(b <= l || r <= a) { 684 | return 0; 685 | } 686 | if(a <= l && r <= b) { 687 | return sum[k]; 688 | } 689 | push(k); 690 | ll lv = _query_sum(a, b, 2*k+1, l, (l+r)/2); 691 | ll rv = _query_sum(a, b, 2*k+2, (l+r)/2, r); 692 | return lv + rv; 693 | } 694 | 695 | public: 696 | SegmentTree(int n) { 697 | SegmentTree(n, nullptr); 698 | } 699 | 700 | SegmentTree(int n, ll *a) : n(n) { 701 | n0 = 1; 702 | while(n0 < n) n0 <<= 1; 703 | 704 | for(int i=0; i<2*n0; ++i) ladd[i] = 0, lval[i] = inf; 705 | len[0] = n0; 706 | for(int i=0; i> 1); 707 | 708 | for(int i=0; i=0; i--) { 721 | update(i); 722 | } 723 | } 724 | 725 | // range minimize query 726 | void update_min(int a, int b, ll x) { 727 | _update_min(x, a, b, 0, 0, n0); 728 | } 729 | 730 | // range maximize query 731 | void update_max(int a, int b, ll x) { 732 | _update_max(x, a, b, 0, 0, n0); 733 | } 734 | 735 | // range add query 736 | void add_val(int a, int b, ll x) { 737 | _add_val(x, a, b, 0, 0, n0); 738 | } 739 | 740 | // range update query 741 | void update_val(int a, int b, ll x) { 742 | _update_val(x, a, b, 0, 0, n0); 743 | } 744 | 745 | // range minimum query 746 | ll query_max(int a, int b) { 747 | return _query_max(a, b, 0, 0, n0); 748 | } 749 | 750 | // range maximum query 751 | ll query_min(int a, int b) { 752 | return _query_min(a, b, 0, 0, n0); 753 | } 754 | 755 | // range sum query 756 | ll query_sum(int a, int b) { 757 | return _query_sum(a, b, 0, 0, n0); 758 | } 759 | }; 760 | // Segment Tree Beats end 761 | 762 | // Fenwick Tree (FenwickPoint) start 763 | struct FenwickPoint 764 | { 765 | vector fw; 766 | int siz; 767 | FenwickPoint(): fw(vector()), siz(0) {} 768 | FenwickPoint(int N){ fw.assign(N+1,0); siz = N+1; } 769 | void reset(int N){ fw.assign(N+1,0); siz = N+1; } 770 | void add(int p, ll val) 771 | { 772 | for(p++; p fw,fw2; 795 | int siz; 796 | FenwickRange(): fw(vector()), fw2(vector()), siz(0) {} 797 | FenwickRange(int N) 798 | { 799 | fw.assign(N+1,0); 800 | fw2.assign(N+1,0); 801 | siz = N+1; 802 | } 803 | void reset(int N) 804 | { 805 | fw.assign(N+1,0); 806 | fw2.assign(N+1,0); 807 | siz = N+1; 808 | } 809 | void add(int l, int r, ll val) //[l,r] + val 810 | { 811 | l++; r++; 812 | for(int tl=l; tl prefix_function(string &s){ 837 | int n=(int)s.length(); 838 | vector pi(n); 839 | pi[0]=0; 840 | for(int i=1;i0 && s[i]!=s[j]) j=pi[j-1]; 843 | if(s[i]==s[j]) j++; 844 | pi[i]=j; 845 | } 846 | return pi; 847 | } 848 | // Prefix function end 849 | 850 | // Z-algorithm/Z-function start [Z algorithm/Z function] 851 | vector z_function(string &s){ 852 | int n=(int)s.length(); 853 | vector z(n); 854 | for(int i=1,l=0,r=0; ir) l=i, r=i+z[i]-1; 858 | } 859 | return z; 860 | } 861 | // Z-algorithm end 862 | 863 | // Trie start 864 | struct TrieNode { 865 | int next[26]; 866 | bool leaf = false; 867 | TrieNode(){fill(begin(next), end(next), -1);} 868 | }; 869 | struct Trie { 870 | int siz; 871 | vector tr; 872 | Trie(): siz(0), tr(vector(1)) {} 873 | TrieNode& operator[](int u){ return tr[u]; } 874 | int size(){ return siz; } 875 | void addstring(const string &s) { 876 | int v = 0; 877 | for (char ch : s) { 878 | int c = ch - 'a'; 879 | if (tr[v].next[c] == -1) { 880 | tr[v].next[c] = tr.size(); 881 | tr.emplace_back(); 882 | } 883 | v = tr[v].next[c]; 884 | } 885 | if(!tr[v].leaf) siz++; 886 | tr[v].leaf = true; 887 | } 888 | template 889 | void dfs(int u, F f) { 890 | forn(i,0,26) { 891 | if(tr[u].next[i] != -1) { 892 | dfs(tr[u].next[i]); 893 | } 894 | } 895 | } 896 | }; 897 | // Trie end 898 | 899 | // Aho-Corasick start [Aho Corasick] 900 | // Reference: https://codeforces.com/blog/entry/14854 901 | struct AhoCorasick { 902 | enum {alpha = 26, first = 'A'}; // change this! 903 | struct Node { 904 | int fail = 0; 905 | int next[alpha]; 906 | bool leaf = 0; 907 | Node() { memset(next, 0, sizeof(next)); } 908 | }; 909 | vector N; 910 | void insert(const string& s) { 911 | assert(!s.empty()); 912 | int u = 0; 913 | for (char c : s) { 914 | int nxt = N[u].next[c - first]; 915 | if (!nxt) { 916 | nxt = N[u].next[c - first] = sz(N); 917 | N.emplace_back(); 918 | } 919 | u = nxt; 920 | } 921 | N[u].leaf = 1; 922 | } 923 | AhoCorasick(const vector& v) : N(1) { 924 | forn(i,0,sz(v)) insert(v[i]); 925 | 926 | queue q; 927 | forn(i,0,alpha) if (N[0].next[i]) q.push(N[0].next[i]); 928 | for (; !q.empty(); q.pop()) { 929 | int u = q.front(), prev = N[u].fail; 930 | forn(i,0,alpha) { 931 | int& nxt = N[u].next[i]; 932 | int y = N[prev].next[i]; 933 | if (!nxt) nxt = y; 934 | else { 935 | N[nxt].fail = y; 936 | q.push(nxt); 937 | } 938 | } 939 | } 940 | } 941 | }; 942 | // Aho-Corasick end 943 | 944 | // Suffix array start 945 | const int MAX_N = 500005; 946 | const int MAX_C = 305; // Maximum initial rank + 1 947 | 948 | int RA[MAX_N], tempRA[MAX_N]; // rank array and temporary rank array 949 | int SA[MAX_N], tempSA[MAX_N]; // suffix array and temporary suffix array. SA[i] = starting position of i-th suffix in lexicographical order 950 | int RSA[MAX_N]; // RSA[i]: the i-th suffix is in the RSA[i]-ith lexicographically smallest suffix 951 | int SRT[MAX_N]; // for counting/radix sort 952 | int Phi[MAX_N], LCP[MAX_N], PLCP[MAX_N]; // for LCP counting 953 | 954 | class SuffixArray { 955 | public: 956 | vector T; // the input string, up to MAX_N characters 957 | int n; // the length of input string 958 | void countingSort(int k) { // O(n) 959 | int i, sum, maxi = max(MAX_C, n); // up to 255 ASCII chars or length of n 960 | memset(SRT, 0, sizeof SRT); // clear frequency table 961 | for (i = 0; i < n; i++) // count the frequency of each integer rank 962 | SRT[i + k < n ? RA[i + k] : 0]++; 963 | for (i = sum = 0; i < maxi; i++) { 964 | int t = SRT[i]; SRT[i] = sum; sum += t; 965 | } 966 | for (i = 0; i < n; i++) // shuffle the suffix array if necessary 967 | tempSA[SRT[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i]; 968 | for (i = 0; i < n; i++) // update the suffix array SA 969 | SA[i] = tempSA[i]; 970 | } 971 | void constructSA(const vector &s) { // this version can go up to 100000 characters 972 | int i, k, r; 973 | T = s; 974 | n = s.size(); 975 | for (i = 0; i < n; i++) RA[i] = T[i]; // initial rankings 976 | for (i = 0; i < n; i++) SA[i] = i; // initial SA: {0, 1, 2, ..., n-1} 977 | for (k = 1; k < n; k <<= 1) { // repeat sorting process log n times 978 | countingSort(k); // actually radix sort: sort based on the second item 979 | countingSort(0); // then (stable) sort based on the first item 980 | tempRA[SA[0]] = r = 0; // re-ranking; start from rank r = 0 981 | for (i = 1; i < n; i++) // compare adjacent suffixes 982 | tempRA[SA[i]] = // if same pair => same rank r; otherwise, increase r 983 | (RA[SA[i]] == RA[SA[i - 1]] && RA[SA[i] + k] == RA[SA[i - 1] + k]) ? r : ++r; 984 | for (i = 0; i < n; i++) // update the rank array RA 985 | RA[i] = tempRA[i]; 986 | if (RA[SA[n - 1]] == n - 1) break; // nice optimization trick 987 | } 988 | } 989 | void computeLCP() { 990 | int i, L; 991 | Phi[SA[0]] = -1; // default value 992 | for (i = 1; i < n; i++) // compute Phi in O(n) 993 | Phi[SA[i]] = SA[i - 1]; // remember which suffix is behind this suffix 994 | for (i = L = 0; i < n; i++) { // compute Permuted LCP in O(n) 995 | if (Phi[i] == -1) { PLCP[i] = 0; continue; } // special case 996 | while (T[i + L] == T[Phi[i] + L]) L++; // L increased max n times 997 | PLCP[i] = L; 998 | L = max(L - 1, 0); // L decreased max n times 999 | } 1000 | for (i = 0; i < n; i++) // compute LCP in O(n) 1001 | LCP[i] = PLCP[SA[i]]; // put the permuted LCP to the correct position 1002 | } 1003 | void constructRSA() { 1004 | int i; 1005 | for (i = 0; i < n; i++) RSA[SA[i]] = i; 1006 | } 1007 | }; 1008 | // Suffix array end 1009 | 1010 | // DSU start 1011 | struct DSU { 1012 | struct Node{ int p, sz; }; 1013 | vector comp; int cc; 1014 | Node& operator[](int id){ return comp[rt(id)]; } 1015 | DSU(int n = 0){ comp.resize(n); 1016 | forn(i,0,n){ cc=n; comp[i]={i,1}; } 1017 | } 1018 | inline int rt(int u){ return (comp[u].p==u) ? u : comp[u].p=rt(comp[u].p); } 1019 | inline bool sameset(int u, int v){ return rt(u)==rt(v); } 1020 | void merge(int u, int v){ 1021 | u = rt(u); v = rt(v); 1022 | if(u == v) return; 1023 | if(comp[u].sz < comp[v].sz) swap(u,v); 1024 | comp[v].p = u; 1025 | comp[u].sz += comp[v].sz; 1026 | cc--; 1027 | } 1028 | }; 1029 | // DSU end 1030 | 1031 | // Kruskal start 1032 | int n,m; 1033 | vector> edges; 1034 | vector> mst; 1035 | ll sumw=0; 1036 | 1037 | void kruskal() 1038 | { 1039 | DSU dsu(n); 1040 | sort(edges.begin(),edges.end()); 1041 | sumw=0; 1042 | 1043 | forn(i,0,edges.size()){ 1044 | int u=edges[i].S.F, v=edges[i].S.S; ll w=edges[i].F; 1045 | if(dsu.sameset(u,v)) continue; 1046 | mst.pb({{u,v},w}); 1047 | dsu.merge(u,v); 1048 | sumw+=w; 1049 | if(dsu.cc==1) break; 1050 | } 1051 | } 1052 | // Kruskal end 1053 | 1054 | // Dijkstra start 1055 | vector adj[MAXN]; // (node, distance) 1056 | ll dist[MAXN]; 1057 | // int parents[MAXN]; 1058 | 1059 | void dijkstra(int src) 1060 | { 1061 | priority_queue, greater> q; // (distance, node) 1062 | fill(dist, dist + n, INF); 1063 | // fill(parents, parents + n, -1); 1064 | dist[src] = 0; 1065 | q.push({dist[src], src}); 1066 | while (!q.empty()) 1067 | { 1068 | auto [cur_dist, u] = q.top(); 1069 | q.pop(); 1070 | if (cur_dist > dist[u]) continue; 1071 | for (auto [v, w] : adj[u]) 1072 | { 1073 | if (dist[v] <= cur_dist + w) continue; 1074 | dist[v] = cur_dist + w; 1075 | // parents[v] = u; 1076 | q.push({dist[v], v}); 1077 | } 1078 | } 1079 | } 1080 | // Dijkstra end 1081 | 1082 | // Floyd-Warshall start 1083 | ll dist[MAXN][MAXN]; 1084 | void floyd(){ 1085 | forn(i,0,n) forn(j,0,n) dist[i][j] = (adj[i][j]==0 ? INF : adj[i][j]); 1086 | forn(i,0,n) dist[i][i] = 0; 1087 | forn(k,0,n) forn(i,0,n) forn(j,0,n) 1088 | dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]); 1089 | } 1090 | // Floyd-Warshall end 1091 | 1092 | // SPFA/Bellman-Ford/Shortest Path Faster Algorithm start 1093 | // Returns one of the nodes in neg cycle if it exists (0-indexed), otherwise -1 1094 | int prt[MAXN]; 1095 | ll dist[MAXN]; 1096 | 1097 | int spfa(int src) 1098 | { 1099 | int cnt[n]{}; 1100 | bool inqueue[n]{}; 1101 | 1102 | forn(i,0,n) dist[i]=INF, prt[i]=-1; 1103 | dist[src]=0; 1104 | 1105 | queue q; 1106 | q.push(src); 1107 | inqueue[src]=true; 1108 | 1109 | while(!q.empty()) 1110 | { 1111 | int u=q.front(); q.pop(); 1112 | inqueue[u]=false; 1113 | for(ii tmp: adj[u]) 1114 | { 1115 | int v=tmp.F; ll w=tmp.S; 1116 | if(dist[v]>dist[u]+w) 1117 | { 1118 | dist[v]=dist[u]+w; 1119 | prt[v]=u; 1120 | if(!inqueue[v]) 1121 | { 1122 | q.push(v); 1123 | inqueue[v]=true; 1124 | cnt[v]++; 1125 | if(cnt[v]==n) 1126 | { 1127 | forn(i,0,n) v=prt[v]; 1128 | return v; 1129 | } 1130 | } 1131 | } 1132 | } 1133 | } 1134 | 1135 | return -1; 1136 | } 1137 | // SPFA/Bellman-Ford/Shortest Path Faster Algorithm end 1138 | 1139 | // Dinic Flow start: O(V^2E) 1140 | struct DinicFlow 1141 | { 1142 | struct Edge 1143 | { 1144 | int v,r; ll cap; 1145 | Edge(int v=0, ll cap=0, int r=0): v(v), r(r), cap(cap) {} 1146 | }; 1147 | int n_; 1148 | vector> adj; 1149 | vector level, ptr; 1150 | 1151 | DinicFlow(int n) 1152 | { 1153 | n_ = n; 1154 | adj.resize(n); 1155 | level.resize(n); 1156 | ptr.resize(n); 1157 | } 1158 | void addedge(int u, int v, ll c) 1159 | { 1160 | int u_sz = adj[u].size(), v_sz = adj[v].size(); 1161 | adj[u].emplace_back(v, c, v_sz); 1162 | adj[v].emplace_back(u, 0, u_sz); 1163 | } 1164 | void bfs(int s) 1165 | { 1166 | level.assign(n_, -1); 1167 | level[s] = 0; 1168 | queue q; 1169 | q.push(s); 1170 | while(!q.empty()) 1171 | { 1172 | int u = q.front(); q.pop(); 1173 | for(const Edge &e: adj[u]) 1174 | { 1175 | if(e.cap>0 && level[e.v]==-1) 1176 | { 1177 | level[e.v] = level[u]+1; 1178 | q.push(e.v); 1179 | } 1180 | } 1181 | } 1182 | } 1183 | ll dfs(int u, int t, ll f) 1184 | { 1185 | if(u == t) return f; 1186 | for(int &i=ptr[u];i0 && level[u]+1==level[e.v]) 1190 | { 1191 | ll newf = dfs(e.v, t, min(f,e.cap)); 1192 | if(!newf) continue; 1193 | e.cap -= newf; 1194 | adj[e.v][e.r].cap += newf; 1195 | return newf; 1196 | } 1197 | } 1198 | return 0; 1199 | } 1200 | ll flow(int s, int t) 1201 | { 1202 | ll sum = 0; 1203 | while(1) 1204 | { 1205 | bfs(s); 1206 | if(level[t]==-1) break; 1207 | ptr.assign(n_, 0); 1208 | while(1) 1209 | { 1210 | ll f = dfs(s, t, INF); 1211 | if(!f) break; 1212 | sum += f; 1213 | } 1214 | } 1215 | return sum; 1216 | } 1217 | }; 1218 | // Dinic Flow end 1219 | 1220 | // Min Cost Max Flow start 1221 | struct Edge { 1222 | int u, v; long long cap, cost; 1223 | Edge(int u, int v, long long cap, long long cost): u(u), v(v), cap(cap), cost(cost) {} 1224 | }; 1225 | struct MinCostFlow 1226 | { 1227 | int n, s, t; 1228 | long long flow, cost; 1229 | vector > graph; 1230 | vector e; 1231 | vector dist, potential; 1232 | vector parent; 1233 | bool negativeCost; 1234 | 1235 | MinCostFlow(int _n){ 1236 | // 0-based indexing 1237 | n = _n; 1238 | graph.assign(n, vector ()); 1239 | negativeCost = false; 1240 | } 1241 | 1242 | void addEdge(int u, int v, long long cap, long long cost, bool directed = true){ 1243 | if(cost < 0) 1244 | negativeCost = true; 1245 | 1246 | graph[u].push_back(e.size()); 1247 | e.push_back(Edge(u, v, cap, cost)); 1248 | 1249 | graph[v].push_back(e.size()); 1250 | e.push_back(Edge(v, u, 0, -cost)); 1251 | 1252 | if(!directed) 1253 | addEdge(v, u, cap, cost, true); 1254 | } 1255 | 1256 | pair getMinCostFlow(int _s, int _t){ 1257 | s = _s; t = _t; 1258 | flow = 0, cost = 0; 1259 | 1260 | potential.assign(n, 0); 1261 | if(negativeCost){ 1262 | // run Bellman-Ford to find starting potential 1263 | dist.assign(n, 1LL<<62); 1264 | for(int i = 0, relax = false; i < n && relax; i++, relax = false){ 1265 | for(int u = 0; u < n; u++){ 1266 | for(int k = 0; k < graph[u].size(); k++){ 1267 | int eIdx = graph[u][i]; 1268 | int v = e[eIdx].v; ll cap = e[eIdx].cap, w = e[eIdx].cost; 1269 | 1270 | if(dist[v] > dist[u] + w && cap > 0){ 1271 | dist[v] = dist[u] + w; 1272 | relax = true; 1273 | } } } } 1274 | 1275 | for(int i = 0; i < n; i++){ 1276 | if(dist[i] < (1LL<<62)){ 1277 | potential[i] = dist[i]; 1278 | } } } 1279 | 1280 | while(dijkstra()){ 1281 | flow += sendFlow(t, 1LL<<62); 1282 | } 1283 | 1284 | return make_pair(flow, cost); 1285 | } 1286 | 1287 | bool dijkstra(){ 1288 | parent.assign(n, -1); 1289 | dist.assign(n, 1LL<<62); 1290 | priority_queue, greater > pq; 1291 | 1292 | dist[s] = 0; 1293 | pq.push(ii(0, s)); 1294 | 1295 | 1296 | while(!pq.empty()){ 1297 | int u = pq.top().second; 1298 | long long d = pq.top().first; 1299 | pq.pop(); 1300 | 1301 | if(d != dist[u]) continue; 1302 | 1303 | for(int i = 0; i < graph[u].size(); i++){ 1304 | int eIdx = graph[u][i]; 1305 | int v = e[eIdx].v; ll cap = e[eIdx].cap; 1306 | ll w = e[eIdx].cost + potential[u] - potential[v]; 1307 | 1308 | if(dist[u] + w < dist[v] && cap > 0){ 1309 | dist[v] = dist[u] + w; 1310 | parent[v] = eIdx; 1311 | 1312 | pq.push(ii(dist[v], v)); 1313 | } } } 1314 | 1315 | // update potential 1316 | for(int i = 0; i < n; i++){ 1317 | if(dist[i] < (1LL<<62)) 1318 | potential[i] += dist[i]; 1319 | } 1320 | 1321 | return dist[t] != (1LL<<62); 1322 | } 1323 | 1324 | long long sendFlow(int v, long long curFlow){ 1325 | if(parent[v] == -1) 1326 | return curFlow; 1327 | int eIdx = parent[v]; 1328 | int u = e[eIdx].u; ll w = e[eIdx].cost; 1329 | 1330 | long long f = sendFlow(u, min(curFlow, e[eIdx].cap)); 1331 | 1332 | cost += f*w; 1333 | e[eIdx].cap -= f; 1334 | e[eIdx^1].cap += f; 1335 | 1336 | return f; 1337 | } 1338 | }; 1339 | // Min Cost Max Flow end 1340 | 1341 | // Min Cost Max Flow (long double) start 1342 | typedef pair dii; 1343 | struct Edge { 1344 | int u, v; ld cap, cost; 1345 | Edge(int u, int v, ld cap, ld cost): u(u), v(v), cap(cap), cost(cost) {} 1346 | }; 1347 | struct MinCostFlow 1348 | { 1349 | int n, s, t; 1350 | ld flow, cost; 1351 | vector > graph; 1352 | vector e; 1353 | vector dist, potential; 1354 | vector parent; 1355 | bool negativeCost; 1356 | 1357 | MinCostFlow(int _n){ 1358 | // 0-based indexing 1359 | n = _n; 1360 | graph.assign(n, vector ()); 1361 | negativeCost = false; 1362 | } 1363 | 1364 | void addEdge(int u, int v, ld cap, ld cost, bool directed = true){ 1365 | if(cost < 0) 1366 | negativeCost = true; 1367 | 1368 | graph[u].push_back(e.size()); 1369 | conv[{u,v}]=e.size(); 1370 | e.push_back(Edge(u, v, cap, cost)); 1371 | 1372 | graph[v].push_back(e.size()); 1373 | e.push_back(Edge(v, u, 0, -cost)); 1374 | 1375 | if(!directed) 1376 | addEdge(v, u, cap, cost, true); 1377 | } 1378 | 1379 | pair getMinCostFlow(int _s, int _t){ 1380 | s = _s; t = _t; 1381 | flow = 0, cost = 0; 1382 | 1383 | potential.assign(n, 0); 1384 | if(negativeCost){ 1385 | // run Bellman-Ford to find starting potential 1386 | dist.assign(n, 1e10); 1387 | for(int i = 0, relax = false; i < n && relax; i++, relax = false){ 1388 | for(int u = 0; u < n; u++){ 1389 | for(int k = 0; k < sz(graph[u]); k++){ 1390 | int eIdx = graph[u][i]; 1391 | int v = e[eIdx].v; ld cap = e[eIdx].cap, w = e[eIdx].cost; 1392 | 1393 | if(dist[v] > dist[u] + w && cap > 1e-9){ 1394 | dist[v] = dist[u] + w; 1395 | relax = true; 1396 | } } } } 1397 | 1398 | for(int i = 0; i < n; i++){ 1399 | if(dist[i] < (1e10)){ 1400 | potential[i] = dist[i]; 1401 | } } } 1402 | 1403 | while(dijkstra()){ 1404 | flow += sendFlow(t, 1e10); 1405 | } 1406 | 1407 | return make_pair(flow, cost); 1408 | } 1409 | 1410 | bool dijkstra(){ 1411 | parent.assign(n, -1); 1412 | dist.assign(n, 1e10); 1413 | priority_queue, greater > pq; 1414 | 1415 | dist[s] = 0; 1416 | pq.push(dii(0, s)); 1417 | 1418 | 1419 | while(!pq.empty()){ 1420 | int u = pq.top().second; 1421 | ld d = pq.top().first; 1422 | pq.pop(); 1423 | 1424 | if(d != dist[u]) continue; 1425 | 1426 | for(int i = 0; i < sz(graph[u]); i++){ 1427 | int eIdx = graph[u][i]; 1428 | int v = e[eIdx].v; ld cap = e[eIdx].cap; 1429 | ld w = e[eIdx].cost + potential[u] - potential[v]; 1430 | 1431 | if(dist[u] + w < dist[v] && cap > 1e-9){ 1432 | dist[v] = dist[u] + w; 1433 | parent[v] = eIdx; 1434 | 1435 | pq.push(dii(dist[v], v)); 1436 | } } } 1437 | 1438 | // update potential 1439 | for(int i = 0; i < n; i++){ 1440 | if(dist[i] < (1e10)) 1441 | potential[i] += dist[i]; 1442 | } 1443 | 1444 | return dist[t] != (1e10); 1445 | } 1446 | 1447 | ld sendFlow(int v, ld curFlow){ 1448 | if(parent[v] == -1) 1449 | return curFlow; 1450 | int eIdx = parent[v]; 1451 | int u = e[eIdx].u; ld w = e[eIdx].cost; 1452 | 1453 | ld f = sendFlow(u, min(curFlow, e[eIdx].cap)); 1454 | 1455 | cost += f*w; 1456 | e[eIdx].cap -= f; 1457 | e[eIdx^1].cap += f; 1458 | 1459 | return f; 1460 | } 1461 | }; 1462 | // Min Cost Max Flow (long double) end 1463 | 1464 | // Hopcroft-Karp matching (MCBM, max-cardinality bipartite matching) start 1465 | // Read n1,n2 -> init() -> addEdge() -> maxMatching() 1466 | const int MAXN1 = 50000; 1467 | const int MAXN2 = 50000; 1468 | const int MAXM = 150000; 1469 | 1470 | int n1, n2, edges, last[MAXN1], pre[MAXM], head[MAXM]; 1471 | int matching[MAXN2], dist[MAXN1], Q[MAXN1]; 1472 | bool used[MAXN1], vis[MAXN1]; 1473 | 1474 | class HopcroftKarp { 1475 | public: 1476 | void init(int _n1, int _n2) 1477 | { 1478 | n1 = _n1; 1479 | n2 = _n2; 1480 | edges = 0; 1481 | fill(last, last + n1, -1); 1482 | } 1483 | void addEdge(int u, int v) 1484 | { 1485 | head[edges] = v; 1486 | pre[edges] = last[u]; 1487 | last[u] = edges++; 1488 | } 1489 | void bfs() 1490 | { 1491 | fill(dist, dist + n1, -1); 1492 | int sizeQ = 0; 1493 | for (int u = 0; u < n1; ++u) { 1494 | if (!used[u]) { 1495 | Q[sizeQ++] = u; 1496 | dist[u] = 0; 1497 | } 1498 | } 1499 | for (int i = 0; i < sizeQ; i++) { 1500 | int u1 = Q[i]; 1501 | for (int e = last[u1]; e >= 0; e = pre[e]) { 1502 | int u2 = matching[head[e]]; 1503 | if (u2 >= 0 && dist[u2] < 0) { 1504 | dist[u2] = dist[u1] + 1; 1505 | Q[sizeQ++] = u2; 1506 | } 1507 | } 1508 | } 1509 | } 1510 | bool dfs(int u1) 1511 | { 1512 | vis[u1] = true; 1513 | for (int e = last[u1]; e >= 0; e = pre[e]) { 1514 | int v = head[e]; 1515 | int u2 = matching[v]; 1516 | if (u2 < 0 || ((!vis[u2] && dist[u2] == dist[u1] + 1) && dfs(u2))) { 1517 | matching[v] = u1; 1518 | used[u1] = true; 1519 | return true; 1520 | } 1521 | } 1522 | return false; 1523 | } 1524 | int maxMatching() 1525 | { 1526 | fill(used, used + n1, false); 1527 | fill(matching, matching + n2, -1); 1528 | for (int res = 0;;) { 1529 | bfs(); 1530 | fill(vis, vis + n1, false); 1531 | int f = 0; 1532 | for (int u = 0; u < n1; ++u) 1533 | if (!used[u] && dfs(u)) 1534 | ++f; 1535 | if (!f) 1536 | return res; 1537 | res += f; 1538 | } 1539 | } 1540 | }; 1541 | // Hopcroft-Karp matching end 1542 | 1543 | // SCC (Strongly connected components) start 1544 | // init(n) -> read input -> tarjan() -> sccidx[] 1545 | struct SCC 1546 | { 1547 | const int INF2 = int(1e9); 1548 | vector > vec; 1549 | int index; 1550 | vector idx; 1551 | vector lowlink; 1552 | vector onstack; 1553 | stack s; 1554 | vector sccidx; 1555 | vector> adj; // condensation graph 1556 | int scccnt; 1557 | vi topo; 1558 | 1559 | // lower sccidx means appear later 1560 | void init(int n) 1561 | { 1562 | idx.assign(n,-1); 1563 | index = 0; 1564 | onstack.assign(n,0); 1565 | lowlink.assign(n,INF2); 1566 | while(!s.empty()) s.pop(); 1567 | sccidx.assign(n,-1); 1568 | scccnt = 0; 1569 | vec.clear(); 1570 | topo.clear(); 1571 | vec.resize(n); 1572 | } 1573 | void addedge(int u, int v) //u -> v 1574 | { 1575 | vec[u].pb(v); 1576 | } 1577 | void connect(int u) 1578 | { 1579 | idx[u] = index; 1580 | lowlink[u] = index; 1581 | index++; 1582 | s.push(u); 1583 | onstack[u] = true; 1584 | for(int i = 0; i < sz(vec[u]); i++) 1585 | { 1586 | int v = vec[u][i]; 1587 | if(idx[v] == -1) 1588 | { 1589 | connect(v); 1590 | lowlink[u] = min(lowlink[u], lowlink[v]); 1591 | } 1592 | else if(onstack[v]) 1593 | { 1594 | lowlink[u] = min(lowlink[u], idx[v]); 1595 | } 1596 | } 1597 | if(lowlink[u] == idx[u]) 1598 | { 1599 | while(1) 1600 | { 1601 | int v = s.top(); 1602 | s.pop(); 1603 | onstack[v] = false; 1604 | sccidx[v] = scccnt; 1605 | if(v == u) break; 1606 | } 1607 | scccnt++; 1608 | } 1609 | } 1610 | void tarjan() 1611 | { 1612 | for(int i = 0; i < sz(vec); i++) 1613 | { 1614 | if(idx[i] == -1) 1615 | { 1616 | connect(i); 1617 | } 1618 | } 1619 | } 1620 | void condense() // run after tarjan 1621 | { 1622 | adj.resize(scccnt); 1623 | for(int u = 0; u < sz(vec); u++) 1624 | { 1625 | for(int v: vec[u]) 1626 | { 1627 | if(sccidx[u] != sccidx[v]) 1628 | { 1629 | adj[sccidx[u]].push_back(sccidx[v]); 1630 | } 1631 | } 1632 | } 1633 | for(int u = 0; u < scccnt; u++) 1634 | { 1635 | sort(adj[u].begin(), adj[u].end()); 1636 | adj[u].erase(unique(adj[u].begin(), adj[u].end()), adj[u].end()); 1637 | } 1638 | } 1639 | void toposort() // if graph is a DAG and i just want to toposort 1640 | { 1641 | tarjan(); 1642 | int n = sz(vec); 1643 | topo.resize(n); 1644 | vector tmp; 1645 | for(int i = 0; i < n; i++) 1646 | { 1647 | tmp.pb(ii(sccidx[i],i)); 1648 | } 1649 | sort(tmp.begin(),tmp.end()); 1650 | reverse(tmp.begin(),tmp.end()); 1651 | for(int i = 0; i < n; i++) 1652 | { 1653 | topo[i]=tmp[i].S; 1654 | if(i>0) assert(tmp[i].F!=tmp[i-1].F); 1655 | } 1656 | } 1657 | }; 1658 | // SCC end 1659 | 1660 | // HLD (Heavy-light decomposition) start 1661 | int in[MAXN],out[MAXN],tmr=-1; 1662 | int prt[MAXN],sz[MAXN],dep[MAXN]; 1663 | int top[MAXN]; 1664 | 1665 | void dfs_sz(int u, int p) 1666 | { 1667 | sz[u] = 1; 1668 | prt[u] = p; 1669 | if(sz(adj[u])>1 && adj[u][0]==p) swap(adj[u][0], adj[u][1]); 1670 | 1671 | for(auto &v: adj[u]) 1672 | { 1673 | if(v == p) continue; 1674 | dep[v] = dep[u] + 1; 1675 | dfs_sz(v, u); 1676 | sz[u] += sz[v]; 1677 | if(sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]); 1678 | } 1679 | } 1680 | void dfs_hld(int u, int p) 1681 | { 1682 | if(p == -1) top[u] = u; 1683 | in[u] = ++tmr; 1684 | for(int v: adj[u]) 1685 | { 1686 | if(v == p) continue; 1687 | top[v] = (v == adj[u][0]) ? top[u] : v; 1688 | dfs_hld(v, u); 1689 | } 1690 | out[u] = tmr; 1691 | } 1692 | inline void init_hld(int rt){ dfs_sz(rt, -1); dfs_hld(rt, -1); } 1693 | inline ll merge_hld(ll x, ll y){ return x + y; } 1694 | ll Query(int u, int v) 1695 | { 1696 | ll ans = 0; // dummy value 1697 | while(top[u] != top[v]) 1698 | { 1699 | if(dep[top[u]] < dep[top[v]]) swap(u, v); 1700 | ans = merge_hld(ans, st.query(in[top[u]], in[u])); 1701 | u = prt[top[u]]; 1702 | } 1703 | if(dep[u] < dep[v]) swap(u, v); 1704 | return merge_hld(ans, st.query(in[v], in[u])); 1705 | } 1706 | // For lazy segtree, untested (I can't find problems to test) 1707 | void Update(int u, int v, ll val) 1708 | { 1709 | while(top[u] != top[v]) 1710 | { 1711 | if(dep[top[u]] < dep[top[v]]) swap(u, v); 1712 | st.update(in[top[u]], in[u], val); 1713 | u = prt[top[u]]; 1714 | } 1715 | if(dep[u] < dep[v]) swap(u, v); 1716 | st.update(in[v], in[u], val); 1717 | } 1718 | 1719 | // init_hld(root) -> update/queries 1720 | // Point update: st.update(in[u],w); 1721 | // Update, Query: Range update/query on the path between u, v 1722 | // HLD (Heavy-light decomposition) end 1723 | 1724 | // LCA euler O(log n) query start 1725 | const int LG = 21; 1726 | 1727 | int in[MAXN],out[MAXN],tmr=-1; 1728 | int prt[LG][MAXN]; 1729 | mset(prt,-1); 1730 | 1731 | void dfs_lca(int u, int p) 1732 | { 1733 | in[u]=++tmr; 1734 | prt[0][u]=p; 1735 | forn(i,1,LG){ 1736 | if(prt[i-1][u]!=-1) prt[i][u]=prt[i-1][prt[i-1][u]]; 1737 | } 1738 | for(int v: adj[u]){ 1739 | if(v==p) continue; 1740 | dfs_lca(v,u); 1741 | } 1742 | out[u]=tmr; 1743 | } 1744 | bool isChild(int u, int v) 1745 | { 1746 | return in[u]<=in[v] && out[v]<=out[u]; 1747 | } 1748 | int getLca(int u, int v) 1749 | { 1750 | if(isChild(u,v)) return u; 1751 | for(int i=LG-1;i>=0;i--){ 1752 | if(prt[i][u]!=-1 && !isChild(prt[i][u],v)) 1753 | u=prt[i][u]; 1754 | } 1755 | return prt[0][u]; 1756 | } 1757 | // LCA euler O(log n) query end 1758 | 1759 | // LCA depth O(log n) query start 1760 | const int LG = 20; 1761 | 1762 | int dep[MAXN], prt[MAXN][LG]; 1763 | mset(prt,-1); mset(dep,0); 1764 | 1765 | void dfs_lca(int u, int p) 1766 | { 1767 | prt[u][0]=p; 1768 | forn(j,1,LG){ 1769 | if(prt[u][j-1]!=-1) prt[u][j]=prt[prt[u][j-1]][j-1]; 1770 | } 1771 | for(int v: adj[u]) 1772 | { 1773 | if(v==p) continue; 1774 | dep[v]=dep[u]+1; 1775 | dfs_lca(v,u); 1776 | } 1777 | } 1778 | 1779 | int lca(int u, int v) 1780 | { 1781 | if(dep[u]>dep[v]) swap(u,v); 1782 | for(int i=LG-1;i>=0;i--) 1783 | { 1784 | if(prt[v][i]!=-1 && dep[prt[v][i]]>=dep[u]) 1785 | { 1786 | v=prt[v][i]; 1787 | } 1788 | } 1789 | if(u==v) return u; 1790 | for(int i=LG-1;i>=0;i--) 1791 | { 1792 | if(prt[v][i]!=-1 && prt[v][i]!=prt[u][i]) 1793 | { 1794 | v=prt[v][i]; u=prt[u][i]; 1795 | } 1796 | } 1797 | return prt[u][0]; 1798 | } 1799 | // LCA depth O(log n) query end 1800 | 1801 | // Binary parent start 1802 | int goup(int u, int h){ 1803 | for(int i=LG-1;i>=0;i--){ 1804 | if(h&(1<r) swap(l,r); 1857 | return lcast.query(l,r); 1858 | } 1859 | 1860 | // in main() 1861 | dfs_lca(0,-1); 1862 | lcast=SparseTableLCA(euler); 1863 | 1864 | // LCA O(1) query end 1865 | 1866 | // Centroid decomposition start 1867 | int sz[MAXN]; 1868 | bool vst[MAXN]; 1869 | int cprt[MAXN]; // centroid tree parent 1870 | vector child[MAXN]; // subtree of centroid tree 1871 | mset(cprt,-1); 1872 | 1873 | void dfs_sz(int u, int p) 1874 | { 1875 | sz[u]=1; 1876 | for(int v: adj[u]) 1877 | { 1878 | if(v==p || vst[v]) continue; 1879 | dfs_sz(v,u); 1880 | sz[u]+=sz[v]; 1881 | } 1882 | } 1883 | int centroid(int u, int p, int r) 1884 | { 1885 | for(int v: adj[u]) 1886 | { 1887 | if(v==p || vst[v]) continue; 1888 | if(sz[v]*2>sz[r]) return centroid(v,u,r); 1889 | } 1890 | return u; 1891 | } 1892 | int build_tree(int u) 1893 | { 1894 | dfs_sz(u,-1); 1895 | u=centroid(u,-1,u); 1896 | vst[u]=1; 1897 | for(int v: adj[u]) 1898 | { 1899 | if(vst[v]) continue; 1900 | cprt[build_tree(v)]=u; 1901 | } 1902 | return u; 1903 | } 1904 | void prep(int u, int p) 1905 | { 1906 | for(int v: adj[u]) 1907 | { 1908 | if(v==p || vst[v]) continue; 1909 | 1910 | prep(v, u); 1911 | } 1912 | } 1913 | void solve(int u) 1914 | { 1915 | dfs_sz(u,-1); 1916 | u=centroid(u,-1,u); 1917 | 1918 | prep(u,-1); 1919 | for(int v: adj[u]) 1920 | { 1921 | if(vst[v]) continue; 1922 | 1923 | } 1924 | 1925 | // do stuffs 1926 | 1927 | vst[u]=1; 1928 | for(int v: adj[u]) 1929 | { 1930 | if(vst[v]) continue; 1931 | solve(v); 1932 | } 1933 | } 1934 | // Centroid decomposition end 1935 | 1936 | // Virtual tree start 1937 | // Modifies vadj 1938 | int buildVirtualTree(vector nodes, vi vadj[]) 1939 | { 1940 | // Change these as needed 1941 | auto reset = [&](int u) { 1942 | vadj[u].clear(); 1943 | }; 1944 | auto connect = [&](int u, int v) { // u is parent of v 1945 | vadj[u].push_back(v); 1946 | }; 1947 | 1948 | auto cmpDfs = [&](int u, int v) { 1949 | return in[u] < in[v]; 1950 | }; 1951 | sort(nodes.begin(), nodes.end(), cmpDfs); 1952 | unordered_set uniqueNodes(nodes.begin(), nodes.end()); 1953 | for (int i{1}; i < sz(nodes); i++) 1954 | uniqueNodes.insert(getLca(nodes[i - 1], nodes[i])); 1955 | nodes = vector(uniqueNodes.begin(), uniqueNodes.end()); 1956 | sort(nodes.begin(), nodes.end(), cmpDfs); 1957 | for_each(nodes.begin(), nodes.end(), reset); 1958 | 1959 | stack stk; 1960 | for (int u : nodes) 1961 | { 1962 | if (stk.empty()) { stk.push(u); continue; } 1963 | while (!isChild(stk.top(), u)) stk.pop(); 1964 | connect(stk.top(), u); 1965 | stk.push(u); 1966 | } 1967 | return nodes[0]; 1968 | } 1969 | // Virtual tree end 1970 | 1971 | // Sparse Table start: O(1) Max Query 1972 | struct SparseTable 1973 | { 1974 | vector lg; 1975 | vector> spt; 1976 | 1977 | SparseTable(){} 1978 | SparseTable(int n, ll arr[]){ 1979 | lg.resize(n + 1); 1980 | spt.resize(n, vector(LG + 1)); 1981 | lg[1]=0; 1982 | fore(i,2,n) lg[i] = lg[i/2] + 1; 1983 | forn(i,0,n) spt[i][0] = arr[i]; 1984 | fore(j,1,LG) 1985 | for(int i=0; i+(1<> { 2008 | const ll inf = LLONG_MAX; //double: inf = 1.0L 2009 | bool Max = true; 2010 | inline ll div(ll a, ll b){ 2011 | return a/b - ((a^b)<0 && a%b); 2012 | } 2013 | bool isect(iterator x, iterator y){ 2014 | if(y == end()){ x->p = inf; return false; } 2015 | if(x->k == y->k) x->p = x->m > y->m ? inf : -inf; 2016 | else x->p = div(y->m - x->m, x->k - y->k); 2017 | return x->p >= y->p; 2018 | } 2019 | void add(ll k, ll m){ // k = slope, m = y-intercept 2020 | if(!Max) k = -k, m = -m; 2021 | auto z = insert({k, m, 0}), y = z++, x = y; 2022 | while(isect(y, z)) z = erase(z); 2023 | if(x!=begin() && isect(--x, y)) 2024 | isect(x, y = erase(y)); 2025 | while((y=x) != begin() && (--x)->p >= y->p) 2026 | isect(x, erase(y)); 2027 | } 2028 | ll query(ll x){ 2029 | if(empty()) return Max ? 0 : inf; 2030 | auto l = *lower_bound(x); 2031 | return (l.k * x + l.m) * (Max ? 1 : -1); 2032 | } 2033 | }; 2034 | // Convex Hull Dynamic end (CHT) 2035 | 2036 | // Li Chao Tree start 2037 | struct Line { 2038 | ll m,c; 2039 | Line(): m(0), c(INF) {} 2040 | Line(ll m, ll c): m(m), c(c) {} 2041 | ll eval(ll x){ return m*x+c; } 2042 | }; 2043 | 2044 | struct LiChaoTree { 2045 | int sz; 2046 | bool isMax; // whether this maintains max 2047 | vector v; 2048 | LiChaoTree(): sz(0), isMax(false), v(vector()) {} 2049 | LiChaoTree(int sz, bool isMax): sz(sz), isMax(isMax) { 2050 | v.resize(sz*4, {0,INF}); 2051 | } 2052 | void addline(Line& val) { 2053 | if(isMax) { 2054 | val.m = -val.m; 2055 | val.c = -val.c; 2056 | } 2057 | addline(val, 1, 0, sz-1); 2058 | } 2059 | ll query(int x) { 2060 | return (isMax ? -1 : 1) * query(x, 1, 0, sz-1); 2061 | } 2062 | void addline(Line& val, int k, int l, int r) { 2063 | int mid = (l+r)>>1; 2064 | bool lc = val.eval(l) <= v[k].eval(l); 2065 | bool mc = val.eval(mid) <= v[k].eval(mid); 2066 | if(mc) swap(val, v[k]); 2067 | if(l==r) return; 2068 | if(lc==mc) addline(val, k*2, l, mid); 2069 | else addline(val, k*2+1, mid+1, r); 2070 | } 2071 | ll query(int x, int k, int l, int r) { 2072 | ll cur = v[k].eval(x); 2073 | if(l==r) return cur; 2074 | int mid=(l+r)>>1; 2075 | if(x<=mid) return min(cur, query(x, k*2, l, mid)); 2076 | return min(cur, query(x, k*2+1, mid+1, r)); 2077 | } 2078 | }; 2079 | // Li Chao Tree end 2080 | 2081 | // Convex Hull fast start (CHT) 2082 | struct Line { 2083 | ll m, b; 2084 | Line(ll _m, ll _b): m(_m), b(_b) {} 2085 | inline ll eval(ll x){ return m*x+b; } 2086 | }; 2087 | 2088 | struct ConvexHull { 2089 | deque d; 2090 | inline void clear(){ d.clear(); } 2091 | bool bad(const Line &Z){ 2092 | if(int(d.size())<2) return false; 2093 | const Line &X = d[int(d.size())-2], &Y = d[int(d.size())-1]; 2094 | return (X.b-Z.b)*(Y.m-X.m) <= (X.b-Y.b)*(Z.m-X.m); 2095 | } 2096 | void addline(ll m, ll b){ 2097 | Line l = Line(m,b); 2098 | while(bad(l)) d.pop_back(); 2099 | d.push_back(l); 2100 | } 2101 | ll query(ll x){ 2102 | if(d.empty()) return 0; 2103 | while(int(d.size())>1 && (d[0].b-d[1].b <= x*(d[1].m-d[0].m))) d.pop_front(); 2104 | return d.front().eval(x); 2105 | } 2106 | }; 2107 | // Convex Hull fast end (CHT) 2108 | 2109 | // Combi/Maths start 2110 | vector fact,ifact,inv,pow2; 2111 | ll add(ll a, ll b, ll m = MOD) 2112 | { 2113 | a+=b; 2114 | if(abs(a)>=m) a%=m; 2115 | if(a<0) a+=m; 2116 | return a; 2117 | } 2118 | ll mult(ll a, ll b, ll m = MOD) 2119 | { 2120 | if(abs(a)>=m) a%=m; 2121 | if(abs(b)>=m) b%=m; 2122 | a*=b; 2123 | if(abs(a)>=m) a%=m; 2124 | if(a<0) a+=m; 2125 | return a; 2126 | } 2127 | void radd(ll &a, ll b, ll m = MOD){ a=add(a,b,m); } 2128 | void rmult(ll &a, ll b, ll m = MOD){ a=mult(a,b,m); } 2129 | ll pw(ll a, ll b, ll m = MOD) 2130 | { 2131 | assert(b >= 0); // can return 0 if desired 2132 | if(abs(a)>=m) a%=m; 2133 | if(a==0 && b==0) return 0; // value of 0^0 2134 | ll r=1; 2135 | while(b){ 2136 | if(b&1) r=mult(r,a,m); 2137 | a=mult(a,a,m); 2138 | b>>=1; 2139 | } 2140 | return r; 2141 | } 2142 | ll inverse(ll a, ll m = MOD) 2143 | { 2144 | return pw(a,m-2,m); 2145 | } 2146 | ll choose(ll a, ll b, ll m = MOD) 2147 | { 2148 | if(a=1;i--){ 2164 | ifact[i] = mult(ifact[i + 1], i + 1, m); 2165 | } 2166 | for(int i=1;i<=_n;i++){ 2167 | inv[i] = mult(fact[i - 1], ifact[i], m); 2168 | } 2169 | } 2170 | // partition n into k blocks of size >= 0 2171 | ll nonneg_partition(ll n, ll k) 2172 | { 2173 | assert(k >= 1); // can return 0 if desired 2174 | return choose(n + k - 1, k - 1); 2175 | } 2176 | // partition n into k blocks of size >= minVal 2177 | ll partition(ll n, ll k, ll minVal = 1) 2178 | { 2179 | assert(k >= 1); // can return 0 if desired 2180 | return nonneg_partition(n - k * minVal, k); 2181 | } 2182 | void getpf(vector& pf, ll n) 2183 | { 2184 | for(ll i=2; i*i<=n; i++) 2185 | { 2186 | int cnt=0; 2187 | while(n%i==0){ 2188 | n/=i; cnt++; 2189 | } 2190 | if(cnt>0) pf.pb({i,cnt}); 2191 | } 2192 | if(n>1) pf.pb({n,1}); 2193 | } 2194 | // Combi/Maths end 2195 | 2196 | // Matrix start 2197 | struct Matrix{ 2198 | vector> a; 2199 | vector& operator[](int x){ return a[x]; } 2200 | inline int r(){ return a.size(); } 2201 | inline int c(){ return (a.size() ? a[0].size() : 0); } 2202 | 2203 | Matrix(int r_ = 0, int c_ = 0, bool identity = 0){ 2204 | a.resize(r_, vector(c_)); 2205 | if(identity){ 2206 | assert(r_ == c_); 2207 | for(int i = 0; i < r_; i++) a[i][i] = 1; 2208 | } 2209 | } 2210 | inline Matrix(const vector>& v){ a = v; } 2211 | inline void operator=(const vector>& v){ a = v; } 2212 | }; 2213 | Matrix operator*(Matrix A, Matrix B){ 2214 | assert(A.c() == B.r()); 2215 | const ll MOD2 = ll(MOD) * MOD; //MOD 2216 | Matrix C(A.r(), B.c()); 2217 | for(int i = 0; i < A.r(); i++){ 2218 | for(int j = 0; j < B.c(); j++){ 2219 | ll w = 0; 2220 | for(int k = 0; k < A.c(); k++){ 2221 | w += ll(A[i][k]) * B[k][j]; 2222 | if(w >= MOD2) w -= MOD2; //MOD 2223 | } 2224 | C[i][j] = w % MOD; //MOD 2225 | } 2226 | } 2227 | return C; 2228 | } 2229 | Matrix operator^(Matrix A, ll b){ 2230 | assert(A.r() == A.c()); 2231 | Matrix R = Matrix(A.r(), A.r(), 1); 2232 | for(; b; b >>= 1){ 2233 | if(b & 1) R = R * A; 2234 | A = A * A; 2235 | } 2236 | return R; 2237 | } 2238 | // Matrix end 2239 | 2240 | // Number Theory NT start 2241 | vector primes, totient, sumdiv, bigdiv, lowprime, mobius; 2242 | vector isprime; 2243 | void Sieve(ll n) // linear Sieve 2244 | { 2245 | isprime.assign(n+1, 1); 2246 | lowprime.assign(n+1, 0); 2247 | isprime[1] = false; 2248 | for(ll i = 2; i <= n; i++) 2249 | { 2250 | if(lowprime[i] == 0) 2251 | { 2252 | primes.pb(i); 2253 | lowprime[i] = i; 2254 | } 2255 | for(int j=0; j pf; 2280 | ll num = 1; ll num2 = x; 2281 | for(ll i = 0; primes[i]*primes[i] <= x; i++) 2282 | { 2283 | if(x%primes[i]==0) 2284 | { 2285 | num2/=primes[i]; 2286 | num*=(primes[i]-1); 2287 | } 2288 | while(x%primes[i]==0) 2289 | { 2290 | x/=primes[i]; 2291 | pf[primes[i]]++; 2292 | } 2293 | } 2294 | if(x>1) 2295 | { 2296 | pf[x]++; num2/=x; num*=(x-1); 2297 | } 2298 | x = 1; 2299 | num*=num2; 2300 | return num; 2301 | } 2302 | bool isprime(ll x) 2303 | { 2304 | if(x==1) return false; 2305 | for(ll i = 0; primes[i]*primes[i] <= x; i++) 2306 | { 2307 | if(x%primes[i]==0) return false; 2308 | } 2309 | return true; 2310 | } 2311 | void SievePhi(ll n) 2312 | { 2313 | totient.resize(n+1); 2314 | for (int i = 1; i <= n; ++i) totient[i] = i; 2315 | for (int i = 2; i <= n; ++i) 2316 | { 2317 | if (totient[i] == i) 2318 | { 2319 | for (int j = i; j <= n; j += i) 2320 | { 2321 | totient[j] -= totient[j] / i; 2322 | } 2323 | } 2324 | } 2325 | } 2326 | void SieveSumDiv(ll n) 2327 | { 2328 | sumdiv.resize(n+1); 2329 | for(int i = 1; i <= n; ++i) 2330 | { 2331 | for(int j = i; j <= n; j += i) 2332 | { 2333 | sumdiv[j] += i; 2334 | } 2335 | } 2336 | } 2337 | ll getPhi(ll n) 2338 | { 2339 | return totient[n]; 2340 | } 2341 | ll getSumDiv(ll n) 2342 | { 2343 | return sumdiv[n]; 2344 | } 2345 | ll pw(ll a, ll b, ll mod) 2346 | { 2347 | ll r = 1; 2348 | if(b < 0) b += mod*100000LL; 2349 | while(b) 2350 | { 2351 | if(b&1) r = (r*a)%mod; 2352 | a = (a*a)%mod; 2353 | b>>=1; 2354 | } 2355 | return r; 2356 | } 2357 | ll inv(ll a, ll mod) 2358 | { 2359 | return pw(a, mod - 2, mod); 2360 | } 2361 | ll invgeneral(ll a, ll mod) 2362 | { 2363 | ll ph = phi(mod); 2364 | ph--; 2365 | return pw(a, ph, mod); 2366 | } 2367 | void getpf(vector& pf, ll n) 2368 | { 2369 | for(ll i = 0; primes[i]*primes[i] <= n; i++) 2370 | { 2371 | int cnt = 0; 2372 | while(n%primes[i]==0) 2373 | { 2374 | n/=primes[i]; cnt++; 2375 | } 2376 | if(cnt>0) pf.pb(ii(primes[i], cnt)); 2377 | } 2378 | if(n>1) 2379 | { 2380 | pf.pb(ii(n, 1)); 2381 | } 2382 | } 2383 | void getdiv(vector& div, vector& pf, ll n = 1, int i = 0) 2384 | { 2385 | if (pf.empty()) // divisors of 1 2386 | { 2387 | div = {1}; 2388 | return; 2389 | } 2390 | ll x, k; 2391 | if(i >= sz(pf)) return; 2392 | x = n; 2393 | for(k = 0; k <= pf[i].S; k++) 2394 | { 2395 | if(i == sz(pf) - 1) div.pb(x); 2396 | getdiv(div, pf, x, i + 1); 2397 | x *= pf[i].F; 2398 | } 2399 | } 2400 | // End Number Theory NT 2401 | 2402 | // Longest increasing subsequence (lis) start 2403 | ll lisend[MAXN]; 2404 | int lislen[MAXN], idx[MAXN], prt[MAXN]; 2405 | int lis(int n, ll a[]) 2406 | { 2407 | const ll inf = 2e9; 2408 | 2409 | lisend[0]=-inf; 2410 | for(int i=1;iql) 2454 | { 2455 | add(--L); 2456 | } 2457 | while(Rqr) 2466 | { 2467 | remove(R--); 2468 | } 2469 | } 2470 | // Sqrt decomposition/Mo's algorithm end 2471 | 2472 | // FFT (Fast Fourier Transform) start 2473 | typedef complex cd; 2474 | const ld PI = acos(-1); 2475 | void fft(vector &a, bool invert) 2476 | { 2477 | int n=a.size(); 2478 | for(int i=1,j=0;i>1; 2481 | for(;j&bit;bit>>=1) j^=bit; 2482 | j^=bit; 2483 | if(i fa(n),fb(n); 2509 | for(int i=0;i> 1; 2534 | for (; j & bit; bit >>= 1) 2535 | j ^= bit; 2536 | j ^= bit; 2537 | 2538 | if (i < j) 2539 | swap(a[i], a[j]); 2540 | } 2541 | 2542 | for (int len = 2; len <= n; len <<= 1) { 2543 | int wlen = invert ? ROOT_1 : ROOT; 2544 | for (int i = len; i < ROOT_PW; i <<= 1) 2545 | wlen = (int)(1LL * wlen * wlen % MOD); 2546 | 2547 | for (int i = 0; i < n; i += len) { 2548 | int w = 1; 2549 | for (int j = 0; j < len / 2; j++) { 2550 | int u = a[i+j], v = (int)(1LL * a[i+j+len/2] * w % MOD); 2551 | a[i+j] = u + v < MOD ? u + v : u + v - MOD; 2552 | a[i+j+len/2] = u - v >= 0 ? u - v : u - v + MOD; 2553 | w = (int)(1LL * w * wlen % MOD); 2554 | } 2555 | } 2556 | } 2557 | 2558 | if (invert) { 2559 | int n_1 = inverse(n); 2560 | for (int & x : a) 2561 | x = (int)(1LL * x * n_1 % MOD); 2562 | } 2563 | } 2564 | vi mult(vi &a, vi &b) 2565 | { 2566 | int n=1; 2567 | while(n(A, B); 2584 | 2585 | // Source: http://neerc.ifmo.ru/trains/toulouse/2017/fft2.pdf 2586 | typedef complex CD; 2587 | void fft(vector& a) { 2588 | int n = a.size(), L = 31 - __builtin_clz(n); 2589 | static vector> R(2, 1); 2590 | static vector rt(2, 1); // (^ 10% faster if double) 2591 | for (static int k = 2; k < n; k *= 2) { 2592 | R.resize(n); rt.resize(n); 2593 | auto x = polar(1.0L, acos(-1.0L) / k); 2594 | forn(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; 2595 | } 2596 | vector rev(n); 2597 | forn(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2; 2598 | forn(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]); 2599 | for (int k = 1; k < n; k *= 2) 2600 | for (int i = 0; i < n; i += 2*k) forn(j,0,k) { 2601 | // CD z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line 2602 | auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line 2603 | CD z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line 2604 | a[i + j + k] = a[i + j] - z; 2605 | a[i + j] += z; 2606 | } 2607 | } 2608 | 2609 | typedef vector vl; 2610 | template vl convMod(const vl &a, const vl &b) { 2611 | if (a.empty() || b.empty()) return {}; 2612 | vl res(a.size() + b.size() - 1); 2613 | int B=32-__builtin_clz((int)res.size()), n=1< L(n), R(n), outs(n), outl(n); 2615 | forn(i,0,a.size()) L[i] = CD((int)a[i] / cut, (int)a[i] % cut); 2616 | forn(i,0,b.size()) R[i] = CD((int)b[i] / cut, (int)b[i] % cut); 2617 | fft(L), fft(R); 2618 | forn(i,0,n) { 2619 | int j = -i & (n - 1); 2620 | outl[j] = (L[i] + conj(L[j])) * R[i] / (2.0 * n); 2621 | outs[j] = (L[i] - conj(L[j])) * R[i] / (2.0 * n) / 1i; 2622 | } 2623 | fft(outl), fft(outs); 2624 | forn(i,0,res.size()) { 2625 | ll av = ll(real(outl[i])+.5), cv = ll(imag(outs[i])+.5); 2626 | ll bv = ll(imag(outl[i])+.5) + ll(real(outs[i])+.5); 2627 | res[i] = ((av % M * cut + bv) % M * cut + cv) % M; 2628 | } 2629 | return res; 2630 | } 2631 | // FFT mod end 2632 | 2633 | // Fast Walsh-Hadamard Transform (FWHT) start 2634 | // Source: https://alan20210202.github.io/2020/08/07/FWHT/ 2635 | // Modded xor FWHT function tested on AtCoder ABC212 H 2636 | // 2637 | // type: 1 = or, 2 = and, 3 = xor 2638 | // n is the smallest power of 2 at least sz(A) 2639 | // dir = 1/-1 for forward/inverse 2640 | // can change int *A to ll *A 2641 | void fwht(vector &A, int type, int dir = 1) { 2642 | int n = 1; 2643 | while(n < sz(A)) n <<= 1; 2644 | A.resize(n, 0); 2645 | for (int s = 2, h = 1; s <= n; s <<= 1, h <<= 1) 2646 | for (int l = 0; l < n; l += s) 2647 | for (int i = 0; i < h; i++) { 2648 | if (type == 1) // or 2649 | A[l + h + i] += dir * A[l + i]; 2650 | if (type == 2) // and 2651 | A[l + i] += dir * A[l + h + i]; 2652 | if (type == 3) { // xor 2653 | int t = A[l + h + i]; 2654 | A[l + h + i] = A[l + i] - t; 2655 | A[l + i] = A[l + i] + t; 2656 | if(dir < 0) A[l + h + i] /= 2, A[l + i] /= 2; 2657 | } 2658 | } 2659 | } 2660 | vector mult(vector &a, vector &b, int type) 2661 | { 2662 | fwht(a,type); 2663 | fwht(b,type); 2664 | vector ans(sz(a)); 2665 | for(int i=0;i &A, int type, int dir = 1) { 2671 | const ll inv2 = inverse(2); 2672 | int n = 1; 2673 | while(n < sz(A)) n <<= 1; 2674 | A.resize(n, 0); 2675 | for (int s = 2, h = 1; s <= n; s <<= 1, h <<= 1) 2676 | for (int l = 0; l < n; l += s) 2677 | for (int i = 0; i < h; i++) { 2678 | if (type == 1) // or 2679 | A[l + h + i] = add(A[l + h + i], dir * A[l + i]); 2680 | if (type == 2) // and 2681 | A[l + i] = add(A[l + i], dir * A[l + h + i]); 2682 | if (type == 3) { // xor 2683 | int t = A[l + h + i]; 2684 | A[l + h + i] = add(A[l + i], -t); 2685 | A[l + i] = add(A[l + i], t); 2686 | if(dir < 0) { 2687 | A[l + h + i] = mult(A[l + h + i], inv2); 2688 | A[l + i] = mult(A[l + i], inv2); 2689 | } 2690 | } 2691 | } 2692 | } 2693 | vector mult(vector &a, vector &b, int type) 2694 | { 2695 | fwht(a,type); 2696 | fwht(b,type); 2697 | vector ans(sz(a)); 2698 | for(int i=0;i vd; 2706 | const double eps = 1e-12; 2707 | int solveLinear(vector& A, vd& b, vd& x) { 2708 | int n = sz(A), m = sz(x), rank = 0, br, bc; 2709 | if (n) assert(sz(A[0]) == m); 2710 | vi col(m); iota(all(col), 0); 2711 | forn(i,0,n) { 2712 | double v, bv = 0; 2713 | forn(r,i,n) forn(c,i,m) 2714 | if ((v = fabs(A[r][c])) > bv) 2715 | br = r, bc = c, bv = v; 2716 | if (bv <= eps) { 2717 | forn(j,i,n) if (fabs(b[j]) > eps) return -1; 2718 | break; 2719 | } 2720 | swap(A[i], A[br]); 2721 | swap(b[i], b[br]); 2722 | swap(col[i], col[bc]); 2723 | forn(j,0,n) swap(A[j][i], A[j][bc]); 2724 | bv = 1/A[i][i]; 2725 | forn(j,i+1,n) { 2726 | double fac = A[j][i] * bv; 2727 | b[j] -= fac * b[i]; 2728 | forn(k,i+1,m) A[j][k] -= fac*A[i][k]; 2729 | } 2730 | rank++; 2731 | } 2732 | x.assign(m, 0); 2733 | for (int i = rank; i--;) { 2734 | b[i] /= A[i][i]; 2735 | x[col[i]] = b[i]; 2736 | forn(j,0,i) b[j] -= A[j][i] * b[i]; 2737 | } 2738 | return rank; // (multiple solutions i f rank < m) 2739 | } 2740 | // Gauss elimination end 2741 | 2742 | // KACTL Geometry start 2743 | template int sgn(T x) { return (x > 0) - (x < 0); } 2744 | template 2745 | struct Point { 2746 | typedef Point P; 2747 | T x, y; 2748 | explicit Point(T x=0, T y=0) : x(x), y(y) {} 2749 | bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); } 2750 | bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); } 2751 | P operator+(P p) const { return P(x+p.x, y+p.y); } 2752 | P operator-(P p) const { return P(x-p.x, y-p.y); } 2753 | P operator*(T d) const { return P(x*d, y*d); } 2754 | P operator/(T d) const { return P(x/d, y/d); } 2755 | T dot(P p) const { return x*p.x + y*p.y; } 2756 | T cross(P p) const { return x*p.y - y*p.x; } 2757 | T cross(P a, P b) const { return (a-*this).cross(b-*this); } 2758 | T dist2() const { return x*x + y*y; } 2759 | double dist() const { return sqrt((double)dist2()); } 2760 | // angle to x-axis in interval [-pi, pi] 2761 | double angle() const { return atan2(y, x); } 2762 | P unit() const { return *this/dist(); } // makes dist()=1 2763 | P perp() const { return P(-y, x); } // rotates +90 degrees 2764 | P normal() const { return perp().unit(); } 2765 | // returns point rotated 'a' radians ccw around the origin 2766 | P rotate(double a) const { 2767 | return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); } 2768 | friend ostream& operator<<(ostream& os, P p) { 2769 | return os << "(" << p.x << "," << p.y << ")"; } 2770 | }; 2771 | template struct Point3D { 2772 | typedef Point3D P; 2773 | typedef const P& R; 2774 | T x, y, z; 2775 | explicit Point3D(T x=0, T y=0, T z=0) : x(x), y(y), z(z) {} 2776 | bool operator<(R p) const { 2777 | return tie(x, y, z) < tie(p.x, p.y, p.z); } 2778 | bool operator==(R p) const { 2779 | return tie(x, y, z) == tie(p.x, p.y, p.z); } 2780 | P operator+(R p) const { return P(x+p.x, y+p.y, z+p.z); } 2781 | P operator-(R p) const { return P(x-p.x, y-p.y, z-p.z); } 2782 | P operator*(T d) const { return P(x*d, y*d, z*d); } 2783 | P operator/(T d) const { return P(x/d, y/d, z/d); } 2784 | T dot(R p) const { return x*p.x + y*p.y + z*p.z; } 2785 | P cross(R p) const { 2786 | return P(y*p.z - z*p.y, z*p.x - x*p.z, x*p.y - y*p.x); 2787 | } 2788 | T dist2() const { return x*x + y*y + z*z; } 2789 | double dist() const { return sqrt((double)dist2()); } 2790 | //Azimuthal angle (longitude) to x-axis in interval [-pi, pi] 2791 | double phi() const { return atan2(y, x); } 2792 | //Zenith angle (latitude) to the z-axis in interval [0, pi] 2793 | double theta() const { return atan2(sqrt(x*x+y*y),z); } 2794 | P unit() const { return *this/(T)dist(); } //makes dist()=1 2795 | //returns unit vector normal to *this and p 2796 | P normal(P p) const { return cross(p).unit(); } 2797 | //returns point rotated 'angle' radians ccw around axis 2798 | P rotate(double angle, P axis) const { 2799 | double s = sin(angle), c = cos(angle); P u = axis.unit(); 2800 | return u*dot(u)*(1-c) + (*this)*c - cross(u)*s; 2801 | } 2802 | }; 2803 | 2804 | // ------- Lines start ------- 2805 | template 2806 | double lineDist(const P& a, const P& b, const P& p) { 2807 | return (double)(b-a).cross(p-a)/(b-a).dist(); 2808 | } 2809 | template 2810 | int sideOf(P s, P e, P p) { return sgn(s.cross(e, p)); } 2811 | 2812 | template 2813 | int sideOf(const P& s, const P& e, const P& p, double eps) { 2814 | auto a = (e-s).cross(p-s); 2815 | double l = (e-s).dist()*eps; 2816 | return (a > l) - (a < -l); 2817 | } 2818 | // {0/-1,{0,0}} if no inter/infinite inter, {1,point} otherwise 2819 | template 2820 | pair lineInter(P s1, P e1, P s2, P e2) { 2821 | auto d = (e1 - s1).cross(e2 - s2); 2822 | if (d == 0) // if parallel 2823 | return {-(s1.cross(e1, s2) == 0), P(0, 0)}; 2824 | auto p = s2.cross(e1, e2), q = s2.cross(e2, s1); 2825 | return {1, (s1 * p + e1 * q) / d}; 2826 | } 2827 | // projects p on line ab, refl returns reflection instead 2828 | template 2829 | P lineProj(P a, P b, P p, bool refl=false) { 2830 | P v = b - a; 2831 | return p - v.perp()*(1+refl)*v.cross(p-a)/v.dist2(); 2832 | } 2833 | #define P Point 2834 | P linearTransformation(const P& p0, const P& p1, 2835 | const P& q0, const P& q1, const P& r) { 2836 | P dp = p1-p0, dq = q1-q0, num(dp.cross(dq), dp.dot(dq)); 2837 | return q0 + P((r-p0).cross(num), (r-p0).dot(num))/dp.dist2(); 2838 | } 2839 | #undef P 2840 | // ------- Lines end ------- 2841 | 2842 | // ------- Segments start ------- 2843 | template bool onSegment(P s, P e, P p) { 2844 | return p.cross(s, e) == 0 && (s - p).dot(e - p) <= 0; 2845 | } 2846 | #define P Point 2847 | double segDist(P& s, P& e, P& p) { 2848 | if (s==e) return (p-s).dist(); 2849 | auto d = (e-s).dist2(), t = min(d,max(.0,(p-s).dot(e-s))); 2850 | return ((p-s)*d-(e-s)*t).dist()/d; 2851 | } 2852 | template vector

segInter(P a, P b, P c, P d) { 2853 | auto oa = c.cross(d, a), ob = c.cross(d, b), 2854 | oc = a.cross(b, c), od = a.cross(b, d); 2855 | // Checks if intersection is single non-endpoint point. 2856 | if (sgn(oa) * sgn(ob) < 0 && sgn(oc) * sgn(od) < 0) 2857 | return {(a * ob - b * oa) / (ob - oa)}; 2858 | set

s; 2859 | if (onSegment(c, d, a)) s.insert(a); 2860 | if (onSegment(c, d, b)) s.insert(b); 2861 | if (onSegment(a, b, c)) s.insert(c); 2862 | if (onSegment(a, b, d)) s.insert(d); 2863 | return {all(s)}; 2864 | } 2865 | #undef 2866 | // ------- Segments end ------- 2867 | 2868 | // ------- Polygons start ------- 2869 | // The double of the area, points ccw order (cw gives negative) 2870 | template 2871 | T polygonArea2(vector>& v) { 2872 | T a = v.back().cross(v[0]); 2873 | rep(i,0,sz(v)-1) a += v[i].cross(v[i+1]); 2874 | return a; 2875 | } 2876 | template 2877 | bool inPolygon(vector

&p, P a, bool strict = true) { 2878 | int cnt = 0, n = sz(p); 2879 | rep(i,0,n) { 2880 | P q = p[(i + 1) % n]; 2881 | if (onSegment(p[i], q, a)) return !strict; 2882 | //or: if (segDist(p[i], q, a) <= eps) return !strict; 2883 | cnt ^= ((a.y 0; 2884 | } 2885 | return cnt; 2886 | } 2887 | #define P Point 2888 | P polygonCenter(const vector

& v) { 2889 | P res(0, 0); double A = 0; 2890 | for (int i = 0, j = sz(v) - 1; i < sz(v); j = i++) { 2891 | res = res + (v[i] + v[j]) * v[j].cross(v[i]); 2892 | A += v[j].cross(v[i]); 2893 | } 2894 | return res / A / 3; 2895 | } 2896 | vector

polygonCut(const vector

& poly, P s, P e) { 2897 | vector

res; 2898 | rep(i,0,sz(poly)) { 2899 | P cur = poly[i], prev = i ? poly[i-1] : poly.back(); 2900 | bool side = s.cross(e, cur) < 0; 2901 | if (side != (s.cross(e, prev) < 0)) 2902 | res.push_back(lineInter(s, e, cur, prev).second); 2903 | if (side) 2904 | res.push_back(cur); 2905 | } 2906 | return res; 2907 | } 2908 | double rat(P a, P b) { return sgn(b.x) ? a.x/b.x : a.y/b.y; } 2909 | double polyUnion(vector>& poly) { 2910 | double ret = 0; 2911 | rep(i,0,sz(poly)) rep(v,0,sz(poly[i])) { 2912 | P A = poly[i][v], B = poly[i][(v + 1) % sz(poly[i])]; 2913 | vector> segs = {{0, 0}, {1, 0}}; 2914 | rep(j,0,sz(poly)) if (i != j) { 2915 | rep(u,0,sz(poly[j])) { 2916 | P C = poly[j][u], D = poly[j][(u + 1) % sz(poly[j])]; 2917 | int sc = sideOf(A, B, C), sd = sideOf(A, B, D); 2918 | if (sc != sd) { 2919 | double sa = C.cross(D, A), sb = C.cross(D, B); 2920 | if (min(sc, sd) < 0) 2921 | segs.emplace_back(sa / (sa - sb), sgn(sc - sd)); 2922 | } else if (!sc && !sd && j0){ 2923 | segs.emplace_back(rat(C - A, B - A), 1); 2924 | segs.emplace_back(rat(D - A, B - A), -1); 2925 | } 2926 | } 2927 | } 2928 | sort(all(segs)); 2929 | for (auto& s : segs) s.first = min(max(s.first, 0.0), 1.0); 2930 | double sum = 0; 2931 | int cnt = segs[0].second; 2932 | rep(j,1,sz(segs)) { 2933 | if (!cnt) sum += segs[j].first - segs[j - 1].first; 2934 | cnt += segs[j].second; 2935 | } 2936 | ret += A.cross(B) * sum; 2937 | } 2938 | return ret / 2; 2939 | } 2940 | #undef P 2941 | template 2942 | double signedPolyVolume(const V& p, const L& trilist) { 2943 | double v = 0; 2944 | for (auto i : trilist) v += p[i.a].cross(p[i.b]).dot(p[i.c]); 2945 | return v / 6; 2946 | } 2947 | // ------- Polygons end ------- 2948 | 2949 | // ------- Hull start ------- 2950 | #define P Point 2951 | vector

convexHull(vector

pts) { 2952 | if (sz(pts) <= 1) return pts; 2953 | sort(all(pts)); 2954 | vector

h(sz(pts)+1); 2955 | int s = 0, t = 0; 2956 | for (int it = 2; it--; s = --t, reverse(all(pts))) 2957 | for (P p : pts) { 2958 | while (t >= s + 2 && h[t-2].cross(h[t-1], p) <= 0) t--; 2959 | h[t++] = p; 2960 | } 2961 | return {h.begin(), h.begin() + t - (t == 2 && h[0] == h[1])}; 2962 | } 2963 | #undef 2964 | // points ccw order, strict = exclude boundary 2965 | bool inHull(const vector>& l, Point p, bool strict = true) { 2966 | int a = 1, b = sz(l) - 1, r = !strict; 2967 | if (sz(l) < 3) return r && onSegment(l[0], l.back(), p); 2968 | if (sideOf(l[0], l[a], l[b]) > 0) swap(a, b); 2969 | if (sideOf(l[0], l[a], p) >= r || sideOf(l[0], l[b], p)<= -r) 2970 | return false; 2971 | while (abs(a - b) > 1) { 2972 | int c = (a + b) / 2; 2973 | (sideOf(l[0], l[c], p) > 0 ? b : a) = c; 2974 | } 2975 | return sgn(l[a].cross(l[b], p)) < r; 2976 | } 2977 | 2978 | // - Line hull intersection start 2979 | #define cmp(i,j) sgn(dir.perp().cross(poly[(i)%n]-poly[(j)%n])) 2980 | #define extr(i) cmp(i + 1, i) >= 0 && cmp(i, i - 1 + n) < 0 2981 | template int extrVertex(vector

& poly, P dir) { 2982 | int n = sz(poly), lo = 0, hi = n; 2983 | if (extr(0)) return 0; 2984 | while (lo + 1 < hi) { 2985 | int m = (lo + hi) / 2; 2986 | if (extr(m)) return m; 2987 | int ls = cmp(lo + 1, lo), ms = cmp(m + 1, m); 2988 | (ls < ms || (ls == ms && ls == cmp(lo, m)) ? hi : lo) = m; 2989 | } 2990 | return lo; 2991 | } 2992 | #define cmpL(i) sgn(a.cross(poly[i], b)) 2993 | template 2994 | array lineHull(P a, P b, vector

& poly) { 2995 | int endA = extrVertex(poly, (a - b).perp()); 2996 | int endB = extrVertex(poly, (b - a).perp()); 2997 | if (cmpL(endA) < 0 || cmpL(endB) > 0) 2998 | return {-1, -1}; 2999 | array res; 3000 | rep(i,0,2) { 3001 | int lo = endB, hi = endA, n = sz(poly); 3002 | while ((lo + 1) % n != hi) { 3003 | int m = ((lo + hi + (lo < hi ? 0 : n)) / 2) % n; 3004 | (cmpL(m) == cmpL(endB) ? lo : hi) = m; 3005 | } 3006 | res[i] = (lo + !cmpL(hi)) % n; 3007 | swap(endA, endB); 3008 | } 3009 | if (res[0] == res[1]) return {res[0], -1}; 3010 | if (!cmpL(res[0]) && !cmpL(res[1])) 3011 | switch ((res[0] - res[1] + sz(poly) + 1) % sz(poly)) { 3012 | case 0: return {res[0], res[0]}; 3013 | case 2: return {res[1], res[1]}; 3014 | } 3015 | return res; 3016 | } 3017 | // - Line hull intersection end 3018 | 3019 | // Closest pair of points O(n log n) 3020 | #define P Point 3021 | pair closest(vector

v) { 3022 | assert(sz(v) > 1); 3023 | set

S; 3024 | sort(all(v), [](P a, P b) { return a.y < b.y; }); 3025 | pair> ret{LLONG_MAX, {P(), P()}}; 3026 | int j = 0; 3027 | for (P p : v) { 3028 | P d{1 + (ll)sqrt(ret.first), 0}; 3029 | while (v[j].y <= p.y - d.x) S.erase(v[j++]); 3030 | auto lo = S.lower_bound(p - d), hi = S.upper_bound(p + d); 3031 | for (; lo != hi; ++lo) 3032 | ret = min(ret, {(*lo - p).dist2(), {*lo, p}}); 3033 | S.insert(p); 3034 | } 3035 | return ret.second; 3036 | } 3037 | #undef P 3038 | 3039 | // ------- Hull end------- 3040 | 3041 | // KD-tree start 3042 | #define T long long 3043 | #define P Point 3044 | const T INF = numeric_limits::max(); 3045 | 3046 | bool on_x(const P& a, const P& b) { return a.x < b.x; } 3047 | bool on_y(const P& a, const P& b) { return a.y < b.y; } 3048 | 3049 | struct Node { 3050 | P pt; // if this is a leaf, the single point in it 3051 | T x0 = INF, x1 = -INF, y0 = INF, y1 = -INF; // bounds 3052 | Node *first = 0, *second = 0; 3053 | 3054 | T distance(const P& p) { // min squared distance to a point 3055 | T x = (p.x < x0 ? x0 : p.x > x1 ? x1 : p.x); 3056 | T y = (p.y < y0 ? y0 : p.y > y1 ? y1 : p.y); 3057 | return (P(x,y) - p).dist2(); 3058 | } 3059 | 3060 | Node(vector

&& vp) : pt(vp[0]) { 3061 | for (P p : vp) { 3062 | x0 = min(x0, p.x); x1 = max(x1, p.x); 3063 | y0 = min(y0, p.y); y1 = max(y1, p.y); 3064 | } 3065 | if (vp.size() > 1) { 3066 | // split on x if width >= height (not ideal...) 3067 | sort(all(vp), x1 - x0 >= y1 - y0 ? on_x : on_y); 3068 | // divide by taking half the array for each child (not 3069 | // best performance with many duplicates in the middle) 3070 | int half = sz(vp)/2; 3071 | first = new Node({vp.begin(), vp.begin() + half}); 3072 | second = new Node({vp.begin() + half, vp.end()}); 3073 | } 3074 | } 3075 | }; 3076 | 3077 | struct KDTree { 3078 | Node* root; 3079 | KDTree(const vector

& vp) : root(new Node({all(vp)})) {} 3080 | 3081 | pair search(Node *node, const P& p) { 3082 | if (!node->first) { 3083 | // uncomment if we should not find the point itself: 3084 | // if (p == node->pt) return {INF, P()}; 3085 | return make_pair((p - node->pt).dist2(), node->pt); 3086 | } 3087 | 3088 | Node *f = node->first, *s = node->second; 3089 | T bfirst = f->distance(p), bsec = s->distance(p); 3090 | if (bfirst > bsec) swap(bsec, bfirst), swap(f, s); 3091 | 3092 | // search closest side first, other side if needed 3093 | auto best = search(f, p); 3094 | if (bsec < best.first) 3095 | best = min(best, search(s, p)); 3096 | return best; 3097 | } 3098 | 3099 | // find nearest point to a point, and its squared distance 3100 | // (requires an arbitrary operator< for Point) 3101 | pair nearest(const P& p) { 3102 | return search(root, p); 3103 | } 3104 | }; 3105 | #undef T 3106 | #undef P 3107 | // KD-tree end 3108 | 3109 | // KACTL Geometry end 3110 | 3111 | // Geometry (self-made) start (incomplete) 3112 | template struct Point { 3113 | T x,y; 3114 | Point(): x(0), y(0) {} 3115 | Point(T x, T y): x(x), y(y) {} 3116 | Point operator+(const Point &p) { return {x+p.x, y+p.y}; } 3117 | Point operator-(const Point &p) { return {x-p.x, y-p.y}; } 3118 | Point operator*(T p) { return {x*p, y*p}; } 3119 | Point operator/(T p) { return {x/p, y/p}; } 3120 | Point translate(const Point &v) { return *this+v; } 3121 | Point scale(const Point &c, ld factor) { return c+(*this-c)*factor; } 3122 | Point rotate(double d) { return *this * polar(1.0, d); } // counter-clockwise, d in rad 3123 | Point perp() { return {-y, x}; } 3124 | }; 3125 | template bool operator==(const Point &a, const Point &b) { return a.x==b.x && a.y==b.y; } 3126 | template bool operator!=(const Point &a, const Point &b) { return !(a==b); } 3127 | template ostream& operator<<(ostream& out, const Point &p) { return out<<"("< T sq(const Point &p) { return p.x*p.x + p.y*p.y; } 3129 | template ld abs(const Point &p) { return sqrtl(sq(p)); } 3130 | template T dot(const Point &a, const Point &b) { return a.x*b.x + a.y*b.y; } 3131 | template T cross(const Point &a, const Point &b) { return a.x*b.y - b.x*a.y; } 3132 | template bool isPerp(const Point &a, const Point &b) { return dot(a,b)==0; } 3133 | template T angle(const Point &a, const Point &b) { 3134 | T cosTheta = dot(a,b) / abs(a) / abs(b); 3135 | return acos(max(-1.0, min(1.0, cosTheta))); 3136 | } 3137 | template bool orient(const Point &a, const Point &b, const Point &c) { return cross(b-a, c-a); } 3138 | template ld areaPoly(const vector> &v) { 3139 | ld area = 0; 3140 | for(int i=0,n=v.size();i struct Line { 3145 | Point v; T c; 3146 | Line(const Point &v, T c): v(v), c(c) {} 3147 | Line(T a, T b, T c): v({b,-a}), c(c) {} // ax+by=c 3148 | Line(const Point &a, const Point &b): v(b-a), c(cross(v,a)) {} // between a and b 3149 | bool operator()(const Point &a, const Point &b) { return dot(v,a) &p) { return cross(v,p)-c; } 3151 | Line translate(Point &p) { return {v, c+cross(v,p)}; } 3152 | Line shiftLeft(double dist) { return {v, c+dist*abs(v)}; } 3153 | Point proj(Point &p) { return p - perp(v)*side(p)/sq(v); } 3154 | Point reflect(Point &p) { return p - perp(v)*2*side(p)/sq(v); } 3155 | }; 3156 | template pair> lineInter(Line &a, Line &b) { 3157 | T d = cross(a.v, b.v); 3158 | if(d==0) return {0, {0,0}}; 3159 | Point res = (b.v*a.c - a.v*b.c)*1.0L/d; 3160 | return {1, res}; 3161 | } 3162 | // Geometry (self-made) end 3163 | 3164 | // Randomizer start 3165 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 3166 | uniform_int_distribution(1,6)(rng); 3167 | uniform_int_distribution<> dis(1,6); 3168 | 3169 | Examples: 3170 | cout< mp; 3177 | struct custom_hash { 3178 | static uint64_t splitmix64(uint64_t x) { 3179 | // http://xorshift.di.unimi.it/splitmix64.c 3180 | x += 0x9e3779b97f4a7c15; 3181 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 3182 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 3183 | return x ^ (x >> 31); 3184 | } 3185 | 3186 | size_t operator()(uint64_t x) const { 3187 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 3188 | return splitmix64(x + FIXED_RANDOM); 3189 | } 3190 | }; 3191 | // Hash map custom hash end 3192 | 3193 | //Binary converter start 3194 | string bconv(ll x) 3195 | { 3196 | string res; 3197 | for(int i=8;i>=0;i--){ 3198 | if((1LL<=n || y>=m; 3212 | } 3213 | 3214 | bool vst[MAXN][MAXN]; 3215 | 3216 | forn(k,0,4){ 3217 | int x1=x+dx[k], y1=y+dy[k]; 3218 | if(oob(x1,y1)) continue; 3219 | 3220 | 3221 | } 3222 | 3223 | void dfs(int x, int y) 3224 | { 3225 | if(vst[x][y]) return; 3226 | vst[x][y]=1; 3227 | forn(k,0,4){ 3228 | int x1=x+dx[k], y1=y+dy[k]; 3229 | if(oob(x1,y1)) continue; 3230 | if(vst[x1][y1]) continue; 3231 | 3232 | dfs(x1,y1); 3233 | } 3234 | } 3235 | //Grid movement (4-direction) end 3236 | 3237 | //Grid movement (8-direction) start 3238 | const int dx[]={-1,0,1,-1,1,-1,0,1}; 3239 | const int dy[]={-1,-1,-1,0,0,1,1,1}; 3240 | //Grid movement (8-direction) end 3241 | 3242 | //Nearest/Closest pair of points start 3243 | struct Point{ 3244 | ll x,y,id; 3245 | }; 3246 | 3247 | ll dist(const Point &a, const Point &b){ 3248 | return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); 3249 | } 3250 | 3251 | bool cmp_x(const Point &a, const Point &b){ 3252 | return a.x Temp; 3259 | ll mindist=INF; 3260 | ii best={-1,-1}; 3261 | void rec(int l, int r, vector &a) 3262 | { 3263 | if(r-l<=3){ 3264 | for(int i=l;idist(a[i],a[j])){ 3266 | mindist=dist(a[i],a[j]); 3267 | best={a[i].id, a[j].id}; 3268 | } 3269 | } 3270 | sort(a.begin()+l, a.begin()+r, cmp_y); 3271 | return; 3272 | } 3273 | 3274 | int mid=(l+r)/2; 3275 | ll midx=a[mid].x; 3276 | rec(l,mid,a); 3277 | rec(mid,r,a); 3278 | 3279 | merge(a.begin()+l, a.begin()+mid, a.begin()+mid, a.begin()+r, Temp.begin(), cmp_y); 3280 | copy(Temp.begin(), Temp.begin()+r-l, a.begin()+l); 3281 | 3282 | int sz=0; 3283 | for(int i=l;i=0 && a[i].y-Temp[j].ydist(a[i],Temp[j])){ 3287 | mindist=dist(a[i],Temp[j]); 3288 | best={a[i].id, Temp[j].id}; 3289 | } 3290 | } 3291 | Temp[sz++]=a[i]; 3292 | } 3293 | } 3294 | } 3295 | 3296 | Temp.resize(n); 3297 | sort(a.begin(), a.end(), cmp_x); 3298 | rec(0,n,a); 3299 | //Nearest pair of points end 3300 | --------------------------------------------------------------------------------