版本比较
一个fvv的模版库
正文
增加 309 行删除 28 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717282 行未改动155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244124 行未改动全部展开369370371372226227228229230231232233373374375376377378379380381236382383238239240241242243244245246247248249250251252253254384385386387388389390391392393394395396397398399400401402403257404405406407408139 行未改动全部展开548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653225 行未改动全部展开879880881882639883884885886887349 行未改动全部展开12371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280::::info[其他 with $\KaTeX$]
## 快速读入模板```cpp#include<bits/stdc++.h>using namespace std;inline int read(){ int x=0,f=1; char ch=getchar_unlocked(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar_unlocked(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar_unlocked(); } return x*f;}signed main(){ long long n,a=0; n=read(); while(n--)a+=read(); cout<<a;
return 0;}
```## 数组离散化模板
```cpp#include<bits/stdc++.h>using namespace std;int a[100010];signed main(){ ios::sync_with_stdio(0); cin.tie(0); int T; cin>>T; while(T--){ memset(a,0,sizeof a); int n; cin>>n; vector<int>b; for(int i=1;i<=n;i++){ cin>>a[i]; b.push_back(a[i]); } sort(b.begin(),b.end()); b.erase(unique(b.begin(),b.end()),b.end()); for(int i=1;i<=n;i++){ a[i]=lower_bound(b.begin(),b.end(),a[i])-b.begin()+1; } for(int i=1;i<=n;i++)cout<<a[i]<<' '; cout<<"\n"; }
return 0;}
```
## 字符串哈希模版(自然溢出)```cpp#include <bits/stdc++.h>using namespace std;82 行未改动5. 返回 best*/```
## 三分模板```cpp#include<bits/stdc++.h>using namespace std;using d=double;const int N=10010;int n;d a[N],b[N],c[N];d f(d x){ d mx=-1e18; for(int i=0;i<n;i++) mx=max(mx,a[i]*x*x+b[i]*x+c[i]); return mx;}signed main(){ ios::sync_with_stdio(0); cin.tie(0); int T; cin>>T; while(T--){ cin>>n; for(int i=0;i<n;i++)cin>>a[i]>>b[i]>>c[i]; d l=0,r=1000; for(int i=0;i<100;i++){ d f1=l+(r-l)/3.0; d f2=r-(r-l)/3.0; if(f(f1)<f(f2))r=f2; else l=f1; } printf("%.4lf\n",f(l)); }
return 0;}
```
## ST表模版```cpp#include<bits/stdc++.h>using namespace std;#define int long longconst int N=100005;const int K=20;int n,m;int st[N][K];inline int read(){ int x=0,f=1; char ch=getchar_unlocked(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar_unlocked(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar_unlocked(); } return x*f;}signed main(){ n=read(); m=read(); for(int i=1;i<=n;i++) st[i][0]=read(); for(int j=1;j<=17;j++){ for(int i=1;i+(1<<j)-1<=n;i++){ st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]); } } while(m--){ int l=read(),r=read(); int k=log2(r-l+1); int ans=max(st[l][k],st[r-(1<<k)+1][k]); printf("%lld\n",ans); } return 0;}```
::::
::::info[图论系列模版 with $\KaTeX$]
124 行未改动全部展开#include<bits/stdc++.h>using namespace std;const int N = 100010;int cnt = 1;int head[N], to[N], w[N], nxt[N], dp[N],in[N], out[N];void edge(int u, int v, int cost) { to[cnt] = v; w[cnt] = cost; nxt[cnt] = head[u]; head[u] = cnt++; in[v]++; out[u]++;int head[N], to[N], w[N], nxt[N],in[N], out[N];void edge(int u, int v) { to[cnt] = v; nxt[cnt] = head[u]; head[u] = cnt++; in[v]++; out[u]++;}int main() { ios::sync_with_stdio(false); ios::sync_with_stdio(false); cin.tie(nullptr); queue<int> q; int n, m; cin >> n >> m; dp[1] = 0; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; edge(x, y, z); } for (int i = 1; i <= n; i++) if (!in[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; in[v]--; if (!in[v]) q.push(v); queue<int> q; int n,x; cin >> n; memset(head, -1, sizeof(head)); for(int i = 1; i <= n; i++) { while(cin >> x && x != 0) { edge(i, x); } } for (int i = 1; i <= n; i++) if (!in[i]) q.push(i); while (!q.empty()) { int u = q.front(); cout<<u<<' '; q.pop(); for (int i = head[u]; i!=-1; i = nxt[i]) { int v = to[i]; in[v]--; if (!in[v]) q.push(v); } } return 0; return 0;}```## 链式前向星+dijstra模版(堆优化):
139 行未改动全部展开}
```## 负环模板
```cpp#include<bits/stdc++.h>using namespace std;const int MAXN = 100010;const int MAXM = 500010;const int INF = 0x3f3f3f3f;int head[MAXN], to[MAXM], w[MAXM], nxt[MAXM], cnt = 1,dis[MAXN],timex[MAXN];int n, m, s;bool inq[MAXM];void edge(int u, int v, int cost) { to[cnt] = v; w[cnt] = cost; nxt[cnt] = head[u]; head[u] = cnt++;}bool SPFA(int s){ queue<int>q; memset(dis,INF,sizeof dis); q.push(s); inq[s]=1; dis[s]=0; while(!q.empty()){ int u=q.front(); q.pop(); inq[u]=0; for(int i=head[u];i;i=nxt[i]){ int v=to[i],cost=w[i]; if(dis[v]>dis[u]+cost){ dis[v]=dis[u]+cost; if(!inq[v]){ q.push(v); inq[v]=1; timex[v]++; if(timex[v]>n)return 1; } } } } return 0;}int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin>>T; while(T--){ cnt=1; memset(head,0,sizeof(head)); memset(timex, 0, sizeof(timex)); memset(inq, 0, sizeof(inq)); cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; if(z>=0)edge(y, x, z); edge(x, y, z); } if(SPFA(1)) cout << "YES\n"; else cout << "NO\n"; } return 0;}
```
## 传递闭包模版
```cpp#include<bits/stdc++.h>using namespace std;int n,m,dis[105][105];const int INF=0x3f3f3f;int main(){ cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>dis[i][j]; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[i][j] |= (dis[i][k]&dis[k][j]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) cout<<dis[i][j]<<' '; puts(" "); } return 0;}
```
## tarjan强连通分量+链式前向星模版:
```cpp#include <bits/stdc++.h>225 行未改动全部展开 int d=dep[u]-dep[v]; for(int j=19;j>=0;j--)if(d>>j&1)u=fa[u][j]; if(u==v)return u; for(int j=19;j>=0;j--) if(fa[u][j]!=fa[v][j])u=fa[u][j],u=fa[v][j]; if(fa[u][j]!=fa[v][j])u=fa[u][j],v=fa[v][j]; return fa[u][0];}int main(){ ios::sync_with_stdio(0);349 行未改动全部展开 cout<<ans<<endl; return 0;}```## 拓展欧几里得模板
```cppint exgcd(int a,int b,int &x,int &y){ if(b==0){ x=1,y=0; return a; } int d=exgcd(b,a%b,y,x); y-=(a/b)*x; return d;}
```
## 裴蜀定理模板
```cpp#include<bits/stdc++.h>using namespace std;signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n,x,ans=0; cin>>n; for(int i=1;i<=n;i++){ cin>>x; ans=__gcd(ans,abs(x)); } cout<<ans;
return 0;}
```
```