版本比较
一个fvv的模版库
正文
增加 331 行删除 46 行
12345642 行未改动4950515253545556575859606162636465666768697071727311 行未改动858687888990919293124 行未改动全部展开218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257139 行未改动全部展开397398399400401402403404405222 行未改动全部展开62862963063163263363463563663763863999 行未改动739740741742743744745746747241 行未改动全部展开989990991992993994995::::info[其他 with $\KaTeX$]## 字符串哈希模版(自然溢出)```cpp#include <bits/stdc++.h>using namespace std;42 行未改动// 退火参数double t = 10000; // t: 当前温度(初始温度 T0,范围 10^3 ~ 10^5)double a = 0.99; // a: 降温系数 α(范围 0.95 ~ 0.999)double tk = 1e-8; // tk: 终止温度 Tk(范围 10^-8 ~ 10^-15)while (t > tk) { // 当温度高于终止温度时循环// 邻域扰动double nx = c + (rand() % 200 - 100) * t / 10000; // nx: 邻域解double nxf = f(nx); // nxf: 邻域解的能量值double d = nxf - cf; // d: 能量差 Δ = f(nxt) - f(cur)// Metropolis准则if (d < 0 || exp(-d / t) > (double)rand() / RAND_MAX) { // 若 Δ<0 或 概率接受c = nx; // 接受邻域解:更新当前解cf = nxf; // 更新当前解的能量值}// 更新全局最优if (cf < bf) { // 若当前解能量更小bf = cf; // 更新历史最优能量值b = c; // 更新历史最优解}// 降温t *= a; // 温度乘以降温系数}// 输出最优解printf("%.6lf %.6lf", b, bf); // 输出历史最优解 b 和其能量值 bfreturn 0;11 行未改动5. 返回 best*/```::::::::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 main() {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);}}return 0;}```## 链式前向星+dijstra模版(堆优化):139 行未改动全部展开}```## tarjan强连通分量+链式前向星模版:```cpp#include <bits/stdc++.h>222 行未改动全部展开}int lca(int u,int v){if(dep[u]<dep[v])swap(u,v);int d=dep[u]-dep[v];for(int j=19;j>=0;j--)if(diff>>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];return fa[u][0];}int main(){ios::sync_with_stdio(0);99 行未改动return res;}void update(int p,int l,int r,int i,int x){//修改单点if(l==r){sum[p]=x;return;}int mid=(l+r)/2;if(i<=mid)update(p*2,l,mid,i,x);241 行未改动全部展开cout<<ans<<endl;return 0;}``````12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717242 行未改动11511611711811912012112212312412512612712812913013113213313413513613713813914014114214311 行未改动155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244124 行未改动全部展开369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408139 行未改动全部展开548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653222 行未改动全部展开87687787887988088188288388488588688799 行未改动987988989990991992993994995241 行未改动全部展开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;42 行未改动// 退火参数double t = 10000; // t: 当前温度(初始温度 T0,范围 10^3 ~ 10^5)double a = 0.99; // a: 降温系数 α(范围 0.95 ~ 0.999)double tk = 1e-8; // tk: 终止温度 Tk(范围 10^-8 ~ 10^-15)int st = clock();while ((clock() - st) / CLOCKS_PER_SEC < 0.90) {//运行到900ms再停止t=10000; //可重置while (t > tk) { // 当温度高于终止温度时循环// 邻域扰动double nx = c + (rand() % 200 - 100) * t / 10000; // nx: 邻域解double nxf = f(nx); // nxf: 邻域解的能量值double d = nxf - cf; // d: 能量差 Δ = f(nxt) - f(cur)// Metropolis准则if (d < 0 || exp(-d / t) > (double)rand() / RAND_MAX) { // 若 Δ<0 或 概率接受c = nx; // 接受邻域解:更新当前解cf = nxf; // 更新当前解的能量值}// 更新全局最优if (cf < bf) { // 若当前解能量更小bf = cf; // 更新历史最优能量值b = c; // 更新历史最优解}// 降温t *= a; // 温度乘以降温系数}}// 输出最优解printf("%.6lf %.6lf", b, bf); // 输出历史最优解 b 和其能量值 bfreturn 0;11 行未改动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],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);cin.tie(nullptr);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;}```## 链式前向星+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>222 行未改动全部展开}int lca(int u,int v){if(dep[u]<dep[v])swap(u,v);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],v=fa[v][j];return fa[u][0];}int main(){ios::sync_with_stdio(0);99 行未改动return res;}void update(int p,int l,int r,int i,int x){//修改单点if(l==r){sum[p]+=x;return;}int mid=(l+r)/2;if(i<=mid)update(p*2,l,mid,i,x);241 行未改动全部展开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;}``````