-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvscode-snippets.txt
359 lines (358 loc) · 17.6 KB
/
vscode-snippets.txt
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
{
// 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.
// Example:
"FenwickTree": {
"prefix": "fenwick",
"body": [
"template<class T, int sz = N>",
"struct fenwick",
"{",
" T bit[sz] ;",
" void clear(){memset(bit , 0 , sizeof bit) ; }",
" fenwick(){clear() ;}",
" void add(int x, T v){for (; x < sz; x += x & -x)bit[x] += v;}",
" long long upto(int x){T ret = 0;for (;x ; x -= x & -x) ret += bit[x] ;return ret;}",
" inline T get(int l, int r) { return upto(r) - upto(l - 1); }",
"};"
],
"description": "binary indexed tree (aka. fenwick tree)"
},
"PairSegTree": {
"prefix": "pairsegtree",
"body": [
"pair<int, int> tree[4 * N];",
"void update(int ix, pair<int, int> val, int node = 1, int L = 0, int R = N - 1)",
"{",
" if (L == R)",
" return void(tree[node] = val);",
" int mid = (L + R) >> 1;",
" if (ix <= mid)",
" update(ix, val, node * 2, L, mid);",
" else",
" update(ix, val, node * 2 + 1, mid + 1, R);",
" tree[node] = max(tree[node * 2], tree[node * 2 + 1]);",
"}",
"pair<int, int> query(int l, int r, int node = 1, int L = 0, int R = N - 1)",
"{",
" if (l > r || l > R || r < L)",
" return {0, 0};",
" if (L >= l && R <= r)",
" return tree[node];",
" int mid = (L + R) >> 1;",
" return max(query(l, r, node * 2, L, mid), query(l, r, node * 2 + 1, mid + 1, R));",
"}",
],
"description": "binary indexed tree (aka fenwick tree)"
},
"ModOperations": {
"prefix": "modo",
"body": [
"ll add(ll x, ll y) { return (x + y) % mod; }",
"ll mul(ll x, ll y) { return 1ll * x * y % mod; }",
],
"description": "some useful mod operations"
},
"Union-Find": {
"prefix": "dsu",
"body": [
"struct dsu",
"{",
" int fat[N];",
" dsu()",
" {",
" iota(fat, fat + N, 0);",
" }",
" int find(int x) { return fat[x] = (x == fat[x] ? x : find(fat[x])); }",
" void link(int u, int v)",
" {",
" u = find(u), v = find(v);",
" fat[u] = v;",
" }",
" bool same(int u, int v)",
" {",
" return find(u) == find(v);",
" }",
"}; ",
],
"description": "union find data structure"
},
"Range-Set-SegTree": {
"prefix": "rangeSet",
"body": [
"struct rangeSet",
"{",
" int tree[4 * N];",
" int lazy[4 * N];",
" int sen = -1;",
" rangeSet() { memset(tree, 0, sizeof tree), memset(lazy, -1, sizeof lazy); }",
" void pull(int node) { tree[node] = tree[node * 2] + tree[node * 2 + 1]; }",
" void push(int node, int L, int R)",
" {",
" if (lazy[node] != sen)",
" {",
" tree[node] = (R - L + 1) * lazy[node];",
" if (L != R)",
" lazy[node * 2] = lazy[node * 2 + 1] = lazy[node];",
" lazy[node] = sen;",
" }",
" }",
" void update(int l, int r, int v, int node = 1, int L = 0, int R = N - 1)",
" {",
" push(node, L, R);",
" if (l > R || r < L)",
" return;",
" if (L >= l && R <= r)",
" {",
" lazy[node] = v;",
" push(node, L, R);",
" return;",
" }",
" int mid = (L + R) >> 1;",
" update(l, r, v, node * 2, L, mid);",
" update(l, r, v, node * 2 + 1, mid + 1, R);",
" pull(node);",
" }",
" int query(int l, int r, int node = 1, int L = 0, int R = N - 1)",
" {",
" if (l > r || l > R || r < L)",
" return 0;",
" push(node, L, R);",
" if (L >= l && R <= r)",
" return tree[node];",
" int mid = (L + R) >> 1;",
" return query(l, r, node * 2, L, mid) + query(l, r, node * 2 + 1, mid + 1, R);",
" }",
"};",
],
"description": "Range Set Segment Tree"
},
"Fast power mod": {
"prefix": "faspow",
"body": [
"ll faspow(ll x, ll y, ll MOD = mod)",
"{",
" ll ret = 1ll;",
" while (y)",
" {",
" if (y & 1)",
" ret = 1ll * ret * x % MOD;",
" x = 1ll * x * x % MOD;",
" y >>= 1ll;",
" }",
" return ret;",
"}",
],
"description": "Fast Power"
},
"Mod Inverse": {
"prefix": "inv",
"body": [
"ll inv(ll x) { return faspow(x, mod - 2); }",
],
"description": "Mod Inverse"
},
"nCr": {
"prefix": "ncr",
"body": [
"ll ncr(ll x, ll y) { return 1ll * fact[x] * inv(fact[y]) % mod * inv(fact[x - y]) % mod; }",
],
"description": "n choose r"
},
"Fraction": {
"prefix": "fraction",
"body": [
"template <class T>",
"struct fraction",
"{",
" T gcd(T a, T b) { return b == T(0) ? a : gcd(b, a % b); }",
" T n, d;",
" fraction(T n_ = T(0), T d_ = T(1))",
" {",
" assert(d_ != 0);",
" n = n_, d = d_;",
" if (d < T(0))",
" n = -n, d = -d;",
" T g = gcd(abs(n), abs(d));",
" n /= g, d /= g;",
" }",
" fraction(const fraction<T> &other) : n(other.n), d(other.d) {}",
" fraction<T> operator+(const fraction<T> &other) const { return fraction<T>(n * other.d + other.n * d, d * other.d); }",
" fraction<T> operator-(const fraction<T> &other) const { return fraction<T>(n * other.d - other.n * d, d * other.d); }",
" fraction<T> operator*(const fraction<T> &other) const { return fraction<T>(n * other.n, d * other.d); }",
" fraction<T> operator/(const fraction<T> &other) const { return fraction<T>(n * other.d, d * other.n); }",
" bool operator<(const fraction<T> &other) const { return n * other.d < other.n * d; }",
" bool operator<=(const fraction<T> &other) const { return !(other < *this); }",
" bool operator>(const fraction<T> &other) const { return other < *this; }",
" bool operator>=(const fraction<T> &other) const { return !(*this < other); }",
" bool operator==(const fraction<T> &other) const { return n == other.n && d == other.d; }",
" bool operator!=(const fraction<T> &other) const { return !(*this == other); }",
" void print() { cout << '(' << n << '/' << d << ')'; }",
"};",
],
"description": "struct fration with simple arithmetic operations."
},
"segtree": {
"prefix": "segtree",
"body": [
"template <class T>",
"struct segtree",
"{",
" T tree[4 * N];",
" T eval(T x, T y) { return min(x, y); }",
" void update(int ix, T val, int node = 1, int L = 0, int R = N - 1)",
" {",
" if (L == R)",
" return void(tree[node] = val);",
" int mid = (L + R) >> 1;",
" if (ix <= mid)",
" update(ix, val, node * 2, L, mid);",
" else",
" update(ix, val, node * 2 + 1, mid + 1, R);",
" tree[node] = eval(tree[node * 2], tree[node * 2 + 1]);",
" }",
" T query(int l, int r, int node = 1, int L = 0, int R = N - 1)",
" {",
" if (l > r || l > R || r < L)",
" return 1e9;",
" if (L >= l && R <= r)",
" return tree[node];",
" int mid = (L + R) >> 1;",
" return eval(query(l, r, node * 2, L, mid), query(l, r, node * 2 + 1, mid + 1, R));",
" }",
"};",
],
"description": "basic struct segment tree."
},
"pragma-optimizations": {
"prefix": "optimize",
"body": [
"#pragma GCC optimize(\"-Ofast\")",
"//#pragma GCC optimize(\"trapv\")",
"#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native\")",
"#pragma GCC optimize(\"-ffast-math\")",
"#pragma GCC optimize(\"-funroll-loops\")",
],
"description": "some pragma optimizations."
},
"miller-rabin": {
"prefix": "miller",
"body": [
"bool miller(ll n, int k = 40)",
"{",
" if (n <= 3)",
" return n > 1;",
" int s = 0;",
" ll d = n - 1;",
" while (~d & 1)",
" d >>= 1, s++;",
"",
" auto modpow = [&](ll x, ll y, ll MOD = mod) -> ll {",
" ll ret = 1ll;",
" while (y)",
" {",
" if (y & 1)",
" ret = 1ll * ret * x % MOD;",
" x = 1ll * x * x % MOD;",
" y >>= 1ll;",
" }",
" return ret;",
" };",
" while (k--)",
" {",
" ll a = (n - 3) * rand() / RAND_MAX + 2;",
" ll x = modpow(a, d, n);",
" if (x == 1 || x == n - 1)",
" continue;",
" bool ok = false;",
" for (int i = 0; i < s - 1; ++i)",
" {",
" x = (x * x) % n;",
" if (x == 1)",
" return false;",
" if (x == n - 1)",
" {",
" ok = true;",
" break;",
" }",
" }",
" if (!ok)",
" return false;",
" }",
" return true;",
"}",
],
"description": "probablistic test for compositeness."
},
"ordered-set": {
"prefix": "orderd_set",
"body": [
"#include <ext/pb_ds/assoc_container.hpp>",
"using namespace __gnu_pbds; ",
"typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> os;",
],
"description": "ordered set."
},
"rnd": {
"prefix": "rnd",
"body": [
"auto ra = [] {char *p = new char ; delete p ; return ll(p) ; };",
"mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * (ra() | 1));",
],
"description": "random generator."
},
"kosaraj-SCC": {
"prefix": "kosaraju",
"body": [
"vector<int> adj[N] , jda[N] ; ",
"int viz[N] ;",
"void dfs1(int x, vector<int> &ord){",
" viz[x] = 1;",
" for(auto u : adj[x]){",
" if(!viz[u]){",
" dfs1(u , ord) ; ",
" }",
" }",
" ord.push_back(x) ; ",
"}",
"void dfs2(int x, vector<int> &comp){",
" viz[x] = 2 ; ",
" comp.push_back(x) ; ",
" for(auto u : jda[x]){",
" if(viz[u] != 2){",
" dfs2(u, comp) ; ",
" }",
" }",
"}",
"void kosaraju(){",
" vector<int> ord ; ",
" for(int i = 0; i< n;++ i){",
" if(!viz[i]){",
" dfs1(i , ord) ; ",
" }",
" }",
" vector<vector<int> > comps; ",
" while(ord.size()){",
" int bk = ord.back() ; ",
" ord.pop_back() ; ",
" if(viz[bk] != 2){",
" vector<int> comp ; ",
" dfs2(bk,comp) ;",
" comps.push_back(comp) ; ",
" }",
" }",
" cout << comps.size() << endl ; ",
" for(auto &u : comps){",
" cout << u.size() << ' ' ; ",
" for(auto u2 : u){",
" cout << u2 <<' ' ; ",
" }",
" cout << endl; ",
" }",
"}",
],
"description": "get all strongly connected components in a digraph."
},
}