快速读入模板
inline long long read(){
long long s=0,f=1;
char ch=getchar();
while((ch<'0'||ch>'9')&&ch!=EOF){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';
ch=getchar();
}
return s*f;
}
快速输出模板
char f[200] ;
inline void prt(long long x)
{
long long tmp=x>0?x:-x;
if(x<0)putchar('-');
long long cnt=0;
while(tmp>0)
{
f[cnt++]=tmp%10+'0';
tmp/=10;
}
while(cnt>0)putchar(f[--cnt]);
}
快速幂
int quickpow(int a,int b){
int s=1;
while(b){
if(b&1)s=(s*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return s%mod;
}
无优化SPFA模板
#include<bits/stdc++.h>
using namespace std;
long long dist[10100],u[500001],v[500001],w[500001],n,m,s,c;
#define MAXN 2147483647;
int main()
{
cin>>n;
cin>>m;
cin>>s;
for(register int i=1;i<=m;i++){
cin>>u[i];
cin>>v[i];
cin>>w[i];
}
for(int i=1;i<=n;i++){
dist[i]=MAXN;
}
dist[s]=0;
for(int k=1;k<=n-1;k++){
c=0;
for(int i=1;i<=m;i++)if(dist[v[i]]>dist[u[i]]+w[i]){
dist[v[i]]=dist[u[i]]+w[i];
c=1;
}
if(!c)break;
}
for(int i=1;i<=n;i++)printf("%lld ",dist[i]);
return 0;
}
单调队列处理模板
#include<bits/stdc++.h>
using namespace std;
int n,m;
int q1[1000001],q2[1000001];
int a[1000001];
int minque(){
int h=1,t=0;
for(int i=1;i<=n;i++){
while(h<=t&&q1[h]+m<=i)h++;
while(h<=t&&a[i]<a[q1[t]])t--;
q1[++t]=i;
if(i>=m) printf("%d ",a[q1[h]]);
}
cout<<endl;
}
int maxque(){
int h=1,t=0;
for(int i=1;i<=n;i++){
while(h<=t&&q2[h]+m<=i) h++;
while(h<=t&&a[i]>a[q2[t]]) t--;
q2[++t]=i;
if(i>=m) printf("%d ",a[q2[h]]);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
minque();
maxque();
return 0;
}
无优化Dijkstra模板
#include<bits/stdc++.h>
#define MAXN 0x7fffffff
using namespace std;
int dist[5001],n,m,edge[5001][5001],minn,idx,s;
bool vis[5001];
void dijkstra(){
for(int j=1;j<n;++j){
minn=MAXN;
for(int i=1;i<=n;i++){
if(!vis[i]&&dist[i]<minn){
minn=dist[i];
idx=i;
}
}
vis[idx]=1;
for(int i=1;i<=n;i++){
if(edge[idx][i]!=MAXN){
int t=edge[idx][i]+dist[idx];
if(dist[i]>t)dist[i]=t;
}
}
}
}
int main()
{
cin>>n>>m>>s;
for(int i=1;i<=n;i++)for(int j=1;j<=n;++j){
if(i!=j)edge[i][j]=MAXN;
}
for(int i=0;i<m;i++){
int x,y,len;
cin>>x>>y>>len;
edge[x][y]=len;
}
for(int i=1;i<=n;++i){
dist[i]=edge[s][i];
}
vis[s]=1;
dijkstra();
for(int i=1;i<=n;i++)cout<<dist[i]<<' ';
return 0;
}
并查集模板
inline int find(int x){
while(x!=fa[x]){
x=fa[x];
fa[x]=fa[fa[x]];
}
return x;
}
inline void merge(int v,int u) {
int t1,t2;
t1=find(v);
t2=find(u);
if(t1!=t2)fa[t2]=t1;
}
inline void init(){
for(i=1;i<=n;i++)fa[i]=i;
}
手写快排模板
void qsort(int l,int r){
int mid=a[(l+r)/2];
int i=l,j=r;
do{
while(a[i]<mid)i++;
while(a[j]>mid)j--;
if(i<=j){
swap(a[i],a[j]);
i++;
j--;
}
}while(i<=j);
if(l<j)qsort(l,j);
if(i<r)qsort(i,r);
}
优化八聚氧
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(3,"Ofast","inline")
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("-fgcse")
%:pragma GCC optimize("-fgcse-lm")
%:pragma GCC optimize("-fipa-sra")
%:pragma GCC optimize("-ftree-pre")
%:pragma GCC optimize("-ftree-vrp")
%:pragma GCC optimize("-fpeephole2")
%:pragma GCC optimize("-ffast-math")
%:pragma GCC optimize("-fsched-spec")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("-falign-jumps")
%:pragma GCC optimize("-falign-loops")
%:pragma GCC optimize("-falign-labels")
%:pragma GCC optimize("-fdevirtualize")
%:pragma GCC optimize("-fcaller-saves")
%:pragma GCC optimize("-fcrossjumping")
%:pragma GCC optimize("-fthread-jumps")
%:pragma GCC optimize("-funroll-loops")
%:pragma GCC optimize("-fwhole-program")
%:pragma GCC optimize("-freorder-blocks")
%:pragma GCC optimize("-fschedule-insns")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("-ftree-tail-merge")
%:pragma GCC optimize("-fschedule-insns2")
%:pragma GCC optimize("-fstrict-aliasing")
%:pragma GCC optimize("-fstrict-overflow")
%:pragma GCC optimize("-falign-functions")
%:pragma GCC optimize("-fcse-skip-blocks")
%:pragma GCC optimize("-fcse-follow-jumps")
%:pragma GCC optimize("-fsched-interblock")
%:pragma GCC optimize("-fpartial-inlining")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("-freorder-functions")
%:pragma GCC optimize("-findirect-inlining")
%:pragma GCC optimize("-fhoist-adjacent-loads")
%:pragma GCC optimize("-frerun-cse-after-loop")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("-finline-small-functions")
%:pragma GCC optimize("-ftree-switch-conversion")
%:pragma GCC optimize("-foptimize-sibling-calls")
%:pragma GCC optimize("-fexpensive-optimizations")
%:pragma GCC optimize("-funsafe-loop-optimizations")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC optimize("-fdelete-null-pointer-checks")