-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcpp.snippets
1192 lines (1067 loc) · 24.2 KB
/
cpp.snippets
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
snippet vec2
vector<vector<${1:T}>> ${2:name}(${3:size}, vector<$1>(${4:size}));
snippet template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
${1}
return 0;
}
snippet tests
int tt;
cin >> tt;
while(tt--) {
${1}
}
snippet fenwick
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
snippet debug
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef ABHI
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#define eprintf(...) 42
#endif
snippet trace
#ifdef ABHI
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
snippet segt
template<class T, int SZ> struct Seg { // SZ should be power of 2
T seg[2 * SZ], ID = 0;
Seg() {
for(int i = 0; i < 2 * SZ; ++i) seg[i] = ID;
}
T comb(T a, T b) {
return max(a, b);
}
// easily change this to min or max
// comb(ID,b) must equal b
void build() { for(int i = SZ - 1; i >= 0; --i) { seg[i] = comb(seg[2*i],seg[2*i+1]); } }
void upd(int p, T value) { // set value at position p
for (seg[p += SZ] = value; p > 1; p >>= 1)
seg[p >> 1] = comb(seg[(p|1)^1],seg[p|1]);
// make sure non-commutative operations work
}
T query(int l, int r) { // query on interval [l, r]
T res1 = ID, res2 = ID; r++;
for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
if (l&1) res1 = comb(res1,seg[l++]);
if (r&1) res2 = comb(seg[--r],res2);
}
return comb(res1,res2);
}
};
snippet segtree
class SegTree {
public:
struct Node {
// don't forget to set default value (used for leaves)
// not necessarily neutral element!
int x = 0;
int add = 0;
void apply(int val){
x = val;
}
void update(int val){
x += val;
}
};
private:
vector<Node> tree;
int N , n;
// for lazy propogation
inline void push(int x , int l , int r){
if(tree[x].add != 0){
tree[x].update(tree[x].add);
if(l != r){
int cover = (r - l + 1);
tree[x << 1].add += (tree[x].add);
tree[(x << 1)|1].add += (tree[x].add);
}
tree[x].add = 0;
}
}
Node combine(const Node &A , const Node &B){
Node ans;
ans.x = max(A.x , B.x);
return ans;
}
void build(int s , int e , int idx) {
if(s == e){
tree[idx].apply(0);
return;
}
int mid = (s+e) >> 1;
build(s , mid , (idx << 1));
build(mid + 1 , e , ((idx << 1)|1));
return;
}
template <typename T>
void build(const vector < T > &ar , int s , int e , int idx) {
if(s == e){
tree[idx].apply(ar[s]);
return;
}
int mid = (s+e) >> 1;
build(ar , s , mid , (idx << 1));
build(ar , mid + 1 , e , ((idx << 1)|1));
tree[idx] = combine(tree[idx << 1] , tree[(idx << 1)|1]);
return;
}
Node query(int s , int e , int lf , int rf , int idx) {
push(idx , s , e);
if(rf < s or e < lf) return Node();
if(lf <= s and e <= rf){
return tree[idx];
}
int mid = (s + e) >> 1;
int l = idx << 1;
int r = (idx << 1) | 1;
if(lf <= mid){
if(rf <= mid){
return query(s , mid , lf , rf , l);
}else{
return combine(query(s , mid , lf , rf , l) , query(mid + 1 , e , lf , rf , r));
}
}else{
return query(mid + 1 , e , lf , rf , r);
}
}
template <typename M>
void update(int s , int e , int i , M val , int idx) {
if(s > i or e < i) return;
if(s == e){
tree[idx].apply(val);
return;
}
int mid = (s + e) >> 1;
update(s , mid , i , val , (idx << 1));
update(mid + 1 , e , i , val , (idx << 1)|1);
tree[idx] = combine(tree[idx << 1] , tree[(idx << 1) | 1]);
return;
}
template <typename M>
void update(int s , int e , int l , int r , M val , int idx) {
push(idx , s , e);
if(s > r or e < l or s > e) return;
if(l <= s && e <= r){
tree[idx].add += val;
push(idx , s , e);
return;
}
int mid = (s + e) >> 1;
update(s , mid , l , r , val , (idx << 1));
update(mid + 1 , e , l , r , val , (idx << 1)|1);
tree[idx] = combine(tree[idx << 1] , tree[(idx << 1) | 1]);
return;
}
public:
template<typename T>
SegTree(const vector<T> &ar) {
n = (int) ar.size();
N = 4*(n) + 1;
tree.resize(N, {});
build(ar , 0 , n - 1 , 1);
}
SegTree(int _n) : n (_n){
N = 4 * (n) + 1;
tree.resize(N , {});
build(0 , n - 1 , 1);
}
SegTree(){
n = 100000;
N = 4 * n + 1;
tree.resize(N , {});
}
void build(){
build(0 , n - 1 , 1);
}
template<typename T>
void build(const vector<T> &ar) {
build(ar , 0 , n - 1 , 1);
}
Node query(int l ,int r){
return query(0 , n - 1 , l , r , 1);
}
template<typename T>
void update(int l , int r , T val){
update(0 , n - 1 , l , r , val , 1);
}
template<typename M>
void update(int i , M val) {
update(0 , n - 1 , i , val , 1);
}
};
snippet math
const ll MOD = 998244353;
inline ll add(ll a, ll b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline ll sub(ll a, ll b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline ll mul(ll a) {
return a;
}
template<typename... T>
inline ll mul(ll a, T... b) {
return (ll) ((ll) a * mul(b...) % MOD);
}
inline ll power(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
inline ll inv(ll a) {
a %= MOD;
if (a < 0) a += MOD;
ll b = MOD, u = 0, v = 1;
while (a) {
ll t = b / a;
b -= t * a; swap(a, b);
u -= t * v; swap(u, v);
}
assert(b == 1);
if (u < 0) u += MOD;
return u;
}
snippet begin
${1:ar}.begin(), $1.end()
snippet convexhull
namespace geo{
struct point{
int x , y;
};
bool operator < (point a, point b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool cw(point a, point b, point c) { //clockwise
return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y-b.y) < 0;
}
bool ccw(point a, point b, point c) { //counter-clockwise
return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0;
}
}
using namespace geo;
vector <point> convex_hull(vector<point> a) {
if (a.size() == 1)
return a;
sort(a.begin(), a.end());
point p1 = a[0], p2 = a.back();
vector<point> up, down;
up.push_back(p1);
down.push_back(p1);
for (int i = 1; i < (int)a.size(); i++) {
if (i == (int) a.size() - 1 || cw(p1, a[i], p2)) {
while (up.size() >= 2 && !cw(up[up.size()-2], up[up.size()-1], a[i]))
up.pop_back();
up.push_back(a[i]);
}
if (i == (int) a.size() - 1 || ccw(p1, a[i], p2)) {
while(down.size() >= 2 && !ccw(down[down.size()-2], down[down.size()-1], a[i]))
down.pop_back();
down.push_back(a[i]);
}
}
vector <point> ans;
for (int i = 0; i < (int)up.size(); i++)
ans.push_back(up[i]);
for (int i = (int) down.size() - 2; i > 0; i--)
ans.push_back(down[i]);
return ans;
}
snippet dsu
template <typename T>
class dsu {
public:
vector<T> p;
vector<T> rank;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
rank.resize(n);
iota(p.begin(), p.end(), 0);
fill(rank.begin(), rank.end() , 1);
}
inline T get(T x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
inline bool unite(T x, T y) {
T px = get(x);
T py = get(y);
if (px != py) {
if(rank[px] == rank[py]){
p[py] = px;
rank[px]++;
} else if(rank[px] < rank[py]){
p[px] = py;
} else {
p[py] = px;
}
return true;
}
return false;
}
};
snippet dijkstra
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
const ll inf = (ll) (1e18);
void dijkstra(int S) {
ll u , vv , c , w;
minpq<pair<ll, ll>> Q;
fill(dist.begin(), dist.end(), inf);
Q.push({0,S});
dist[S] = 0;
while(!Q.empty()){
u = Q.top().second;
c = Q.top().first;
Q.pop();
if(dist[u] < c){
continue;
}
for(int i = 0 ; i < (int) G[u].size() ; i++){
w = G[u][i].first;
vv = G[u][i].second;
if(dist[vv] > dist[u] + w){
dist[vv] = dist[u] + w;
Q.push({dist[vv],vv});
}
}
}
}
snippet dbg
#ifdef ABHI
#include "../debug.h"
#define _GLIBCXX_DEBUG
#else
#define debug(...) 42
#define cerr if(0) cout
#endif
snippet eulerphi
template<typename T>
T phi(T n){
double res = n;
for(int p = 2 ; p*p <= n ; ++p){
if(n % p == 0){
while(n % p == 0) {
n /= p;
}
res -= res/p;
}
}
if(n > 1){
res -= res / n;
}
return (T) res;
}
snippet exgcd
template <typename T>
T gcdE(T a , T b , T &x , T &y){
if(a == 0){
*x = 0;
*y = 1;
return b;
}
T x1 , y1;
T gcd = gcdE(b%a , a , &x1 , &y1);
x = y1 - (b/a)*x1;
y = x1;
return gcd;
}
snippet fenwick2d
template <typename T>
class fenwick{
vector<vector<T>> tree;
int n;
public:
fenwick(int _n) : n(_n){
tree.resize(n + 1 , vector <T>(n + 1,0));
}
void modify(int x , int y , T val){
for(int i = x ; i <= n ; i += (i&-i)){
for(int j = y ; j <= n ; j += (j&-j)){
tree[i][j] += val;
}
}
}
T query(int x , int y){
T res = 0;
for(int i = x ; i ; i -= (i&-i)){
for(int j = y ; j ; j-= (j&-j)){
res += tree[i][j];
}
}
return res;
}
T query(int x1 , int y1 , int x2 , int y2){
return (query(x2,y2) - query(x2,y1-1) - query(x1-1,y2) + query(x1-1,y1-1));
}
};
snippet hashing
// code credit : https://www.codechef.com/viewsolution/18660185
typedef long long lint;
const int maxn = 400001;
const int mod = 1004669333, base = 33, inv_base = 121778101;
vector<int> base_pow(maxn + 1), inv_base_pow(maxn + 1);
void prep() {
base_pow[0] = 1;
for (int i = 1; i <= maxn; ++i)
base_pow[i] = (lint)base_pow[i - 1] * base % mod;
inv_base_pow[0] = 1;
for (int i = 1; i <= maxn; ++i)
inv_base_pow[i] = (lint)inv_base_pow[i - 1] * inv_base % mod;
}
struct hashes_t {
string s;
int n;
vector<int> acc_hash, acc_inv_hash;
hashes_t(const string &_s): s(_s), n(s.size()), acc_hash(n + 1, 0)
, acc_inv_hash(n + 1, 0) {
for (int i = 0; i < n; ++i) {
acc_hash[i + 1] =
(acc_hash[i] + (lint)base_pow[i] * (s[i] - 'a' + 1)) % mod;
acc_inv_hash[i + 1] =
(acc_inv_hash[i] + (lint)inv_base_pow[i] * (s[i] - 'a' + 1)) % mod;
}
}
int get_hash(int a, int b) {
int hash = acc_hash[b + 1] - acc_hash[a];
if (hash < 0) hash += mod;
hash = (lint)hash * inv_base_pow[a] % mod;
return hash;
}
int get_inv_hash(int a, int b) {
int hash = acc_inv_hash[b + 1] - acc_inv_hash[a];
if (hash < 0) hash += mod;
hash = (lint)hash * base_pow[b] % mod;
return hash;
}
};
snippet kmp
template <typename T>
vector<int> kmp_table(int n, const T &s) {
vector<int> p(n, 0);
int k = 0;
for (int i = 1; i < n; i++) {
while (k > 0 && !(s[i] == s[k])) {
k = p[k - 1];
}
if (s[i] == s[k]) {
k++;
}
p[i] = k;
}
return p;
}
template <typename T>
vector<int> kmp_table(const T &s) {
return kmp_table((int) s.size(), s);
}
template <typename T>
vector<int> kmp_search(int n, const T &s, int m, const T &w, const vector<int> &p) {
assert(n >= 1 && (int) p.size() == n);
vector<int> res;
int k = 0;
for (int i = 0; i < m; i++) {
while (k > 0 && (k == n || !(w[i] == s[k]))) {
k = p[k - 1];
}
if (w[i] == s[k]) {
k++;
}
if (k == n) {
res.push_back(i - n + 1);
}
}
return res;
// returns 0-indexed positions of occurrences of s in w
}
template <typename T>
vector<int> kmp_search(const T &s, const T &w, const vector<int> &p) {
return kmp_search((int) s.size(), s, (int) w.size(), w, p);
}
snippet LCA
const int MAXN = 200010;
const int LG = 20;
vector<vector<int>> g(MAXN);
vector<vector<int>> par(MAXN, vector<int>(LG, -1));
vector<int> dep(MAXN);
void dfs(int u, int p) {
if(p == -1) dep[u] = 0;
else dep[u] = dep[p] + 1;
par[u][0] = p;
for(int x: g[u]) if(x != p) {
dfs(x, u);
}
}
void precompute() {
for(int k = 1; k < LG; ++k) {
for(int i = 0; i < MAXN; ++i) {
if(par[i][k - 1] != -1) {
par[i][k] = par[par[i][k - 1]][k - 1];
}
}
}
}
int walk(int u, int k) {
for(int i = 0; i < LG && u != -1; ++i) {
if((k >> i) & 1) {
assert(u != -1);
u = par[u][i];
}
}
return u;
}
int LCA(int u, int v) {
if(dep[u] < dep[v]) {
v = walk(v, dep[v] - dep[u]);
}
if(dep[v] < dep[u]) {
u = walk(u, dep[u] - dep[v]);
}
if(u == v) {
return u;
}
for(int i = LG - 1; i >= 0; --i) {
if(par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[u][0];
}
int dist(int u, int v) {
int L = LCA(u, v);
return dep[u] + dep[v] - 2 * ((L + 1) ? dep[L] : 0);
}
snippet matrix
const int sz = 3;
struct vec{
int arr[SZ];
void reset(){
memset(arr , 0 , sizeof(arr));
}
};
struct matrix{
int arr[SZ][SZ];
void reset(){
memset(arr , 0 , sizeof(arr));
}
void makeiden(){
reset();
for(int i = 0 ; i < SZ ; ++i){
arr[i][i] = 1;
}
}
matrix operator + (const matrix &o) const {
matrix res;
for(int i = 0 ; i < SZ ; ++i){
for(int j = 0 ; j < SZ ; ++j){
res.arr[i][j] = arr[i][j] + o.arr[i][j];
res.arr[i][j] -= (res.arr[i][j] >= mod) * mod;
}
}
return res;
}
matrix operator * (const matrix &o) const {
matrix res;
for(int i = 0 ; i < SZ ; ++i){
for(int j = 0 ; j < SZ ; ++j){
res.arr[i][j] = 0;
for(int k = 0 ; k < SZ ; ++k){
res.arr[i][j] = (res.arr[i][j] + 1LL * arr[i][k] * o.arr[k][j]) % mod;
}
}
}
return res;
}
vec operator * (const vec &o) const {
vec res;
for(int i = 0 ; i < SZ ; ++i){
res.arr[i] = 0;
for(int k = 0 ; k < SZ ; ++k){
res.arr[i] = (res.arr[i] + 1LL * arr[i][k] * o.arr[k]) % mod;
}
}
return res;
}
};
snippet miller
using ll = long long;
ll power(ll n , ll p , ll MOD){
ll ans = 1;
n %= MOD;
for(;p;p>>=1){
if(p&1){
ans = (ans*n)%MOD;
}
n = (n*n)%MOD;
}
return ans;
}
bool miillerTest(ll d, ll n) {
ll a = 2 + rand() % (n - 4);
ll x = power(a, d, n);
if (x == 1 || x == n-1)
return true;
while (d != n-1) {
x = (x * x) % n;
d *= 2;
if (x == 1) return false;
if (x == n-1) return true;
}
return false;
}
bool isPrime(ll n, ll k) {
if (n <= 1 || n == 4) return false;
if (n <= 3) return true;
ll d = n - 1;
while (d % 2 == 0)
d /= 2;
for (ll i = 0; i < k; i++)
if (miillerTest(d, n) == false)
return false;
return true;
}
snippet persistenttrie
// code credit : https://www.codechef.com/viewsolution/23850895
namespace persistentTrie {
const int MAXN = 200007, LOG = 22;
int root[MAXN], availNode = 0;
struct node {
int ara[2], sum;
node() {}
} tree[MAXN * LOG];
void insert(int prevnode, int &curRoot, int val) {
curRoot = ++availNode;
int curnode = curRoot;
for(int i = 20; i >= 0; i--) {
bool bit = val & (1 << i);
tree[curnode] = tree[prevnode];
tree[curnode].ara[bit] = ++availNode;
tree[curnode].sum += 1;
prevnode = tree[prevnode].ara[bit];
curnode = tree[curnode].ara[bit];
}
tree[curnode] = tree[prevnode];
tree[curnode].sum += 1;
}
int find_xor_max(int prevnode, int curnode, int x) {
int ans = 0;
for(int i = 20; i >= 0; i--) {
bool bit = x & (1 << i);
if(tree[tree[curnode].ara[bit ^ 1]].sum > tree[tree[prevnode].ara[bit ^ 1]].sum) {
curnode = tree[curnode].ara[bit ^ 1];
prevnode = tree[prevnode].ara[bit ^ 1];
ans = ans | (1 << i);
}
else {
curnode = tree[curnode].ara[bit];
prevnode = tree[prevnode].ara[bit];
}
}
return ans;
}
// USAGE
// inline void solve() {
// insert(root[0], root[0], 0);
// int n;
// input(n);
// for(int i = 1; i <= n; i++) {
// int val;
// input(val);
// insert(root[i - 1], root[i], val);
// }
// int q;
// input(q);
// while(q--) {
// int l, r, val;
// input(l); input(r); input(val);
// output(find_xor_max(root[l - 1], root[r], val));
// outchar('\n');
// }
// }
} // persistent trie
using namespace persistentTrie;
snippet psegtree
struct Node {
int x;
Node* l, *r;
Node(Node* _l = NULL , Node* _r = NULL , int _x = 0) {
l = _l;
r = _r;
x = _x;
}
};
int ar[N];
Node* versions[N];
void build(Node* tree , int l , int r) {
if(r < l) return;
if(l == r) {
tree->x = ar[l];
return;
}
int mid = (l + r) >> 1;
tree->l = new Node();
tree->r = new Node();
build(tree->l , l , mid);
build(tree->r , mid + 1 , r);
tree->x = tree->l->x + tree->r->x;
}
void update(Node* old, Node* curr, int l , int r , int idx , int val) {
if(idx < l || idx > r || r < l) return;
if(l == r) {
curr->x = old->x + val;
return;
}
int mid = (l + r) >> 1;
if(idx <= mid) {
curr->r = old->r;
curr->l = new Node();
update(old->l , curr->l , l , mid , idx , val);
} else {
curr->l = old->l;
curr->r = new Node();
update(old->r , curr->r , mid + 1 , r , idx , val);
}
curr->x = curr->l->x + curr->r->x;
}
int query(Node* root , int l , int r , int lo , int hi) {
if(lo > r || hi < l || r < l) return 0;
if(root == NULL) assert(false);
if(lo <= l && r <= hi) {
return root->x;
}
int mid = (l + r) >> 1;
int a = query(root->l , l , mid , lo , hi);
int b = query(root->r , mid + 1 , r , lo , hi);
return a + b;
}
snippet factor
const int MAXN = 1e5 + 10;
vector<int> spf(MAXN);
void sieve(){
iota(spf.begin(), spf.end() , 0);
for (int i = 4 ; i < MAXN ; i += 2)
spf[i] = 2;
for (int i = 3 ; i * i < MAXN ; i++) {
if (spf[i] == i) {
for (int j = i * i; j < MAXN ; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
vector<int> getF(int x){
vector<int> factors;
while (x != 1) {
factors.push_back(spf[x]);
x = x / spf[x];
}
return factors;
}
snippet pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
snippet remax
template<typename T> void remax (T &u, const T &v) {u = (v > u ? v : u);}
template<typename T> void remin (T &u, const T &v) {u = (v < u ? v : u);}
snippet sieve
vector <int> primes;
void sieve(){
const int n = 1000010;
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2 ; p * p <= n; ++p) {
if (prime[p] == true) {
for (int i = p * 2 ; i <= n ; i += p)
prime[i] = false;
}
}
for (int p = 2 ; p <= n ; p++)
if (prime[p])
primes.push_back(p);
}
snippet trie
const int SZ = 26;
struct TrieNode {
vector<TrieNode*> node;
int pass = 0;
TrieNode() {
pass = 0;
node.resize(SZ);
for(int i = 0; i < SZ; ++i) {
node[i] = nullptr;
}
}
};
class Trie {
TrieNode *root, *temp;
public:
Trie() {
root = new TrieNode;
}
void add(const string &s) {
temp = root;
for(auto &c: s) {
int x = c - 'a';
if(temp->node[x] == nullptr) {
temp->node[x] = new TrieNode;
}
temp = temp->node[x];
temp->pass++;
}
}
int query(const string &s) {
temp = root;
for(auto &c: s) {
int x = c - 'a';
if(temp->node[x] == nullptr) {