一些模板
归并排序
#include<iostream>
#include<cstdio>
const int N=5e5+100;
using namespace std;
int n,a[N],b[N];
long long num;
void msort(int l,int r)
{
if (l==r) return ;
int mid=(l+r)>>1;
msort(l,mid);
msort(mid+1,r);
int i=l,j=mid+1,k=l;
while (i<=mid&&j<=r)
if (a[i]<=a[j]) b[k++]=a[i++];
else
{
num=num+mid-i+1; // 求逆序对
b[k++]=a[j++];
}
while (i<=mid) b[k++]=a[i++];
while (j<=r) b[k++]=a[j++];
for (int i=l;i<=r;i++) a[i]=b[i];
return ;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",a+i);
msort(1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
printf("
");
printf("%lld",num); //输出逆序对
return 0;
}
Matrix
#include<bits/stdc++.h>
#define ll long long
struct Mat{
int size;
ll **M=NULL;
inline ll Start()
{
if (M!=NULL) return M[1][1];
else return LLONG_MIN;
}
inline void Clear(int sz)
{
if (M==NULL) {New(sz);return ;}
for (int i=0;i<=sz;i++)
for (int j=0;j<=sz;j++)
M[i][j]=0;
return ;
}
inline void New(int sz)
{
if (M!=NULL)
{
printf("
This matrix has been used
");
return ;
}
size=sz;
M=new ll*[sz+10];
for (int i=0;i<sz+10;i++)
M[i]=new ll[sz+10];
Clear(sz);
return ;
}
inline void Build(int sz)
{
size=sz;
if (M==NULL) New(sz);
for (int i=1;i<=sz;i++) M[i][i]=1;
return ;
}
inline void Init(ll now[],int sz)
{
if (M==NULL) New(sz);
int num=0;
for (int i=1;i<=sz;i++)
for (int j=1;j<=sz;j++)
M[i][j]=now[++num];
return ;
}
inline void Out()
{
if (M!=NULL)
for (int i=1;i<=size;i++)
{
for (int j=1;j<=size;j++)
printf("%lld ",M[i][j]);
printf("
");
}
return ;
}
inline void Delete()
{
for (int i=0;i<size+10;i++)
delete []M[i] ;
delete []M;
M=NULL;
size=0;
return ;
}
};
ll mod;
inline Mat operator * (Mat a,Mat b)
{
Mat c;
c.Clear(a.size);
for (int i=1;i<=c.size;i++)
for (int j=1;j<=c.size;j++)
for (int k=1;k<=c.size;k++)
c.M[i][j]=(c.M[i][j]+a.M[i][k]*b.M[k][j]%mod)%mod;
return c;
}
inline Mat Mat_qpow(Mat a,ll p)
{
Mat ans,base;
base.New(a.size);
ans.Build(a.size);
for (base=a;p;p>>=1,base=base*base)
if (p&1) ans=ans*base;
return ans;
}
int main()
{
return 0;
}
网络流模板
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1e4+100,M=1e5+100;
struct edge{
int s,e,v,next;
}ed[(M<<1)];
queue<int>q;
int n,m,s,t,tot=1;
int head[N],deep[N],cur[N];
inline bool bfs(int s)
{
memset(deep,0,sizeof(deep));
for (int i=1;i<=n;i++)
cur[i]=head[i];
q.push(s);
deep[s]=1;
while (!q.empty())
{
int u=q.front();
q.pop();
for (int i=head[u];i;i=ed[i].next)
{
int to=ed[i].e;
if (ed[i].v&&!deep[to])
{
deep[to]=deep[ed[i].s]+1;
q.push(to);
}
}
}
return deep[t];
}
inline int dfs(int x,int flow)
{
if (x==t) return flow;
int out=0;
for (int i=cur[x];i;i=ed[i].next)
{
cur[x]=i;
int to=ed[i].e;
if (ed[i].v&&deep[to]==deep[ed[i].s]+1)
{
int res=dfs(to,min(ed[i].v,flow));
ed[i].v-=res;
ed[i^1].v+=res;
flow-=res;
out+=res;
if (!flow) break;
}
}
if (!out) deep[x]=0;
return out;
}
inline void add(int s,int e,int v)
{
ed[++tot]=(edge){s,e,v,head[s]};
head[s]=tot;
return ;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for (int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,0);
}
int maxflow=0;
while (bfs(s))
maxflow+=dfs(s,INT_MAX);
printf("%d
",maxflow);
return 0;
}