版本比较
模板集
正文
增加 373 行删除 6 行
4 行未改动5678991011121314151617181919202122232424252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707139 行未改动111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181117 行未改动全部展开29930030130220130330430530630760 行未改动36836937037127037237337437537637737837938038138238338438538638738838939039139239339439522 行未改动41841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247314 行未改动48848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664732 行未改动680681682683372684685686687688664 行未改动全部展开135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141348 行未改动146214631464146514661467146814691470147134 行未改动4 行未改动~~非全部原创(可能也没有原创)~~
~~别问为什么zsyun_Cg这么懒~~
目前23模板,持续更新中目前29模板,持续更新中,可借走模板,但不许说是自己的
## 模板集
::::success[代码框架]
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e5+10,M=1e3+10;const int N=1e6+10,M=1e3+10;
int main(){ //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); return 0;}```
:::success[复杂通用版]持续更新中
```cpp#include<bits/stdc++.h>#define ll long long#define endl '\n'using namespace std;const int N=1e6+10,M=1e3+10;const int MOD_1=998244353,MOD_2=1000000000+7,MOD_3=1000000000+9;const int INV2_1=499122177,INV2_2=500000004,INV2_3=500000005;bool ctrl_debug(int fin){ static bool f=1; if(fin==3) return f; else f=bool(fin); return 1;}//控制debug,1为输出,0为不输出 template<typename T>void debug(T a){ if(ctrl_debug(3)){ cout<<"debug: "<<a<<'\n'; //↓本地不报错,评测机报错 //system("pause"); } return;}int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); ctrl_debug(1); return 0;}```
:::
::::
::::success[快读快写]
39 行未改动:::
::::
::::success[对拍]
:::success[随机数生成器]
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e6+10,M=1e3+10;
int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); srand(time(0)); cout<<rand()%100<<" "<<rand()%100<<"\n";//输出两个100以内的随机数 return 0;}```
* 先在前面调用`srand(time(0));`* 然后用`rand()%n`生成随机数,n-1为最大值
:::
:::success[对拍器]
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e6+10,M=1e3+10;
int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); ll t=0; while(1){ cout<<"test: "<<t++<<"\n"; system("随机数生成器.exe > data.in"); system("暴力代码.exe < data.in > std.out"); system("待测代码.exe < data.in > ab.out"); if(system("fc std.out ab.out > diff.log")){ cout<<"WA\n"; break; } cout<<"AC\n"; } return 0;}```
* 代码中所有 `.exe`均为`.cpp`文件编译产生的可执行文件* 四个`.cpp`须在同一目录下* 运行对拍器会生成4个文件,两`.out`文件为两个程序的输出,`.in`为本组随机样例,`diff`文件为两个`.out`的集合
:::
[例示](https://www.luogu.com.cn/problem/U746695)
::::
::::success[背包DP]
:::success[01背包]
117 行未改动全部展开 else{ l=mid+1; } } cout<<l; cout<<--l;}```
:::60 行未改动```cpp//建队//初始化//入队第一个元素//while循环知道队列为空//while循环直到队列为空 //判断 //更新 //入队```
注:BFS非递归
::::
::::success[逆元]
模板在下面快速幂里
a/b%MOD=a*(b在模MOD下的逆元)%MOD
相当于在整除+取模意义下的倒数(主要与正常倒数区分)
::::
::::success[快速幂]
```cpp#include<bits/stdc++.h>22 行未改动 cout<<fast_pow(a,b,modd);//a^b%modd; return 0;}```
:::success[求逆元]
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e5+10,M=1e3+10;const int MOD_1=998244353,MOD_2=1e9+7,MOD_3=1e9+9;const int INV2_1=499122177,INV2_2=500000004,INV2_3=500000005;ll n,m;ll qpow(ll a,ll b,ll mod){ ll res=1; a%=mod; while(b){ if(b&1) res=res*a%mod; a=a*a%mod; b>>=1; } return res;}ll inv(ll a,ll mod){//快速幂求逆元 return qpow(a,mod-2,mod);}ll zuhe(ll n,ll m,ll mod){//组合数 if(m>n){ return 0; } ll num=1,den=1; for(int i=0;i<m;i++){ num=num*(n-i)%mod; den=den*(i+1)%mod; } return num*inv(den,mod)%mod;}int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; cout<<zuhe(n,m,MOD_2);//组合数 return 0;}```
:::
::::
::::success[数论分块]
14 行未改动}```::::
::::success[分块(懒标记)]
```cpp#include<bits/stdc++.h>//#define AC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);#define int long longusing namespace std;const int N=1e6+10;int n,a[301000],blo,cnt,l[301000],r[301000],bel[301000],tag[301000];//blo:块大小 cnt:块数 l[i]:第i块左边界 r[i]:第i块右边界 //bel[i]:节点i所属块 tag[i]:第i块的懒标记//分块预处理void build(int n){ blo=sqrt(n);//块大小 cnt=(n+blo-1)/blo;//块数 for(int i=1;i<=cnt;i++){ l[i]=(i-1)*blo+1; r[i]=i*blo; } r[cnt]=n;//最后一块右边界为n for(int i=1;i<=n;i++){ bel[i]=(i-1)/blo+1; }}//区间加void add(int L,int R,int k){ //同一块内暴力加 if(bel[L]==bel[R]){ for(int i=L;i<=R;i++){ a[i]+=k; } return; } //左边零散块暴力加 for(int i=L;i<=r[bel[L]];i++){ a[i]+=k; } //右边零散块暴力加 for(int i=l[bel[R]];i<=R;i++){ a[i]+=k; } //中间完整块打懒标记 for(int i=bel[L]+1;i<bel[R];i++){ tag[i]+=k; }}//单点查询int query(int x){ return a[x]+tag[bel[x]];//原值加上所在块的懒标记}signed main(){ //AC; ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; //读入原数组 for(int i=1;i<=n;i++){ cin>>a[i]; } build(n);//预处理分块 //处理操作 for(int i=1;i<=n;i++){ int opt,l,r,c; cin>>opt>>l>>r>>c; if(opt==1){//操作1:单点查询 cout<<query(r)<<"\n"; } else{//操作0:区间加 add(l,r,c); } } return 0;}```::::
::::success[LIS最长上升子序列]
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=5e3+10,M=1e3+10;int n,a[N],dp[N],ma=INT_MIN;//dp[i]为LIS长度为i时结尾的最小值bool cheak(int mid,int x){ return mid>=x;}int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=n;i++){ dp[i]=INT_MAX; } for(int i=1;i<=n;i++){ int l=1,r=n,mid=0; while(l<=r){//二分找到最优的修改位置 mid=(l+r)>>1; if(cheak(dp[mid],a[i])==1){ r=mid-1; } else{ l=mid+1; } } ma=max(ma,l); dp[l]=min(a[i],dp[l]); for(int j=1;j<=n;j++){ //cout<<dp[j]<<" "; } //cout<<"\n"<<i<<" "<<l<<"\n"; } cout<<ma; return 0;}```
::::
::::success[最大子段和]
太简单,不做介绍
```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e6+10,M=1e3+10;ll n,sum=0,ma=INT_MIN;int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n; for(int i=1,x;i<=n;i++){ cin>>x; sum+=x; ma=max(ma,sum); if(sum<=0){ sum=0; } } cout<<ma; return 0;}```
::::
::::success[并查集]
```cppint fa[200005],h[200005];32 行未改动}```::::
::::success[链世前向星]::::success[链式前向星]
```cpp#include<bits/stdc++.h>#define ll long long664 行未改动全部展开::::
:::::
::::success[树状数组]```cpp#include<bits/stdc++.h>#define ll long longusing namespace std;const int N=1e6+10,M=1e3+10;int n,m,tree[N],a[N];//tree:树状数组 a:原数组//lowbit:求x二进制下最低位1所对应的值int lowbit(int x){ return x&(-x);}//单点修改:在pos位置加上valvoid update(int pos,int val){ while(pos<=n){//向上更新父节点 tree[pos]+=val; pos+=lowbit(pos); }}//前缀和查询:求1~pos的和int sum(int pos){ int ans=0; while(pos>0){//向下累加区间和 ans+=tree[pos]; pos-=lowbit(pos); } return ans;}int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; //建树 for(int i=1;i<=n;i++){ cin>>a[i]; update(i,a[i]);//初始化树状数组 } for(int i=1;i<=m;i++){ int op,x,y; cin>>op>>x>>y; if(op==1){//操作1:单点修改 update(x,y); } else{//操作2:区间查询 cout<<sum(y)-sum(x-1)<<"\n"; } } return 0;}```
::::
::::success[ST表]
```cpp#include<bits/stdc++.h>48 行未改动int n,cnt=0;int prime[N];//存质数,可用vector bool vis[N];//标记是否为合数 void xian_xing_shai(int n){//线性筛 vis[0]=1; vis[1]=1; for(int i=2;i<=n;i++){//遍历 if(!vis[i]){//是质数 prime[++cnt]=i;//加入质数表 }34 行未改动