-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvscode_snippet.json
More file actions
1509 lines (1508 loc) · 44.1 KB
/
vscode_snippet.json
File metadata and controls
1509 lines (1508 loc) · 44.1 KB
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
{
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
"Unionfind": {
"prefix": "UnionFind",
"body": [
"",
"const int MX_SZ=1e5+5;",
"int par[MX_SZ];",
"int sz[MX_SZ];",
"",
"int getPar(int x){ return par[x]=par[x]==x?x:getPar(par[x]);}",
"",
"void setUnion(int x,int y){",
"\tint px=getPar(x),py=getPar(y);",
"\tif(px!=py){",
"\t\tpar[px]=py;",
"\t\tsz[py]+=sz[py];",
"\t}",
"}",
""
],
"description": "UnionFind"
},
"Prime Calculation": {
"prefix": "primeSnip",
"body": [
"/// ====================================PRIME utility ==================================================",
"int sz=1e6+5;",
"bool PrimeSieve[1000005]; // 1e6+5",
"void buildSieve(){",
" for(int i=2;i<=sz;i++) PrimeSieve[i]=1;",
" PrimeSieve[0]=0;// ",
" PrimeSieve[1]=0; // 1 is neither prime nor composite ",
" for(int i=2;i<sz;i++){",
" if(PrimeSieve[i]==0) continue; // the current number itself is composite ",
" for(int j=2;j*i<sz;j++){",
" PrimeSieve[i*j]=0;",
" }",
" }",
"}",
"// add SieveOfEratosthenes",
"bool isPrime(int n){",
" for(int i=2;i*i<=n;i++){",
" if(n%i==0) return 0;",
" }",
" return 1;",
"}",
"",
"/// ====================================PRIME utility ENDS here==================================================",
""
],
"description": ""
},
"math snippet": {
"prefix": "mathSnip",
"body": [
"// ========================================MATH UTIL BEGINS==============================================",
"//==================================== compute higher powers with mod ===================================",
"uint power(int x, int y, int p = MOD)",
"{",
" unsigned long long res = 1;",
"",
" x = x % p;",
" while (y > 0)",
" {",
"",
" if (y & 1)",
" res = (res * x) % p;",
"",
" y = y >> 1;",
" x = (x * x) % p;",
" }",
" return res;",
"}",
"",
"// =============================================================================================================",
"",
"uint modInverse(int n, int p=MOD) // using fermats little thm. [p needs to be prime which is mostly the case as mod value generally is 1e9+7]",
"{",
" return power(n, p - 2, p);",
"}",
"// can also derive this using extended euclidean... however this has a much simpler code....",
"",
"",
"// =========================================Used to calculate nCr of higher values ===================================",
"uint nCr(int n, int r, int p=MOD) // faster calculation.. ",
"{",
" if (n < r)",
" return 0;",
"",
" if (r == 0)",
" return 1;",
" ",
" vector<int> fac(n+1,0);",
" fac[0] = 1;",
" for (int i = 1; i <= n; i++)",
" fac[i] = (fac[i - 1] * i) % p;",
"",
" return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;",
"}",
"// ==================================== MATH UTIL ENDS=======================================================//",
""
],
"description": ""
},
"Segment Tree": {
"prefix": "segmentTreeSnip",
"body": [
"// ====================================start of the segment tree=========================================",
"class STree{",
"private:",
" vector<int> tree;",
" int n;",
" int sum(int v, int tl, int tr, int l, int r) { ",
" if (tr <tl) return 0;",
" if(l>tr || tl>r) return 0; // invalid indices tried!",
" if (l <= tl && r >= tr) return tree[v]; // the current segment completely belongs to the answer",
" ",
" int tm = (tl + tr) / 2;",
" return sum(v*2, tl, tm, l, r) + sum(v*2+1, tm+1, tr,l, r);",
" }",
" void buildTree(vector<int>& a,int tl, int tr,int v=1){",
" if(tl>tr) return;",
" if(tl==tr){",
" tree[v]=a[tl];",
" return;",
" }",
"",
" int mid=(tl+tr)/2;",
" buildTree(a,tl,mid,v*2);",
" buildTree(a,mid+1,tr,v*2+1);",
" tree[v]=tree[v*2]+tree[v*2+1];",
" }",
" void update(int idx,int val,int v,int tl,int tr){",
" if(tl>tr) return;",
" if(idx<tl || idx>tr) return; //not in the current range",
" if(tl==tr){",
" tree[v]=val;",
" return;",
" }",
" int tmid=(tl+tr)/2;",
" update(idx,val,v*2,tl,tmid);",
" update(idx,val,v*2+1,tmid+1,tr);",
" tree[v]=tree[v*2]+tree[v*2+1];",
" }",
"public:",
" STree(int sz,vector<int>& vec){",
" n=sz;",
" tree.assign(sz*4,0);",
" buildTree(vec,0,n-1);",
" }",
"",
" int getSum(int l,int r){",
" return sum(1,0,n-1,l,r); // call the function!",
" }",
" void update(int idx,int val){ // v is the new value!",
" update(idx,val,1,0,n-1);",
" }",
" void printTree(vector<int>& v){",
" cout<<v<<\"\\n\";",
" }",
"",
"};",
"// ========================================= Segment tree ends here====================================",
"",
"",
""
],
"description": ""
}
,
"Min segment tree": {
"prefix": "mnSegmentSnip",
"body": [
"// ====================================start of the segment tree=========================================",
"class STreeMN{ ",
"private:",
" int MN=INT_MIN;",
" int MX=INT_MAX;",
" vector<int> tree;",
" int n;",
" int getMn(int v, int tl, int tr, int l, int r) { ",
" if (tr <tl) return MX;",
" if(l>tr || tl>r) return MX; // invalid indices tried!",
" if (l <= tl && r >= tr) return tree[v]; // the current segment completely belongs to the answer",
" int tm = (tl + tr) / 2;",
" return min(getMn(v*2, tl, tm, l, r) , getMn(v*2+1, tm+1, tr, l, r));",
" }",
" void buildTree(vector<int>& a,int tl, int tr,int v=1){",
" if(tl>tr) return;",
" if(tl==tr){",
" tree[v]=a[tl];",
" return;",
" }",
"",
" int mid=(tl+tr)/2;",
" buildTree(a,tl,mid,v*2);",
" buildTree(a,mid+1,tr,v*2+1);",
" tree[v]=min(tree[v*2],tree[v*2+1]);",
" }",
" void update(int idx,int val,int v,int tl,int tr){",
" if(tl>tr) return;",
" if(idx<tl || idx>tr) return; //not in the current range",
" if(tl==tr){",
" tree[v]=val;",
" return;",
" }",
" int tmid=(tl+tr)/2;",
" update(idx,val,v*2,tl,tmid);",
" update(idx,val,v*2+1,tmid+1,tr);",
" tree[v]=min(tree[v*2],tree[v*2+1]);",
" }",
"public:",
" STreeMN(int sz,vector<int>& vec){",
" n=sz;",
" tree.assign(sz*4,0);",
" buildTree(vec,0,n-1);",
" }",
"",
" int getMn(int l,int r){",
" return getMn(1,0,n-1,l,r); // call the function!",
" }",
" void update(int idx,int val){ // v is the new value!",
" update(idx,val,1,0,n-1);",
" }",
" void printTree(vector<int>& v){",
" cout<<v<<\"\\n\";",
" }",
"",
"};",
""
],
"description": ""
},
"max segment tree": {
"prefix": "mxSegmentSnip",
"body": [
" // ====================================start of the segment tree=========================================",
"class STreeMX{ ",
"private:",
" int MN=INT_MIN;",
" int MX=INT_MAX;",
" vector<int> tree;",
" int n;",
" int getMx(int v, int tl, int tr, int l, int r) { ",
" if (tr <tl) return MN;",
" if(l>tr || tl>r) return MN; // invalid indices tried!",
" if (l <= tl && r >= tr) return tree[v]; // the current segment completely belongs to the answer",
" ",
" int tm = (tl + tr) / 2;",
" return max(getMx(v*2, tl, tm, l, r) , getMx(v*2+1, tm+1, tr, l, r));",
" }",
" void buildTree(vector<int>& a,int tl, int tr,int v=1){",
" if(tl>tr) return;",
" if(tl==tr){",
" tree[v]=a[tl];",
" return;",
" }",
"",
" int mid=(tl+tr)/2;",
" buildTree(a,tl,mid,v*2);",
" buildTree(a,mid+1,tr,v*2+1);",
" tree[v]=max(tree[v*2],tree[v*2+1]);",
" }",
" void update(int idx,int val,int v,int tl,int tr){",
" if(tl>tr) return;",
" if(idx<tl || idx>tr) return; //not in the current range",
" if(tl==tr){",
" tree[v]=val;",
" return;",
" }",
" int tmid=(tl+tr)/2;",
" update(idx,val,v*2,tl,tmid);",
" update(idx,val,v*2+1,tmid+1,tr);",
" tree[v]=max(tree[v*2],tree[v*2+1]);",
" }",
"public:",
" STreeMX(int sz,vector<int>& vec){",
" n=sz;",
" tree.assign(sz*4,0);",
" buildTree(vec,0,n-1);",
" }",
"",
" int getMx(int l,int r){",
" return getMx(1,0,n-1,l,r); // call the function!",
" }",
" void update(int idx,int val){ // v is the new value!",
" update(idx,val,1,0,n-1);",
" }",
" void printTree(vector<int>& v){",
" cout<<v<<\"\\n\";",
" }",
"",
"};",
"// ========================================= Segment tree ends here====================================",
""
],
"description": ""
},
"basic template": {
"prefix": "qa",
"body": [
"#include \"bits/stdc++.h\"",
"#define int long long",
"#define uint unsigned long long",
"#define vi vector<int>",
"#define vvi vector<vi >",
"#define vb vector<bool>",
"#define vvb vector<vb >",
"#define fr(i,n) for(int i=0; i<(n); i++)",
"#define rep(i,a,n) for(int i=(a); i<=(n); i++)",
"#define nl cout<<\"\\n\"",
"#define dbg(var) cout<<#var<<\"=\"<<var<<\" \"",
"#define all(v) v.begin(),v.end()",
"#define sz(v) (int)(v.size())",
"#define srt(v) sort(v.begin(),v.end()) // sort ",
"#define mxe(v) *max_element(v.begin(),v.end()) // find max element in vector",
"#define mne(v) *min_element(v.begin(),v.end()) // find min element in vector",
"#define unq(v) v.resize(distance(v.begin(), unique(v.begin(), v.end())));",
"// make sure to sort before applying unique // else only consecutive duplicates would be removed ",
"#define bin(x,y) bitset<y>(x) ",
"using namespace std;",
"int MOD=1e9+7; // Hardcoded, directly change from here for functions!",
"",
"",
"",
"void modadd(int &a , int b) {a=((a%MOD)+(b%MOD))%MOD;}",
"void modsub(int &a , int b) {a=((a%MOD)-(b%MOD)+MOD)%MOD;}",
"void modmul(int &a , int b) {a=((a%MOD)*(b%MOD))%MOD;}",
"// ================================== take ip/op like vector,pairs directly!==================================",
"template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }",
"template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }",
"template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }",
"template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\\n'; return cout; }",
"template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }",
"// ===================================END Of the input module ==========================================",
"",
"",
"void solve(){",
" int n=1,m=0;",
" string s;",
" cin>>n;",
" vi v(n);",
" cin>>v;$0",
" ",
"}",
"",
"int32_t main()",
"{",
" ",
" ios_base::sync_with_stdio(false);",
" cin.tie(NULL);",
"",
" int T = 1;",
" cin >> T;",
" while (T--)",
" {",
" solve();",
" }",
" return 0;",
"}",
"",
" "
],
"description": ""
},
"gr-inp-wt": {
"prefix": "gr-inp-Fwt",
"body": [
"int e=${0};",
"vector<vector<pair<int,int>>> g(n+1);",
"for(int i=1;i<=e;i++){",
" int u,v,wt; cin>>u>>v>>wt;",
" g[u].push_back({v,wt});",
" g[v].push_back({u,wt});",
"}"
],
"description": "graph input weight"
},
"gr-inp": {
"prefix": "gr-inp",
"body": [
"int e=${0};",
"vector<vector<int>> g(n+1);",
"for(int i=1;i<=e;i++){",
" int u,v; cin>>u>>v;",
" g[u].push_back(v);",
" g[v].push_back(u);",
"}"
],
"description": "graph input"
},
"file_io": {
"prefix": "file_io",
"body": [
"void file_i_o(){",
" freopen(\"./tests/test01.txt\", \"r\", stdin);",
" freopen(\"./tests/output01.txt\", \"w\", stdout);",
"}"
],
"description": "for coding competetions"
},
"clock": {
"prefix": "clock for TL",
"body": [
"auto start = chrono::high_resolution_clock::now();",
"// code goes here",
"auto stop = chrono::high_resolution_clock::now();",
"auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);",
"cout << duration.count() << \" ms\\n\";",
""
],
"description": "clock"
},
"segtree": {
"prefix": "segtree",
"body": [
"template<class T, class U>",
"// T -> node, U->update.",
"struct Lsegtree{",
" vector<T>st;",
" vector<U>lazy;",
" int n;",
" T identity_element;",
" U identity_update;",
"",
" /*",
" Definition of identity_element: the element I such that combine(x,I) = x",
" for all x",
"",
" Definition of identity_update: the element I such that apply(x,I) = x",
" for all x ",
" */",
"",
" Lsegtree(int n, T identity_element, U identity_update){",
" this->n = n;",
" this->identity_element = identity_element;",
" this->identity_update = identity_update;",
" st.assign(4*n,identity_element);",
" lazy.assign(4*n, identity_update);",
" }",
"",
" T combine(T l, T r){",
" // change this function as required.",
" T ans = (l + r);",
" return ans;",
" }",
"",
" void buildUtil(int v, int tl, int tr, vector<T>&a){",
" if(tl == tr){",
" st[v] = a[tl];",
" return;",
" }",
" int tm = (tl + tr)>>1;",
" buildUtil(2*v + 1, tl, tm,a);",
" buildUtil(2*v + 2,tm+1,tr,a);",
" st[v] = combine(st[2*v + 1], st[2*v + 2]);",
" }",
"",
" // change the following 2 functions, and you're more or less done.",
" T apply(T curr, U upd, int tl, int tr){",
" T ans = (tr-tl+1)*upd;",
" // increment range by upd:",
" // T ans = curr + (tr - tl + 1)*upd",
" return ans;",
" }",
"",
" U combineUpdate(U old_upd, U new_upd, int tl, int tr){",
" U ans = old_upd;",
" ans=new_upd;",
" return ans;",
" } ",
"",
" void push_down(int v, int tl, int tr){",
" //for the below line to work, make sure the \"==\" operator is defined for U.",
" if(lazy[v] == identity_update)return;",
" st[v] = apply(st[v], lazy[v], tl, tr);",
" if(2*v + 1 <= 4*n){",
" int tm = (tl + tr)>>1;",
" lazy[2*v + 1] = combineUpdate(lazy[2*v+1], lazy[v], tl, tm);",
" lazy[2*v + 2] = combineUpdate(lazy[2*v+2], lazy[v], tm+1,tr); ",
" }",
" lazy[v] = identity_update;",
" }",
"",
" T queryUtil(int v, int tl, int tr, int l, int r){",
" push_down(v,tl,tr);",
" if(l > r)return identity_element;",
" if(tr < l or tl > r){",
" return identity_element;",
" }",
" if(l <= tl and r >= tr){",
" return st[v];",
" }",
" int tm = (tl + tr)>>1;",
" return combine(queryUtil(2*v+1,tl,tm,l,r), queryUtil(2*v+2,tm+1,tr,l,r));",
" }",
" ",
" void updateUtil(int v, int tl, int tr, int l, int r, U upd){",
" push_down(v,tl,tr); ",
" if(tr < l or tl > r)return;",
" if(tl >=l and tr <=r){",
" lazy[v] = combineUpdate(lazy[v],upd,tl,tr);",
" push_down(v,tl,tr);",
" } else{",
" int tm = (tl + tr)>>1;",
" updateUtil(2*v+1,tl,tm,l,r,upd);",
" updateUtil(2*v+2,tm+1,tr,l,r,upd);",
" st[v] = combine(st[2*v + 1], st[2*v+2]);",
" }",
" }",
"",
" void build(vector<T>a){",
" assert( (int)a.size() == n);",
" buildUtil(0,0,n-1,a);",
" }",
"",
" T query(int l, int r){",
" return queryUtil(0,0,n-1,l,r);",
" }",
"",
" void update(int l,int r, U upd){",
" updateUtil(0,0,n-1,l,r,upd);",
" }",
"};"
],
"description": "segtree"
},
"binary indexed tree": {
"prefix": "fenwick",
"body": [
"// 0-indexed BIT (binary indexed tree / Fenwick tree) (i : [0, len))",
"template <class T>",
"struct BIT{",
" int n;",
" vector<T> data;",
" BIT(int len = 0) : n(len), data(len) {}",
" void reset() { fill(data.begin(), data.end(), T(0)); }",
" void add(int pos, T v){ ",
" // a[pos] += v",
" pos++;",
" while (pos > 0 and pos <= n)",
" data[pos - 1] += v, pos += pos & -pos;",
" }",
" T sum(int k) const{ ",
" // a[0] + ... + a[k - 1]",
" T res = 0;",
" while (k > 0)",
" res += data[k - 1], k -= k & -k;",
" return res;",
" }",
"",
" T sum(int l, int r) const { return sum(r) - sum(l); } // a[l] + ... + a[r - 1]",
" // dbg functions",
" template <class OStream>",
" friend OStream &operator<<(OStream &os, const BIT &bit){",
" T prv = 0;",
" os << '[';",
" for (int i = 1; i <= bit.n; i++){",
" T now = bit.sum(i);",
" os << now - prv << ',', prv = now;",
" }",
" return os << ']';",
" }",
"};"
],
"description": "binary indexed tree"
},
"trie": {
"prefix": "trie",
"body": [
"struct Trie{",
" struct node{",
" node* next[10];",
" node(){",
" for(int i=0;i<10;i++) next[i]=NULL;",
" }",
" };",
" ",
" node root;",
" ",
" void add(vector<int>&val){",
" node* temp=&root;",
" for(auto ele : val){",
" if(temp->next[ele]==NULL) temp->next[ele]=new node();",
" temp=temp->next[ele];",
" }",
" }",
" ",
" int query(vector<int>&val){",
" node* temp=&root;",
" int ans=0;",
" for(auto ele : val){",
" if(temp->next[ele]==NULL) break;",
" ans++;",
" temp=temp->next[ele];",
" }",
" return ans;",
" }",
"};",
""
],
"description": "trie"
},
"pbds": {
"prefix": "pbds",
"body": [
"#include <ext/pb_ds/assoc_container.hpp>",
"#include <ext/pb_ds/tree_policy.hpp>",
"using namespace __gnu_pbds;",
"// pbds = find_by_value(), order_of_key()",
"// find_by_order(k) returns iterator to kth element starting from 0;",
"// order_of_key(k) returns count of elements strictly smaller than k;",
"template<class T> using minheap = priority_queue<T,vector<T>,greater<T> >; ",
"template<class T> using ordered_set = tree<T, null_type,less<T>, rb_tree_tag, tree_order_statistics_node_update> ;",
"template<class key, class value, class cmp = std::less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;"
],
"description": "pbds"
},
"xor-basis ======NEED to learn this as well!": {
"prefix": "xor-basis",
"body": [
"struct XorBasis{",
" private:",
" vector<int> basis;",
" int lg;",
" int sz = 0;",
"",
" public:",
" XorBasis(int lg) : lg(lg){",
" basis.resize(lg);",
" }",
" void add(int x){",
" if(x >= (1ll<<lg)) return;",
" for(int i=0;i<lg;i++){",
" if(~x&(1ll<<i)) continue;",
" if(!basis[i]){",
" basis[i] = x;",
" ++sz;",
" }",
" x^=basis[i];",
" }",
" }",
" bool contains(int x){",
" for(int i=0;i<lg;i++){",
" if(~x&(1ll<<i)) continue;",
" if(!basis[i]){",
" return false;",
" }",
" x^=basis[i];",
" }",
" return true;",
" }",
" int size(){",
" return sz;",
" }",
" const vector<int>::iterator begin(){",
" return basis.begin();",
" }",
" const vector<int>::iterator end(){",
" return basis.end();",
" }",
"};",
""
],
"description": "xor-basis"
},
"linear diophantine ============= NO IDEA why": {
"prefix": "diophantine",
"body": [
"long long gcd(long long a, long long b, long long &x, long long &y) {",
" if (a == 0) {",
" x = 0;",
" y = 1;",
" return b;",
" }",
" long long x1, y1;",
" long long d = gcd(b % a, a, x1, y1);",
" x = y1 - (b / a) * x1;",
" y = x1;",
" return d;",
"}",
"",
"bool find_any_solution(long long a, long long b, long long c, long long &x0,",
" long long &y0, long long &g) {",
" g = gcd(abs(a), abs(b), x0, y0);",
" if (c % g) {",
" return false;",
" }",
"",
" x0 *= c / g;",
" y0 *= c / g;",
" if (a < 0) x0 = -x0;",
" if (b < 0) y0 = -y0;",
" return true;",
"}",
"",
"void shift_solution(long long &x, long long &y, long long a, long long b,",
" long long cnt) {",
" x += cnt * b;",
" y -= cnt * a;",
"}",
"",
"long long find_all_solutions(long long a, long long b, long long c,",
" long long minx, long long maxx, long long miny,",
" long long maxy) {",
" long long x, y, g;",
" if (!find_any_solution(a, b, c, x, y, g)) return 0;",
" a /= g;",
" b /= g;",
"",
" long long sign_a = a > 0 ? +1 : -1;",
" long long sign_b = b > 0 ? +1 : -1;",
"",
" shift_solution(x, y, a, b, (minx - x) / b);",
" if (x < minx) shift_solution(x, y, a, b, sign_b);",
" if (x > maxx) return 0;",
" long long lx1 = x;",
"",
" shift_solution(x, y, a, b, (maxx - x) / b);",
" if (x > maxx) shift_solution(x, y, a, b, -sign_b);",
" long long rx1 = x;",
"",
" shift_solution(x, y, a, b, -(miny - y) / a);",
" if (y < miny) shift_solution(x, y, a, b, -sign_a);",
" if (y > maxy) return 0;",
" long long lx2 = x;",
"",
" shift_solution(x, y, a, b, -(maxy - y) / a);",
" if (y > maxy) shift_solution(x, y, a, b, sign_a);",
" long long rx2 = x;",
"",
" if (lx2 > rx2) swap(lx2, rx2);",
" long long lx = max(lx1, lx2);",
" long long rx = min(rx1, rx2);",
"",
" if (lx > rx) return 0;",
" return (rx - lx) / abs(b) + 1;",
"}",
""
],
"description": "linear diophantine"
},
"crt ============= learn how to use": {
"prefix": "crt",
"body": [
"/**",
" * Chinese remainder theorem.",
" * Find z such that z % x[i] = a[i] for all i.",
" * */",
"long long crt(vector<long long> &a, vector<long long> &x) {",
" long long z = 0;",
" long long n = 1;",
" for (int i = 0; i < x.size(); ++i)",
" n *= x[i];",
"",
" for (int i = 0; i < a.size(); ++i) {",
" long long tmp = (a[i] * (n / x[i])) % n;",
" tmp = (tmp * mod_inv(n / x[i], x[i])) % n;",
" z = (z + tmp) % n;",
" }",
"",
" return (z + n) % n;",
"}"
],
"description": "crt"
},
"totient-seive ========what": {
"prefix": "totient-seive",
"body": [
" for (int i = 1; i < MN; i++)",
" phi[i] = i;",
"",
" for (int i = 1; i < MN; i++)",
" if (!sieve[i]) // is prime",
" for (int j = i; j < MN; j += i)",
" phi[j] -= phi[j] / i;",
""
],
"description": "totient-seive"
},
"totient ==============what": {
"prefix": "totient",
"body": [
"long long totient(long long n) {",
" if (n == 1) return 0;",
" long long ans = n;",
" for (int i = 0; primes[i] * primes[i] <= n; ++i) {",
" if ((n % primes[i]) == 0) {",
" while ((n % primes[i]) == 0) n /= primes[i];",
" ans -= ans / primes[i];",
" }",
" }",
" if (n > 1) {",
" ans -= ans / n;",
" }",
" return ans;",
"}"
],
"description": "totient"
},
"log =============learn how to use": {
"prefix": "log",
"body": [
"// Computes x which a ^ x = b mod n.",
"",
"long long d_log(long long a, long long b, long long n) {",
" long long m = ceil(sqrt(n));",
" long long aj = 1;",
" map<long long, long long> M;",
" for (int i = 0; i < m; ++i) {",
" if (!M.count(aj))",
" M[aj] = i;",
" aj = (aj * a) % n;",
" }",
"",
" long long coef = mod_pow(a, n - 2, n);",
" coef = mod_pow(coef, m, n);",
" // coef = a ^ (-m)",
" long long gamma = b;",
" for (int i = 0; i < m; ++i) {",
" if (M.count(gamma)) {",
" return i * m + M[gamma];",
" } else {",
" gamma = (gamma * coef) % n;",
" }",
" }",
" return -1;",
"}"
],
"description": "log"
},
"matrix =========understood!": {
"prefix": "matrix",
"body": [
"const int MN = 111;",
"const int mod = 10000;",
"",
"struct matrix {",
" int r, c;",
" int m[MN][MN];",
"",
" matrix (int _r, int _c) : r (_r), c (_c) {",
" memset(m, 0, sizeof m);",
" }",
"",
" void print() {",
" for (int i = 0; i < r; ++i) {",
" for (int j = 0; j < c; ++j)",
" cout << m[i][j] << \" \";",
" cout << endl;",
" }",
" }",
"",
" int x[MN][MN];",
" matrix & operator *= (const matrix &o) {",
" memset(x, 0, sizeof x);",
" for (int i = 0; i < r; ++i)",
" for (int k = 0; k < c; ++k)",
" if (m[i][k] != 0)",
" for (int j = 0; j < c; ++j) {",
" x[i][j] = (x[i][j] + ((m[i][k] * o.m[k][j]) % mod) ) % mod;",
" }",
" memcpy(m, x, sizeof(m));",
" return *this;",
" }",
"};",
"",
"void matrix_pow(matrix b, long long e, matrix &res) {",
" memset(res.m, 0, sizeof res.m);",
" for (int i = 0; i < b.r; ++i)",
" res.m[i][i] = 1;",
"",
" if (e == 0) return;",
" while (true) {",
" if (e & 1) res *= b;",
" if ((e >>= 1) == 0) break;",
" b *= b;",
" }",
"}"
],
"description": "matrix"
},
"power_in_fac ====need to check usecase": {
"prefix": "highest_exponent",
"body": [
"int highest_exponent(int p, const int &n){",
" int ans = 0;",
" int t = p;",
" while(t <= n){",
" ans += n/t;",
" t*=p;",
" }",
" return ans;",
"}"
],
"description": "power_in_fac"
},
"freq-map ===cool!": {
"prefix": "freq-map",
"body": [
"map<int, int> m;",
"for(int i=0; i<n;i++){",
" if(m.find(a[i])==m.end()) m[a[i]]=1;",
" else m[a[i]]++;",
"}"
],
"description": "freq-map"
},
"two-sat (kosaraju) =========see where and when to use! --> description has usecases!": {
"prefix": "two-sat (kosaraju)",
"body": [
"/**",
" * Given a set of clauses (a1 v a2)^(a2 v ¬a3)....",
" * this algorithm find a solution to it set of clauses.",
" * test: http://lightoj.com/volume_showproblem.php?problem=1251",
" **/",
"",
"#include<bits/stdc++.h>",
"using namespace std;",
"#define MAX 100000",
"#define endl '\\n'",
"",
"vector<int> G[MAX];",
"vector<int> GT[MAX];",
"vector<int> Ftime;",
"vector<vector<int> > SCC;",
"bool visited[MAX];",
"int n;",
"",
"",
"void dfs1(int n){",
" visited[n] = 1;",
"",
" for (int i = 0; i < G[n].size(); ++i) {",
" int curr = G[n][i];",
" if (visited[curr]) continue;",
" dfs1(curr);",
" }",
"",
" Ftime.push_back(n);",
"}",
"",
"void dfs2(int n, vector<int> &scc) {",
" visited[n] = 1;",
" scc.push_back(n);",
"",
" for (int i = 0;i < GT[n].size(); ++i) {",
" int curr = GT[n][i];",
" if (visited[curr]) continue;",
" dfs2(curr, scc);",
" }",
"}",
"",
"",
"void kosaraju() {",
" memset(visited, 0, sizeof visited);",
"",
" for (int i = 0; i < 2 * n ; ++i) {",
" if (!visited[i]) dfs1(i);",
" }",
"",
" memset(visited, 0, sizeof visited);",
" for (int i = Ftime.size() - 1; i >= 0; i--) {",
" if (visited[Ftime[i]]) continue;",
" vector<int> _scc;",
" dfs2(Ftime[i],_scc);",
" SCC.push_back(_scc);",
" }",
"}",
"",
"/**",
" * After having the SCC, we must traverse each scc, if in one SCC are -b y b, there is not a solution.",
" * Otherwise we build a solution, making the first \"node\" that we find truth and its complement false.",
" **/",
"",
"",
"bool two_sat(vector<int> &val) {",
" kosaraju();",
" for (int i = 0; i < SCC.size(); ++i) {",
" vector<bool> tmpvisited(2 * n, false);",
" for (int j = 0; j < SCC[i].size(); ++j) {",
" if (tmpvisited[SCC[i][j] ^ 1]) return 0;",
" if (val[SCC[i][j]] != -1) continue;",
" else {",
" val[SCC[i][j]] = 0;",
" val[SCC[i][j] ^ 1] = 1;",
" }",
" tmpvisited[SCC[i][j]] = 1;",
" }",
" }",
" return 1;",
"}",
"",
"// Example of use",
"",