forked from ncduy0303/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.json
2090 lines (2090 loc) · 87.7 KB
/
snippets.json
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"CP Main Template": {
"prefix": "cp",
"body": [
"#include <bits/stdc++.h>",
"",
"using namespace std;",
"",
"template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << \", \" << p.second << ')'; }",
"template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = \", \"; return os << '}'; }",
"void dbg_out() { cerr << endl; }",
"template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }",
"#ifdef LOCAL",
"#define dbg(...) cerr << \"(\" << #__VA_ARGS__ << \"):\", dbg_out(__VA_ARGS__)",
"#else",
"#define dbg(...)",
"#endif",
"",
"#define ar array",
"#define ll long long",
"#define ld long double",
"#define sza(x) ((int)x.size())",
"#define all(a) (a).begin(), (a).end()",
"",
"const int MAX_N = 1e5 + 5;",
"const ll MOD = 1e9 + 7;",
"const ll INF = 1e9;",
"const ld EPS = 1e-9;",
"",
"",
"",
"void solve() {",
" ",
"}",
"",
"int main() {",
" ios_base::sync_with_stdio(0);",
" cin.tie(0); cout.tie(0);",
" int tc = 1;",
" // cin >> tc;",
" for (int t = 1; t <= tc; t++) {",
" // cout << \"Case #\" << t << \": \";",
" solve();",
" }",
"}"
],
"description": "CP Main Template"
},
"Debugger Template": {
"prefix": "debug",
"body": [
"template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << \", \" << p.second << ')'; }",
"template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = \", \"; return os << '}'; }",
"void dbg_out() { cerr << endl; }",
"template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }",
"#ifdef LOCAL",
"#define dbg(...) cerr << \"(\" << #__VA_ARGS__ << \"):\", dbg_out(__VA_ARGS__)",
"#else",
"#define dbg(...)",
"#endif"
],
"description": "Debugger Template"
},
"Randomiser": {
"prefix": "rand",
"body": [
"mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());",
"ll rand(ll l, ll r) {return uniform_int_distribution(l, r)(rng);}"
],
"description": "Randomiser"
},
"Atcoder Library": {
"prefix": "atcoder",
"body": [
"#include <atcoder/all>",
"using namespace atcoder;"
],
"description": "Atcoder Library"
},
"Fast OI": {
"prefix": "fastoi",
"body": [
"inline int readInt() {",
" int x = 0;",
" char ch = getchar_unlocked();",
" while (ch < '0' || ch > '9') ch = getchar_unlocked();",
" while ('0' <= ch && ch <= '9') {",
" x = (x << 3) + (x << 1) + ch - '0';",
" ch = getchar_unlocked();",
" }",
" return x;",
"}"
],
"description": "Fast OI"
},
"File input/output": {
"prefix": "open_file",
"body": [
"void open_file(string filename) {",
" freopen((filename + \".in\").c_str(), \"r\", stdin);",
" freopen((filename + \".out\").c_str(), \"w\", stdout);",
"}"
],
"description": "File input/output"
},
"lambda function": {
"prefix": "lambda",
"body": [
"auto ok = [&](int x) -> bool {",
"",
"};"
],
"description": "lambda function"
},
"Complex": {
"prefix": "complex",
"body": [
"#define P complex<double>",
"#define X real()",
"#define Y imag()",
"inline istream& operator>>(istream &in, P &p) {double x, y; in >> x >> y, p = P(x, y); return in;}"
],
"description": "Complex"
},
"int128": {
"prefix": "int128",
"body": [
"__int128 read() {",
" __int128 x = 0, f = 1;",
" char ch = getchar();",
" while (ch < '0' || ch > '9') {",
" if (ch == '-') f = -1;",
" ch = getchar();",
" }",
" while (ch >= '0' && ch <= '9') {",
" x = x * 10 + ch - '0';",
" ch = getchar();",
" }",
" return x * f;",
"}",
"",
"void print(__int128 x) {",
" if (x == 0) {",
" cout << 0;",
" return;",
" } ",
" vector<int> tmp;",
" while (x) {",
" tmp.push_back(x % 10);",
" x /= 10;",
" }",
" for (int x : tmp) cout << x;",
"}"
],
"description": "int128"
},
"Disjoint Set Union": {
"prefix": "dsu",
"body": [
"struct dsu {",
" vector<int> p, sz;",
" dsu(int n) {",
" p.resize(n); iota(p.begin(), p.end(), 0);",
" sz.assign(n, 1);",
" }",
" int find(int u) {return u == p[u] ? u : p[u] = find(p[u]);}",
" bool same(int u, int v) {return find(u) == find(v);}",
" bool merge(int u, int v) {",
" u = find(u), v = find(v);",
" if (u == v) return false;",
" if (sz[u] > sz[v]) swap(u, v);",
" sz[v] += sz[u];",
" p[u] = v;",
" return true;",
" }",
"};"
],
"description": "Disjoint Set Union"
},
"dsu (with rollback)": {
"prefix": "dsu_persistent",
"body": [
"struct dsu { ",
" int num;",
" vector<int> p, sz;",
" stack<int> st;",
" void init(int n) {",
" num = n;",
" p.resize(n); iota(p.begin(), p.end(), 0);",
" sz.assign(n, 1);",
" }",
" int find(int u) {return u == p[u] ? u : find(p[u]);}",
" bool same(int u, int v) {return find(u) == find(v);}",
" bool merge(int u, int v) {",
" u = find(u), v = find(v);",
" if (u == v) return false;",
" if (sz[u] > sz[v]) swap(u, v);",
" num--;",
" sz[v] += sz[u];",
" p[u] = v;",
" st.push(u);",
" return true;",
" }",
" void rollback(int t) {",
" while (st.size() > t) {",
" int u = st.top(); st.pop();",
" num++;",
" sz[p[u]] -= sz[u];",
" p[u] = u;",
" }",
" }",
"};"
],
"description": "Persistent dsu (with rollback)"
},
"Persistent Queue": {
"prefix": "queue_persistent",
"body": [
"struct persistent_queue {",
" const int MAX_L = 20;",
" int ver;",
" vector<int> back_id, sz, data;",
" vector<vector<int>> par;",
" persistent_queue(): ver(), data(1), par(1, vector<int>(MAX_L)), back_id(1), sz(1) {}",
" void push(int t, int x) {",
" ver++;",
" int u = data.size();",
" data.push_back(x);",
" par.push_back(vector<int>(MAX_L, back_id[t]));",
" back_id.push_back(u);",
" sz.push_back(sz[t] + 1);",
" for (int i = 1; i < MAX_L; i++) par[u][i] = par[par[u][i - 1]][i - 1];",
" }",
" int pop(int t) {",
" ver++;",
" int u = back_id[t], len = sz[t] - 1;",
" back_id.push_back(u);",
" sz.push_back(len);",
" for (int i = 0; i < MAX_L; i++) ",
" if (len & (1 << i)) ",
" u = par[u][i];",
" return data[u];",
" }",
"};"
],
"description": "Persistent Queue"
},
"Trie": {
"prefix": "trie",
"body": [
"const int MAX_S = 2e3 + 5;",
"const int MAX_C = 30;",
"",
"template<int NS, int NC> struct Trie {",
" int tr[NS][NC] = {}, cnt[NS] = {}, num = 0;",
" void insert(string s) {",
" int u = 0;",
" for (char c : s) {",
" if (!tr[u][c - 'A']) tr[u][c - 'A'] = ++num;",
" u = tr[u][c - 'A'];",
" }",
" cnt[u]++;",
" }",
" bool search(string s) { ",
" int u = 0;",
" for (char c : s) {",
" if (!tr[u][c - 'A']) return false;",
" u = tr[u][c - 'A'];",
" }",
" return cnt[u] > 0;",
" }",
"};"
],
"description": "Trie"
},
"Sparse Table": {
"prefix": "sparse_table",
"body": [
"struct sparse_table {",
" vector<vector<int>> dp;",
" sparse_table(vector<int> &arr) {",
" int n = arr.size(), k = __lg(n) + 1;",
" dp.resize(k);",
" dp[0] = arr;",
" for (int j = 1; j < k; j++)",
" for (int i = 0; i + (1 << j) <= n; i++)",
" dp[j].push_back(min(dp[j - 1][i], dp[j - 1][i + (1 << (j - 1))]));",
" }",
" int query(int l, int r) {",
" int k = __lg(r - l + 1);",
" return min(dp[k][l], dp[k][r - (1 << k) + 1]);",
" }",
"};"
],
"description": "Sparse Table"
},
"Segment Tree (range sum, range set)": {
"prefix": "segtree",
"body": [
"struct segtree { ",
" struct tdata {",
" ll sum, setval;",
" tdata(): sum(), setval(INF) {}",
" tdata(ll val): sum(val), setval(INF) {}",
" tdata(tdata l, tdata r): sum(l.sum + r.sum), setval(INF) {}",
" };",
" int ln(int node) {return 2 * node;}",
" int rn(int node) {return 2 * node + 1;}",
" int n; vector<tdata> st;",
" segtree(int _n): n(_n), st(4 * _n) {}",
" segtree(vector<int> &arr) : segtree(arr.size()) { build(arr, 1, 0, n - 1); }",
" void apply(int node, int start, int end, ll val) {",
" if (val == INF) return;",
" st[node].sum = val * (end - start + 1);",
" st[node].setval = val;",
" }",
" void pull_up(int node) {",
" st[node].sum = st[ln(node)].sum + st[rn(node)].sum;",
" }",
" void push_down(int node, int start, int end) {",
" if (start == end) return;",
" int mid = (start + end) / 2;",
" apply(ln(node), start, mid, st[node].setval);",
" apply(rn(node), mid + 1, end, st[node].setval);",
" st[node].setval = INF;",
" }",
" void build(vector<int> &arr, int node, int start, int end) {",
" if (start == end) {",
" st[node] = tdata(arr[start]);",
" return;",
" }",
" int mid = (start + end) / 2;",
" build(arr, ln(node), start, mid);",
" build(arr, rn(node), mid + 1, end);",
" pull_up(node);",
" }",
" void update(int node, int start, int end, int idx, ll val) {",
" if (start == end) {",
" apply(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" if (idx <= mid) update(ln(node), start, mid, idx, val);",
" else update(rn(node), mid + 1, end, idx, val);",
" pull_up(node);",
" }",
" void update(int node, int start, int end, int l, int r, ll val) {",
" if (r < start || end < l) return;",
" if (l <= start && end <= r) {",
" apply(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" update(ln(node), start, mid, l, r, val);",
" update(rn(node), mid + 1, end, l, r, val);",
" pull_up(node);",
" }",
" tdata query(int node, int start, int end, int l, int r) {",
" if (r < start || end < l) return tdata();",
" if (l <= start && end <= r) return st[node];",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" return tdata(query(ln(node), start, mid, l, r), query(rn(node), mid + 1, end, l, r));",
" }",
" void update(int idx, ll val) { update(1, 0, n - 1, idx, val); }",
" void update(int l, int r, ll val) { update(1, 0, n - 1, l, r, val); }",
" tdata query(int l, int r) { return query(1, 0, n - 1, l, r); }",
"};"
],
"description": "Segment Tree (range sum, range set)"
},
"Dynamic Segment Tree (range sum, range set)": {
"prefix": "segtree_dynamic",
"body": [
"struct segtree { ",
" struct tdata {",
" tdata *ln = NULL, *rn = NULL;",
" ll start, end, sum, setval;",
" tdata(): start(), end(), sum(), setval(INF) {}",
" tdata(int start, int end, ll val): start(start), end(end), sum(val), setval(INF) {}",
" tdata(tdata l, tdata r): start(l.start), end(r.end), sum(l.sum + r.sum), setval(INF) {}",
" void init() {",
" if (ln == NULL || rn = NULL) {",
" int mid = (start + end) / 2;",
" ln = new tdata(start, mid, 0), rn = new tdata(mid + 1, end, 0);",
" }",
" }",
" };",
" int n; tdata *st;",
" segtree(int _n): n(_n) { st = new tdata(0, _n - 1, 0); }",
" void apply(tdata *node, ll val) {",
" if (val == INF) return;",
" node->sum = val * (node->end - node->start + 1);",
" node->setval = val;",
" }",
" void pull_up(tdata *node) {",
" node->sum = node->ln->sum + node->rn->sum;",
" }",
" void push_down(tdata *node) {",
" if (node->start == node->end) return;",
" node->init();",
" apply(node->ln, node->setval);",
" apply(node->rn, node->setval);",
" node->setval = INF;",
" }",
" void update(tdata *node, int idx, ll val) {",
" if (node->start == node->end) {",
" apply(node, val);",
" return;",
" }",
" push_down(node);",
" node->init();",
" int mid = (node->start + node->end) / 2;",
" if (idx <= mid) update(node->ln, idx, val);",
" else update(node->rn, idx, val);",
" pull_up(node);",
" }",
" void update(tdata *node, int l, int r, ll val) {",
" if (r < node->start || node->end < l) return;",
" if (l <= node->start && node->end <= r) {",
" apply(node, val);",
" return;",
" }",
" push_down(node);",
" node->init();",
" update(node->ln, l, r, val);",
" update(node->rn, l, r, val);",
" pull_up(node);",
" }",
" tdata query(tdata *node, int l, int r) {",
" if (r < node->start || node->end < l) return tdata();",
" if (l <= node->start && node->end <= r) return *node;",
" push_down(node);",
" node->init();",
" return tdata(query(node->ln, l, r), query(node->rn, l, r));",
" }",
" void update(int idx, ll val) { update(st, idx, val); }",
" void update(int l, int r, ll val) { update(st, l, r, val); }",
" tdata query(int l, int r) { return query(st, l, r); }",
"};"
],
"description": "Dynamic Segment Tree (range sum, range set)"
},
"Persistent Segment Tree (range sum, range set, copy)": {
"prefix": "segtree_persistent",
"body": [
"struct segtree { ",
" struct tdata {",
" tdata *ln = NULL, *rn = NULL;",
" ll sum, setval;",
" tdata(): sum(), setval(INF) {}",
" tdata(ll val): sum(val), setval(INF) {}",
" tdata(tdata *ln, tdata *rn, ll val): ln(ln), rn(rn), sum(val), setval(INF) {}",
" tdata(tdata l, tdata r): sum(l.sum + r.sum), setval(INF) {}",
" void init() {",
" if (ln == NULL || rn == NULL) {",
" ln = new tdata(), rn = new tdata();",
" }",
" }",
" };",
" int n, ver; vector<tdata*> st;",
" segtree(int _n, int q): n(_n), ver(), st(q) { st[0] = new tdata(); }",
" segtree(vector<int> &arr, int q): segtree(arr.size(), q) { build(arr, st[0], 0, n - 1); }",
" void apply(tdata *node, int start, int end, ll val) {",
" node->sum = val * (end - start + 1);",
" node->setval = val;",
" }",
" void pull_up(tdata *node) {",
" node->sum = node->ln->sum + node->rn->sum;",
" }",
" void push_down(tdata *node, int start, int end) {",
" if (start == end || node->setval == INF) return;",
" node->init();",
" int mid = (start + end) / 2;",
" apply(node->ln, start, mid, node->setval);",
" apply(node->rn, mid + 1, end, node->setval);",
" node->setval = INF;",
" }",
" void build(vector<int> &arr, tdata *node, int start, int end) {",
" if (start == end) {",
" *node = tdata(arr[start]);",
" return;",
" }",
" node->init();",
" int mid = (start + end) / 2;",
" build(arr, node->ln, start, mid);",
" build(arr, node->rn, mid + 1, end);",
" pull_up(node);",
" }",
" void update(tdata *node, int start, int end, int idx, ll val) {",
" if (start == end) {",
" apply(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" if (idx <= mid) {",
" node->ln = new tdata(node->ln->ln, node->ln->rn, node->ln->sum);",
" update(node->ln, start, mid, idx, val);",
" }",
" else {",
" node->rn = new tdata(node->rn->ln, node->rn->rn, node->rn->sum);",
" update(node->rn, mid + 1, end, idx, val);",
" }",
" pull_up(node);",
" }",
" void update(tdata *node, int start, int end, int l, int r, ll val) {",
" if (r < start || end < l) return;",
" if (l <= start && end <= r) {",
" apply(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" node->ln = new tdata(node->ln->ln, node->ln->rn, node->ln->sum);",
" node->rn = new tdata(node->rn->ln, node->rn->rn, node->rn->sum);",
" update(node->ln, start, mid, l, r, val);",
" update(node->rn, mid + 1, end, l, r, val);",
" pull_up(node);",
" }",
" tdata query(tdata *node, int start, int end, int l, int r) {",
" if (r < start || end < l) return tdata();",
" if (l <= start && end <= r) return *node;",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" return tdata(query(node->ln, start, mid, l, r), query(node->rn, mid + 1, end, l, r));",
" }",
" void update(int k, int idx, ll val) { update(st[k], 0, n - 1, idx, val); }",
" void update(int k, int l, int r, ll val) { update(st[k], 0, n - 1, l, r, val); }",
" tdata query(int k, int l, int r) { return query(st[k], 0, n - 1, l, r); }",
" void copy(int k) { st[++ver] = new tdata(st[k]->ln, st[k]->rn, st[k]->sum); }",
"};"
],
"description": "Persistent Segment Tree (range sum, range set, copy)"
},
"Segment Tree Beats": {
"prefix": "segtreebeats",
"body": [
"struct segtree { ",
" struct tdata {",
" ll sum, addval, mx, mx2, mx_cnt, mn, mn2, mn_cnt;",
" tdata(): sum(), addval(), mx(-INF), mx2(-INF), mx_cnt(), mn(INF), mn2(INF), mn_cnt() {}",
" tdata(ll val): sum(val), addval(), mx(val), mx2(-INF), mx_cnt(1), mn(val), mn2(INF), mn_cnt(1) {}",
" tdata(tdata l, tdata r) {",
" sum = l.sum + r.sum;",
"",
" mx = max(l.mx, r.mx);",
" mx2 = max(l.mx2, r.mx2);",
" mx_cnt = 0;",
" if (l.mx == mx) mx_cnt += l.mx_cnt;",
" else mx2 = max(mx2, l.mx);",
" if (r.mx == mx) mx_cnt += r.mx_cnt;",
" else mx2 = max(mx2, r.mx);",
"",
" mn = min(l.mn, r.mn);",
" mn2 = min(l.mn2, r.mn2);",
" mn_cnt = 0;",
" if (l.mn == mn) mn_cnt += l.mn_cnt;",
" else mn2 = min(mn2, l.mn);",
" if (r.mn == mn) mn_cnt += r.mn_cnt;",
" else mn2 = min(mn2, r.mn);",
" }",
" };",
" int ln(int node) {return 2 * node;}",
" int rn(int node) {return 2 * node + 1;}",
" int n; vector<tdata> st;",
" segtree(int _n): n(_n), st(4 * _n) {}",
" segtree(vector<ll> &arr) : segtree(arr.size()) { build(arr, 1, 0, n - 1); }",
" void apply_add(int node, int start, int end, ll val) {",
" if (val == 0) return;",
" st[node].sum += val * (end - start + 1);",
" st[node].addval += val;",
"",
" st[node].mx += val;",
" if (st[node].mx2 != -INF) st[node].mx2 += val; ",
"",
" st[node].mn += val;",
" if (st[node].mn2 != INF) st[node].mn2 += val; ",
" }",
" void apply_min(int node, int start, int end, ll val) {",
" if (val >= st[node].mx) return;",
" st[node].sum -= (st[node].mx - val) * st[node].mx_cnt;",
" st[node].mx = val;",
"",
" if (val < st[node].mn) st[node].mn = val;",
" else if (val < st[node].mn2) st[node].mn2 = val;",
" }",
" void apply_max(int node, int start, int end, ll val) {",
" if (val <= st[node].mn) return;",
" st[node].sum += (val - st[node].mn) * st[node].mn_cnt;",
" st[node].mn = val;",
"",
" if (val > st[node].mx) st[node].mx = val;",
" else if (val > st[node].mx2) st[node].mx2 = val;",
" }",
" void pull_up(int node) {",
" st[node].sum = st[ln(node)].sum + st[rn(node)].sum;",
"",
" st[node].mx = max(st[ln(node)].mx, st[rn(node)].mx);",
" st[node].mx2 = max(st[ln(node)].mx2, st[rn(node)].mx2);",
" st[node].mx_cnt = 0;",
" if (st[ln(node)].mx == st[node].mx) st[node].mx_cnt += st[ln(node)].mx_cnt;",
" else st[node].mx2 = max(st[node].mx2, st[ln(node)].mx);",
" if (st[rn(node)].mx == st[node].mx) st[node].mx_cnt += st[rn(node)].mx_cnt;",
" else st[node].mx2 = max(st[node].mx2, st[rn(node)].mx);",
"",
" st[node].mn = min(st[ln(node)].mn, st[rn(node)].mn);",
" st[node].mn2 = min(st[ln(node)].mn2, st[rn(node)].mn2);",
" st[node].mn_cnt = 0;",
" if (st[ln(node)].mn == st[node].mn) st[node].mn_cnt += st[ln(node)].mn_cnt;",
" else st[node].mn2 = min(st[node].mn2, st[ln(node)].mn);",
" if (st[rn(node)].mn == st[node].mn) st[node].mn_cnt += st[rn(node)].mn_cnt;",
" else st[node].mn2 = min(st[node].mn2, st[rn(node)].mn);",
" }",
" void push_down(int node, int start, int end) {",
" if (start == end) return;",
" int mid = (start + end) / 2;",
" apply_add(ln(node), start, mid, st[node].addval);",
" apply_add(rn(node), mid + 1, end, st[node].addval);",
" st[node].addval = 0;",
" apply_min(ln(node), start, mid, st[node].mx);",
" apply_min(rn(node), mid + 1, end, st[node].mx);",
" apply_max(ln(node), start, mid, st[node].mn);",
" apply_max(rn(node), mid + 1, end, st[node].mn);",
" }",
" void build(vector<ll> &arr, int node, int start, int end) {",
" if (start == end) {",
" st[node] = tdata(arr[start]);",
" return;",
" }",
" int mid = (start + end) / 2;",
" build(arr, ln(node), start, mid);",
" build(arr, rn(node), mid + 1, end);",
" pull_up(node);",
" }",
" void update_add(int node, int start, int end, int l, int r, ll val) {",
" push_down(node, start, end);",
" if (r < start || end < l) return;",
" if (l <= start && end <= r) {",
" apply_add(node, start, end, val);",
" return;",
" }",
" int mid = (start + end) / 2;",
" update_add(ln(node), start, mid, l, r, val);",
" update_add(rn(node), mid + 1, end, l, r, val);",
" pull_up(node);",
" }",
" void update_min(int node, int start, int end, int l, int r, ll val) {",
" if (r < start || end < l || st[node].mx <= val) return;",
" if (l <= start && end <= r && st[node].mx2 < val) {",
" apply_min(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" update_min(ln(node), start, mid, l, r, val);",
" update_min(rn(node), mid + 1, end, l, r, val);",
" pull_up(node);",
" }",
" void update_max(int node, int start, int end, int l, int r, ll val) {",
" if (r < start || end < l || st[node].mn >= val) return;",
" if (l <= start && end <= r && st[node].mn2 > val) {",
" apply_max(node, start, end, val);",
" return;",
" }",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" update_max(ln(node), start, mid, l, r, val);",
" update_max(rn(node), mid + 1, end, l, r, val);",
" pull_up(node);",
" }",
" tdata query(int node, int start, int end, int l, int r) {",
" if (r < start || end < l) return tdata();",
" if (l <= start && end <= r) return st[node];",
" push_down(node, start, end);",
" int mid = (start + end) / 2;",
" return tdata(query(ln(node), start, mid, l, r), query(rn(node), mid + 1, end, l, r));",
" }",
" void update_add(int l, int r, ll val) { update_add(1, 0, n - 1, l, r, val); }",
" void update_min(int l, int r, ll val) { update_min(1, 0, n - 1, l, r, val); }",
" void update_max(int l, int r, ll val) { update_max(1, 0, n - 1, l, r, val); }",
" tdata query(int l, int r) { return query(1, 0, n - 1, l, r); }",
"};"
],
"description": "Segment Tree Beats"
},
"Fenwick Tree (PURQ && RUPQ && OST)": {
"prefix": "fwtree",
"body": [
"struct fwtree {",
" const int MAX_L = 20;",
" int n; vector<ll> ft;",
" fwtree(int _n): n(_n), ft(_n + 1) {}",
" void update(int x, ll v) {",
" for (; x <= n; x += x&-x) ft[x] += v;",
" }",
" void update(int l, int r, ll v) {",
" update(l, v);",
" update(r + 1, -v);",
" }",
" ll sum(int x) {",
" ll res = 0;",
" for (; x; x -= x&-x) res += ft[x];",
" return res;",
" }",
" ll query(int l, int r) {return sum(r) - sum(l - 1);}",
" int find_by_order(int k) { // return 1 if k < 1 and returns N if k > N",
" int sum = 0, pos = 0;",
" for (int i = MAX_L; i >= 0; i--) {",
" if (pos + (1 << i) < n && sum + ft[pos + (1 << i)] < k) {",
" sum += ft[pos + (1 << i)];",
" pos += (1 << i);",
" }",
" }",
" return pos + 1; ",
" }",
" int order_of_key(int k) {return sum(k);}",
"};"
],
"description": "Fenwick Tree (PURQ && RUPQ && OST)"
},
"Fenwick Tree (RURQ)": {
"prefix": "fwtree_RURQ",
"body": [
"struct fwtree {",
" int n; vector<ll> ft1, ft2;",
" fwtree(int _n): n(_n), ft1(_n + 1), ft2(_n + 1) {}",
" void update(vector<ll> &ft, int x, ll v) {",
" for (; x <= n; x += x&-x) ft[x] += v;",
" }",
" void update(int l, int r, ll v) {",
" update(ft1, l, v);",
" update(ft1, r + 1, -v);",
" update(ft2, l, v * (l - 1));",
" update(ft2, r + 1, -v * r);",
" }",
" ll sum(vector<ll> &ft, int x) {",
" ll res = 0;",
" for (; x; x -= x&-x) res += ft[x];",
" return res;",
" }",
" ll sum(int x) {return sum(ft1, x) * x - sum(ft2, x);}",
" ll query(int l, int r) {return sum(r) - sum(l - 1);}",
"};"
],
"description": "Fenwick Tree (RURQ)"
},
"Fenwick Tree (2D)": {
"prefix": "fwtree2d",
"body": [
"struct fwtree {",
" int n; vector<vector<ll>> ft;",
" fwtree(int _n): n(_n), ft(_n + 1, vector<ll>(_n + 1)) {}",
" void update(int x, int y, ll v) {",
" for (int tx = x; tx <= n; tx += tx&-tx)",
" for (int ty = y; ty <= n; ty += ty&-ty)",
" ft[tx][ty] += v;",
" }",
" ll sum(int x, int y) {",
" ll res = 0;",
" for (int tx = x; tx; tx -= tx&-tx) ",
" for (int ty = y; ty; ty -= ty&-ty)",
" res += ft[tx][ty];",
" return res;",
" }",
" ll query(int x1, int y1, int x2, int y2) {return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1);}",
"};"
],
"description": "Fenwick Tree (2D)"
},
"Policy-based data structure": {
"prefix": "pbds",
"body": [
"#include <ext/pb_ds/assoc_container.hpp>",
"#include <ext/pb_ds/tree_policy.hpp>",
"using namespace __gnu_pbds;",
"template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;",
"template <class K, class V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;"
],
"description": "Policy-based data structure"
},
"nCk (no MOD)": {
"prefix": "nCk_ll",
"body": [
"ll nCk(int n, int k) {",
" if (k < 0 || k > n) return 0;",
" k = min(k, n - k);",
" ll res = 1, tmp = 1;",
" while (k) {",
" res *= n; tmp *= k;",
" ll d = gcd(res, tmp);",
" res /= d; tmp /= d;",
" n--; k--;",
" }",
" return res;",
"}"
],
"description": "nCk (no MOD)"
},
"Binomial Coefficients": {
"prefix": "nCk",
"body": [
"ll qexp(ll a, ll b, ll m) {",
" ll res = 1;",
" while (b) {",
" if (b % 2) res = res * a % m;",
" a = a * a % m;",
" b /= 2;",
" }",
" return res;",
"}",
"",
"vector<ll> fact, invf;",
"",
"void precompute(int n) {",
" fact.assign(n + 1, 1); ",
" for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % MOD;",
" invf.assign(n + 1, 1);",
" invf[n] = qexp(fact[n], MOD - 2, MOD);",
" for (int i = n - 1; i > 0; i--) invf[i] = invf[i + 1] * (i + 1) % MOD;",
"}",
"",
"ll nCk(int n, int k) {",
" if (k < 0 || k > n) return 0;",
" return fact[n] * invf[k] % MOD * invf[n - k] % MOD;",
" // return fact[n] * qexp(fact[k], MOD - 2, MOD) % MOD * qexp(fact[n - k], MOD - 2, MOD) % MOD;",
"}"
],
"description": "Binomial Coefficients"
},
"Matrix": {
"prefix": "matrix",
"body": [
"template <class T> struct matrix {",
" vector<vector<T>> m;",
" int r, c;",
" matrix() : r(), c() {}",
" matrix(int r, int c, T x) : r(r), c(c), m(r, vector<T>(c, x)) {}",
" matrix(int n) : matrix(n, n, 0) { // identity matrix",
" for (int i = 0; i < n; i++) m[i][i] = 1;",
" }",
" matrix operator+ (matrix<T> b) {",
" matrix<T> a = *this;",
" assert(a.r == b.r); ",
" assert(a.c == b.c);",
" matrix<T> o(a.r, a.c, 0);",
" for (int i = 0; i < a.r; i++)",
" for (int j = 0; j < a.c; j++)",
" o.m[i][j] = a.m[i][j] + b.m[i][j];",
" return o;",
" }",
" matrix operator- (matrix<T> b) {",
" matrix<T> a = *this;",
" assert(a.r == b.r); ",
" assert(a.c == b.c);",
" matrix<T> o(a.r, a.c, 0);",
" for (int i = 0; i < a.r; i++)",
" for (int j = 0; j < a.c; j++)",
" o.m[i][j] = a.m[i][j] - b.m[i][j];",
" return o;",
" }",
" matrix operator* (matrix<T> b) {",
" matrix<T> a = *this;",
" assert(a.c == b.r);",
" matrix<T> o(a.r, b.c, 0);",
" for (int i = 0; i < a.r; i++)",
" for (int j = 0; j < b.c; j++)",
" for (int k = 0; k < a.c; k++)",
" o.m[i][j] = o.m[i][j] + a.m[i][k] * b.m[k][j];",
" return o;",
" }",
" matrix operator^ (ll b) {",
" matrix<T> a = *this;",
" assert(a.r == a.c);",
" matrix<T> o(a.r);",
" while (b) {",
" if (b % 2) o = o * a;",
" a = a * a;",
" b /= 2;",
" }",
" return o;",
" }",
" void print() {",
" for (int i = 0; i < r; i++) {",
" for (int j = 0; j < c; j++) cout << m[i][j] << \" \";",
" cout << \"\\n\";",
" }",
" }",
"};"
],
"description": "Matrix"
},
"Quick Exponention": {
"prefix": "qexp",
"body": [
"ll qexp(ll a, ll b, ll m) {",
" ll res = 1;",
" while (b) {",
" if (b % 2) res = res * a % m;",
" a = a * a % m;",
" b /= 2;",
" }",
" return res;",
"}"
],
"description": "Quick Exponention"
},
"Max Flow Algorithm": {
"prefix": "max_flow",
"body": [
"struct max_flow_graph {",
" struct edge {",
" int u, v, cap, flow;",
" };",
" int n; ",
" vector<edge> el; ",
" vector<vector<int>> adj;",
" vector<int> dist, par, vis;",
" max_flow_graph(int _n) : n(_n), adj(_n) {}",
" void add_edge(int u, int v, int w) {",
" adj[u].push_back(el.size());",
" el.push_back({u, v, w, 0});",
" adj[v].push_back(el.size());",
" el.push_back({v, u, 0, 0}); ",
" }",
" int send_one_flow(int s, int e) {",
" int nf = INF;",
" for (int u = e; u != s; u = el[par[u]].u) {",
" nf = min(nf, el[par[u]].cap - el[par[u]].flow);",
" }",
" for (int u = e; u != s; u = el[par[u]].u) {",
" el[par[u]].flow += nf;",
" el[par[u]^1].flow -= nf;",
" }",
" return nf;",
" }",
" bool bfs(int s, int e) {",
" dist.assign(n, INF);",
" par.assign(n, 0);",
" queue<int> q;",
" q.push(s); dist[s] = 0;",
" while (q.size()) {",
" int u = q.front(); q.pop();",
" if (u == e) break;",
" for (int idx : adj[u]) {",
" if (el[idx].cap > el[idx].flow && dist[el[idx].v] > dist[el[idx].u] + 1) {",
" dist[el[idx].v] = dist[el[idx].u] + 1;",
" par[el[idx].v] = idx;",
" q.push(el[idx].v);",
" }",
" }",
" }",
" return dist[e] < INF;",
" }",
" int dfs(int s, int e, int f = INF) {",
" if (s == e || f == 0) return f;",
" for (int idx : adj[s]) {",
" if (dist[el[idx].v] != dist[s] + 1) continue;",
" if (int nf = dfs(el[idx].v, e, min(f, el[idx].cap - el[idx].flow))) {",
" el[idx].flow += nf;",
" el[idx^1].flow -= nf;",
" return nf;",
" }",
" }",
" return 0;",
" }",
" ll edmonds_karp(int s, int e) {",
" ll mf = 0;",
" while (bfs(s, e)) mf += send_one_flow(s, e);",
" return mf;",
" }",
" ll dinic(int s, int e) {",
" ll mf = 0;",
" while (bfs(s, e)) {",
" while (int nf = dfs(s, e)) mf += nf;",
" }",
" return mf;",
" }",
" void dfs_cut(int u) {",
" vis[u] = 1;",
" for (int idx : adj[u]) {",
" auto [_, v, cap, flow] = el[idx];",
" if (vis[v] || flow >= cap) continue;",
" dfs_cut(v);",
" }",
" }",
" vector<ar<int,2>> min_cut(int s, int e) {",
" ll mf = dinic(s, e);",
" vis.assign(n, 0);",
" dfs_cut(s);",
" vector<ar<int,2>> res;",
" for (auto [u, v, cap, flow] : el) {",
" if (vis[u] != vis[v] && cap == flow) {",
" res.push_back({u, v});",
" }",
" }",
" return res;",
" }",