模板大全

  1. 1. 高精度
  2. 2. ST 表
  3. 3. 并查集
  4. 4. 线段树
  5. 5. 树状数组
  6. 6. 普通平衡树

整理封装自 ryanright 的博客

高精度

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
#include<bits/stdc++.h>
using namespace std;
#define INT signed

struct ubigint{
protected:

vector<INT> v;
static const INT BASE=100000000;

public:
ubigint() {v.clear();v.push_back(0);};
ubigint(int number)
{
v.clear();
v.push_back(number);
}
ubigint(long long number)
{
v.clear();
do
{
INT lastInt=number%BASE;
v.push_back(lastInt);
}
while(number/=BASE);
}
ubigint(string number)
{
v.clear();
while(number.length()>8)
{
INT nowInt,len=number.length();
sscanf(number.substr(len-8).c_str(),"%d",&nowInt);
v.push_back(nowInt);
number=number.substr(0,len-8);
}
INT nowInt;
sscanf(number.substr(0).c_str(),"%d",&nowInt);
v.push_back(nowInt);
}
ubigint(char* num)
{
string number=num;
v.clear();
while(number.length()>8)
{
INT nowInt,len=number.length();
sscanf(number.substr(len-8).c_str(),"%d",&nowInt);
v.push_back(nowInt);
number=number.substr(0,len-8);
}
INT nowInt;
sscanf(number.substr(0).c_str(),"%d",&nowInt);
v.push_back(nowInt);
}
void print() const
{
for(INT i=v.size()-1;i>=0;i--)
{
if(i==v.size()-1) printf("%d",v[i]);
else printf("%08d",v[i]);
}
}
string sprint() const
{
stringstream ss;ss.str("");
for(INT i=v.size()-1;i>=0;i--) ss<<v[i];
return ss.str();
}
ubigint operator = (int number)
{
v.clear();
v.push_back(number);
return *this;
}
ubigint operator = (long long number)
{
v.clear();
do
{
INT lastInt=number%BASE;
v.push_back(lastInt);
}
while(number/=BASE);
return *this;
}
ubigint operator = (string number)
{
v.clear();
while(number.length()>8)
{
INT nowInt,len=number.length();
sscanf(number.substr(len-8).c_str(),"%d",&nowInt);
v.push_back(nowInt);
number=number.substr(0,len-8);
}
INT nowInt;
sscanf(number.substr(0).c_str(),"%d",&nowInt);
v.push_back(nowInt);
}
void input()
{
string number;
cin>>number;
v.clear();
while(number.length()>8)
{
INT nowInt,len=number.length();
sscanf(number.substr(len-8).c_str(),"%d",&nowInt);
v.push_back(nowInt);
number=number.substr(0,len-8);
}
INT nowInt;
sscanf(number.substr(0).c_str(),"%d",&nowInt);
v.push_back(nowInt);
}
bool operator < (ubigint b) const
{
if(v.size()!=b.v.size()) return v.size()<b.v.size();
for(INT i=v.size()-1;i>=0;i--)
if(v[i]!=b.v[i]) return v[i]<b.v[i];
return false;
}
bool operator > (ubigint b) const
{
return b<(*this);
}
bool operator >= (ubigint b) const
{
return !((*this)<b);
}
bool operator <= (ubigint b) const
{
return !((*this)>b);
}
bool operator == (ubigint b) const
{
return (!(b<(*this)))&&(!((*this)<b));
}
ubigint operator + (ubigint b) const
{
ubigint ans;INT last=0;
ans.v.clear();
for(INT i=0;i<max(v.size(),b.v.size());i++)
{
INT np=v[i]+b.v[i]+last;
last=np/BASE;
ans.v.push_back(np%BASE);
}
if(last!=0) ans.v.push_back(last);
return ans;
}
ubigint operator += (ubigint b)
{
*this=*this+b;
return *this;
}
ubigint operator - (ubigint b) const
{
if(*this<b) return 0;
if(*this==b) return 0;
ubigint ans;INT inp=0,nans=0;
ans.v.clear();
for(INT i=0;i<v.size();i++)
{
nans=v[i];
if(i<b.v.size()) nans-=b.v[i];
nans-=inp;inp=0;
if(nans<0)
{
inp=1;
nans+=BASE;
}
ans.v.push_back(nans);
}
if(inp!=0) return 0;
return ans;
}
ubigint operator -= (ubigint b)
{
*this=*this-b;
return *this;
}
ubigint operator << (int xx) const
{
if(xx==0) return *this;
string ts=sprint();
for(int i=0;i<xx;i++) ts+='0';
return ubigint(ts);
}
ubigint operator <<= (int xx)
{
if(xx==0) return *this;
string ts=sprint();
for(int i=0;i<xx;i++) ts+='0';
return *this=ts;
}
ubigint operator * (int b) const
{
INT last=0;
ubigint ans;ans.v.clear();
for(INT i=0;i<v.size();i++)
{
long long nw=1ll*v[i]*b+last;
last=nw/BASE;
ans.v.push_back(int(nw%BASE));
}
if(last!=0) ans.v.push_back(last);
return ans;
}
ubigint operator * (ubigint b) const
{
ubigint ans;
for(int i=0;i<v.size();i++)
{
int last=0;
for(int j=0;j<b.v.size();i++)
{
long long t=v[i]*b.v[j]+last;
last=t/BASE;
t%=BASE;
ubigint buf=t<<(i+j)*8;
}
}
}
};

int main()
{
ubigint a,b;
a.input();b.input();
ubigint c=a+b;
c.print();
return 0;
}

ST 表

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
#define maxn 2000005
template<typename T>
struct ST_table{
T f[maxn][25];
int logn[maxn],n;
ST_table()
{
n=0;
memset(f,0,sizeof(f));logn[0]=logn[1]=0,logn[2]=1;
for(int i=3;i<=2000000;i++) logn[i]=logn[i/2]+1;
}
void init()
{
for(int j=1;j<=21;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
void clear()
{
n=0;
memset(f,0,sizeof(f));logn[0]=logn[1]=0,logn[2]=1;
for(int i=3;i<=2000000;i++) logn[i]=logn[i/2]+1;
}
void push(T x)
{
f[++n][0]=x;
}
T query(int x,int y)
{
return max(f[x][logn[y-x+1]],f[y-(1<<logn[y-x+1])+1][logn[y-x+1]])
}
};

并查集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#define maxn 10005
struct unic{
int fa[maxn];
int father(int x)
{
if(fa[x]!=x) return fa[x]=father(fa[x]);
return x;
}
void unionn(int x,int y)
{
fa[father(x)]=father(y);
}
void check(int x,int y)
{
return father(x)==father(y);
}
};

线段树

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

#define maxn 1000005
template<typename T>
struct segmt{
struct tree{int l,r,lc,rc;T ans,lazy1,lazy2;}tr[2*maxn];
T a[maxn];int tot,n,m;T p;
inline int bt(int l,int r)
{
int pos=++tot;
tr[pos].l=l;tr[pos].r=r;
tr[pos].lazy1=1;tr[pos].lazy2=0;
if(l==r)
{
tr[pos].lc=tr[pos].rc=-1;
tr[pos].ans=a[l];
}
else
{
tr[pos].lc=bt(l,l+r>>1);
tr[pos].rc=bt((l+r>>1)+1,r);
tr[pos].ans=(tr[tr[pos].lc].ans+tr[tr[pos].rc].ans)%p;
}
return pos;
}
void times(int,int,int,int);
void add(int,int,int,int);
inline void update(int pos)
{
if(tr[pos].lazy1!=1)
{
times(tr[pos].lc,tr[tr[pos].lc].l,tr[tr[pos].lc].r,tr[pos].lazy1);
times(tr[pos].rc,tr[tr[pos].rc].l,tr[tr[pos].rc].r,tr[pos].lazy1);
tr[pos].lazy1=1;
}
if(tr[pos].lazy2)
{
add(tr[pos].lc,tr[tr[pos].lc].l,tr[tr[pos].lc].r,tr[pos].lazy2);
add(tr[pos].rc,tr[tr[pos].rc].l,tr[tr[pos].rc].r,tr[pos].lazy2);
tr[pos].lazy2=0;
}
tr[pos].ans=(tr[tr[pos].lc].ans+tr[tr[pos].rc].ans)%p;
}
inline void times(int pos,int l,int r,T x)
{
if(tr[pos].l==l&&tr[pos].r==r)
{
tr[pos].lazy1=tr[pos].lazy1*x%p;
tr[pos].lazy2=tr[pos].lazy2*x%p;
tr[pos].ans=tr[pos].ans*x%p;
return;
}
update(pos);
if(r<=tr[tr[pos].lc].r) times(tr[pos].lc,l,r,x);
else if(l>=tr[tr[pos].rc].l) times(tr[pos].rc,l,r,x);
else
{
times(tr[pos].lc,l,tr[tr[pos].lc].r,x);
times(tr[pos].rc,tr[tr[pos].rc].l,r,x);
}
tr[pos].ans=(tr[tr[pos].lc].ans+tr[tr[pos].rc].ans)%p;
}
inline void add(int pos,int l,int r,T x)
{
if(tr[pos].l==l&&tr[pos].r==r)
{
tr[pos].lazy2=(tr[pos].lazy2+x)%p;
tr[pos].ans=(tr[pos].ans+x*(r-l+1)%p)%p;
return;
}
update(pos);
if(r<=tr[tr[pos].lc].r) add(tr[pos].lc,l,r,x);
else if(l>=tr[tr[pos].rc].l) add(tr[pos].rc,l,r,x);
else
{
add(tr[pos].lc,l,tr[tr[pos].lc].r,x);
add(tr[pos].rc,tr[tr[pos].rc].l,r,x);
}
tr[pos].ans=(tr[tr[pos].lc].ans+tr[tr[pos].rc].ans)%p;
}
inline int query(int pos,int l,int r)
{
if(tr[pos].l==l&&r==tr[pos].r) return tr[pos].ans;
update(pos);
if(r<=tr[tr[pos].lc].r) return query(tr[pos].lc,l,r);
else if(l>=tr[tr[pos].rc].l) return query(tr[pos].rc,l,r);
else return (query(tr[pos].lc,l,tr[tr[pos].lc].r)+query(tr[pos].rc,tr[tr[pos].rc].l,r))%p;
}
};

树状数组

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
#define maxn 500005
template<typename T>
struct Ltree{
T tr[maxn];
int n,m;
inline int lowbit(int x){return x&-x;}
inline void change(int x,T y)
{
while(x<=n)
{
tr[x]+=y;
x+=lowbit(x);
}
}
inline T query(int x)
{
T ans=0;
while(x>0)
{
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
inline T query(int x,int y)
{
return query(y)-query(x-1);
}
};

普通平衡树

1
2
3
4
5
6
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
#define Tree tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>