个人模板

这里有许多模板

矩阵

主要实现矩乘

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
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

template<typename T>
inline T read()
{
T data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
return data*w;
}

/*if you want to use "int" to build an Matrix, you can
*
*int n, m;
*...
*const int N(n), M(m);
*Matrix<N, M> matrix;
*
*/

template<int N, int M>
struct Matrix
{
private:
int a[N][M];
int MOD;
public:
Matrix(int MO) : MOD(MO) { clear(a, 0); }
int *operator [] (int index)
{
return a[index];
}

template<int K>
Matrix<N, K> operator * (Matrix<M, K> &b)
{
Matrix<N, K> c(MOD);
for(RG int i=0;i<N;i++)
for(RG int j=0;j<M;j++)
for(RG int k=0;k<K;k++)
c[i][k]=(c[i][k]+a[i][j]*b[j][k])%MOD;
return c;
}
};

int main()
{

return 0;
}

Trees

Splay

Splay大法好

替罪羊树

跑得比Splay快n倍

Treap:

跑得比Splay慢…

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
#ifndef BALANCE_TREE_HPP
#define BALANCE_TREE_HPP
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

template<typename T>
inline T read()
{
T data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
return data*w;
}
namespace Balance_tree
{

class Splay
{
private:
struct node
{
int val, size;
node *fa, *son[2];
node(int v, node *f):fa(f), val(v){ son[0]=son[1]=NULL; size=1; }
}*root;
inline int size(node *x) { return x==NULL?0:x->size; }
inline void update(node *x) { x->size=size(x->son[0])+size(x->son[1])+1; }
inline bool son(node *f, node *x) { return f->son[1]==x; }
inline int cmp(node *t, int x) { return x < t->val ? 0 : (x == t->val ? -1 : 1); }
inline void rotate(node *x)
{
node *f=x->fa, *g=f->fa;
bool a=son(f, x), b=!a;
f->son[a]=x->son[b]; if(x->son[b]) x->son[b]->fa=f;
x->son[b]=f; f->fa=x; x->fa=g;
if(g) g->son[son(g, f)]=x;
else root=x;
update(f); update(x);
}
inline void splay(node *x, node *r=NULL)
{
for(;x->fa!=r;rotate(x))
{
node *f=x->fa, *g=f->fa;
if(g!=r) rotate((son(g, f)^son(f, x) ? x : f));
}
}

inline void print(node *x) { if(!x) return; print(x->son[0]); printf("%d ", x->val); print(x->son[1]); }
public:
inline void insert(int x)
{
if(!root) { root = new node(x, NULL); return; }
node *t=root, *f=NULL;
while(t) { int val=t->val; f=t; t=t->son[x>val]; }
t=new node(x, f); if(f) f->son[x>f->val]=t;
splay(t);
}

inline void find(int x)
{
node *t=root; while(t->val!=x && t) t=t->son[x>t->val];
if(t) splay(t);
}

inline void erase(int x)
{
find(x); node *t=root->son[0], *del=root;
if(!t) root=root->son[1];
else
{
while(t->son[1]) t=t->son[1];
splay(t, root); root=t;
root->son[1]=del->son[1]; if(root->son[1]) root->son[1]->fa=root;
}
delete del;
if(root) root->fa=NULL;
update(root);
}

inline int k_th(int k)
{
node *t=root;
while(t)
{
int s = size(t->son[0])+1;
if(k>s) t=t->son[1], k-=s;
else if(k==s) return t->val;
else t=t->son[0];
}
return -1;
}

inline int rank(int x)
{
int ans=0; node *t=root;
while(t)
{
int d=cmp(t, x);
if(d==1) ans+=size(t->son[0])+1;
if(d==-1) d=0;
t=t->son[d];
}
return ans+1;
}

inline int suc(int x, int k) //1->suc, 0->pre
{
int res=-1; node *t=root;
int q=k, c=q^1;
while(t)
{
int d=cmp(t, x);
if(d==c) res=t->val;
if(d==-1) d=q;
t=t->son[d];
}
return res;
}

inline void print() { print(root); }
};

const int maxn();
const int alpha(0.75);

class scapetree
{
private:
struct node
{
int val, size;
node *son[2];
int cmp(int x) const { return x < val ? 0 : ( x== val ? -1 : 1); }
node(int v=0) : val(v), size(1) { son[0]=son[1]=NULL; }
}*root, *xl[maxn];
int tot;
void print(node *r)
{
if(r==NULL) return;
print(r->son[0]);
xl[++tot]=r;
print(r->son[1]);
}

int size(node *t) { return t==NULL?0:t->size; }
void update(node *t) { t->size=size(t->son[0])+size(t->son[1])+1; }

void rebuild(node *&rt, int l, int r)
{
if(l>r) { rt=NULL; return; }
int mid=(l+r)>>1;
rt=xl[mid];
rebuild(rt->son[0], l, mid-1);
rebuild(rt->son[1], mid+1, r);
update(rt);
}

void insert(node *&r, int x)
{
if(r==NULL) r=new node(x);
else
{
int t=r->cmp(x);
if(t==-1) t=0;
insert(r->son[t], x);
}
update(r);
}

void scape(node *&r, int x)
{
if(r==NULL) return;
int d=r->cmp(x);
if(d==-1) return;
if(size(r->son[d])>r->size*alpha)
{
tot=0;
print(r);
rebuild(r, 1, tot);
}
else scape(r->son[d], x);
}

void erase(node *&r, int x)
{
int d=r->cmp(x);
if(d==-1)
{
if((!r->son[0])&&(!r->son[1])) r=NULL;
else
{
tot=0;
print(r->son[0]);
print(r->son[1]);
delete r;
r=NULL;
rebuild(r, 1, tot);
}
}
else erase(r->son[d], x);
if(r) update(r);
}
public:
scapetree(){ root=NULL; }

void insert(int x)
{
insert(root, x);
scape(root, x);
}

void erase(int x)
{
erase(root, x);
}

int k_th(int k)
{
node *t=root;
while(t)
{
int s=size(t->son[0])+1;
if(s==k) return t->val;
if(s<k) k-=s, t=t->son[1];
else t=t->son[0];
}
return -1;
}

int rank(int x)
{
int ans=0;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==1) ans+=size(t->son[0])+1;
if(d==-1) d=0;
t=t->son[d];
}
return ans+1;
}

int suc(int x)
{
int res=-1;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==0) res=t->val;
if(d==-1) d=1;
t=t->son[d];
}
return res;
}

int pre(int x)
{
int res=-1;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==1) res=t->val;
if(d==-1) d=0;
t=t->son[d];
}
return res;
}
};
class Treap
{
private:
struct node
{
int val, ran, size;
node *son[2];
node(int k=0)
:val(k)
{
ran=rand();
son[0]=son[1]=NULL;
size=1;
}
int cmp(int k)
{
return k < val ? 0 : ( k == val ? -1 : 1 );
}
}*root;

int size(node *t) { return t == NULL ? 0 : t->size; }

void update(node *t)
{
t -> size = size(t -> son[0]) + size(t -> son[1]) + 1;
}

void rotate(node *&t, int f)
{
node *son=t->son[f^1];
t->son[f^1]=son->son[f];
son->son[f]=t;
update(t);
update(son);
t=son;
}

void insert(node *&t, int value)
{
if(t==NULL) t=new node(value);
else
{
int f=(t->val<=value);
insert(t->son[f], value);
if((t->son[f]->ran) > (t->ran)) rotate(t, f^1);
}
update(t);
}

void erase(node *&t, int value)
{
int f=t->cmp(value);
if(f==-1)
{
node *t1=t;
if(t->son[0]==NULL)
{
t=t->son[1];
delete t1;
t1=NULL;
}
else if(t->son[1]==NULL)
{
t=t->son[0];
delete t1;
t1=NULL;
}
else
{
int f2=(t->son[0]->ran > t->son[1]->ran);
rotate(t, f2);
erase(t->son[f2], value);
}
}
else erase(t->son[f], value);
if(t!=NULL) update(t);
}
public:
Treap(){ root=NULL; srand(time(NULL)); }

void insert(int val) { insert(root, val); }

void erase(int val) { erase(root, val); }

int k_th(int k)
{
node *t=root;
while(t)
{
int s=size(t->son[0])+1;
if(s==k) return t->val;
if(s<k) k-=s, t=t->son[1];
else t=t->son[0];
}
return -1;
}

int rank(int x)
{
int ans=0;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==1) ans+=size(t->son[0])+1;
if(d==-1) d=0;
t=t->son[d];
}
return ans+1;
}

int suc(int x)
{
int res=-1;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==0) res=t->val;
if(d==-1) d=1;
t=t->son[d];
}
return res;
}

int pre(int x)
{
int res=-1;
node *t=root;
while(t)
{
int d=t->cmp(x);
if(d==1) res=t->val;
if(d==-1) d=0;
t=t->son[d];
}
return res;
}
};
}
#endif

玄学读入

方法$1$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

template<typename T>
inline T read()
{
T data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
return data*w;
}

方法$2$

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
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "rb", stdin);freopen(#x".out", "w+", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

namespace quickIO
{
const int MAXS = (1<<21);
char buf[MAXS], *p;
int len;

inline int read()
{
int data=0, w=1;
char ch=*p++;
while(ch!='-'&&(ch<'0'||ch>'9')&&p-buf<len&&(*p)) ch=*p++;
if(ch=='-') w=-1, ch=*p++;
while(ch>='0'&&ch<='9'&&p-buf<len&&(*p)) data=(data<<3)+(data<<1)+(ch^48), ch=*p++;
return data*w;
}

void init()
{
len = fread(buf,1,MAXS,stdin);
buf[len] = '\0';
p=buf;
}
}

using namespace quickIO;

树剖神秘非递归

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
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

template<typename T = int>
inline T read()
{
T data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
return data*w;
}

struct edge { int next, to; } e[maxn << 1];
int head[maxn], e_num, now[maxn], fa[maxn],
size[maxn], heavy[maxn], pos[maxn], belong[maxn],
cnt_pos[maxn], out_pos[maxn], cnt_node;
bool dk[maxn];
inline void add_edge(int from, int to) { e[++e_num]={head[from], to}; head[from]=e_num; }
inline void split(int root)
{
// 记得在初始化时加 size[i]=1;
clear(now, -1);
RG int i=root;
while(i)
{
do { now[i]=(~now[i]) ? (e[now[i]].next) : (head[i]); } while(fa[i]==e[now[i]].to && now[i]);
if(now[i]) { fa[e[now[i]].to]=i; i=e[now[i]].to; }
else
{
if(fa[i]) size[fa[i]]+=size[i], heavy[fa[i]]=(size[heavy[fa[i]]] < size[i] ? i : heavy[fa[i]]);
i=fa[i];
}
}
i=root; belong[i]=i;
pos[i]=1; cnt_pos[1]=i;
cnt_node=1;
clear(now, -1);
while(i)
{
int k=heavy[i];
if(!dk[i])
{
dk[i]=true;
if(!k) out_pos[i]=cnt_node, i=fa[i];
else pos[k]=++cnt_node, cnt_pos[cnt_node]=k, belong[k]=belong[i], i=k;
continue;
}
do { now[i]=(~now[i]) ? (e[now[i]].next) : (head[i]); }
while((fa[i]==e[now[i]].to || k==e[now[i]].to) && now[i]);
if(now[i])
{
pos[i=e[now[i]].to]=++cnt_node;
cnt_pos[cnt_node]=i; belong[i]=i;
}
else out_pos[i]=cnt_node, i=fa[i];
}
}

int main()
{

return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×