模板

orz hhw posted @ 2015年10月10日 19:52 in 做题记录 with tags 模板 , 3839 阅读

最近切题总是要套模板,而找模板太烦了,于是把模板整理一下贴出来

一、基本

1.读入优化★★★★

正数
1
2
3
4
void read(int &x){
    for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
    for(x=0;ch>='0';ch=getchar())x=x*10+ch-'0';
}

2.输出优化☆

3.离散化★★☆ 小心使用

4.二分★★★ 小心使用

二分
1
for(l=1,r=n;l<=r;)if(ok(mid=l+r>>1))r=mid-1,ans=mid;else l=mid+1;

5.高精度★★☆

二、图论

1.邻接表★★★★★

邻接表
1
void ins(int x,int y){la[++tot]=y;ne[tot]=fir[x];fir[x]=tot;}
邻接表
1
void ins(int x,int y,int z){la[++tot]=y;va[tot]=z;ne[tot]=fir[x];fir[x]=tot;}

2.SPFA★★★★ 注意标记v[x]=0

SPFA
1
for(memset(d,63,sizeof(d)),d[q[t=1]=1]=0,v[1]=1;h^t;)for(i=fir[x=q[++h]],v[x]=0;i;i=ne[i])if(d[x]+va[i]<d[y=la[i]])if(d[y]=d[x]+va[i],!v[y])v[q[++t]=y]=1;
SPFA
1
2
3
4
5
6
7
8
9
10
11
struct SPFA{
    int n,h,t,S,tot,q[N],d[N],fir[N],la[M],ne[M],va[M];bool v[N];
    void in(){tot=0;memset(d,63,sizeof d);CL(v);CL(fir);}
    void ins(int x,int y,int z){la[++tot]=y;ne[tot]=fir[x];fir[x]=tot;va[tot]=z;}
    void work(){
        int i,x,y;
        for(h=d[q[t=1]=S]=0;h^t;)
            for(i=fir[x=q[h=h%n+1]],v[x]=0;i;i=ne[i])
                if(d[x]+va[i]<d[y=la[i]])if(d[y]=d[x]+va[i],!v[y])v[q[t=t%n+1]=y]=1;
    }
}G;

3.Dijsktra+堆★★ 

Dijkstra+堆
1
2
3
4
5
6
7
8
9
10
11
12
struct Dijkstra{
    #define CL(a) memset(a,0,sizeof a)
    int h,t,S,tot,d[N],fir[N],la[M],ne[M],va[M],q[N];bool v[N];
    struct W{int x,z;bool operator<(W a)const{return a.z<z;}};priority_queue<W>Q;
    void in(){tot=0;CL(fir);CL(v);}
    void ins(int x,int y,int z){la[++tot]=y;va[tot]=z;ne[tot]=fir[x];fir[x]=tot;}
    void work(){
        int i,x,y;for(i=1;i<=T;i++)d[i]=1e9;
        for(Q.push(W{1,d[h=S]=t=0});!Q.empty();)
            if(x=Q.top().x,Q.pop(),!v[x])for(v[x]=1,i=fir[x];i;i=ne[i])if(d[x]+va[i]<d[la[i]])Q.push(W{la[i],d[la[i]]=d[x]+va[i]});
    }
}G;

4.二分图★★

5.KM☆

6.ISAP★★★★

ISAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct MaxFlow{
    int S,T,tot,w,fir[N],cur[N],pre[N],d[N],nb[N],va[M],la[M],ne[M];
    #define CL(a) memset(a,0,sizeof a)
    void in(){tot=1;CL(fir);CL(d);CL(nb);CL(pre);}
    void ins(int x,int y,int fl){
        la[++tot]=y;ne[tot]=fir[x];va[tot]=fl;fir[x]=tot;
        la[++tot]=x;ne[tot]=fir[y];va[tot]=0;fir[y]=tot;
    }
    int FLOW(){
        int i,u,V,fl=0;
        for(i=1;i<=T;i++)cur[i]=fir[i];
        for(u=S,nb[0]=T;d[S]<T;){
            if(u==T){
                for(V=1e9,i=S;i!=T;i=la[cur[i]])if(va[cur[i]]<V)V=va[cur[u=i]];
                for(fl+=V,i=S;i!=T;i=la[cur[i]])va[cur[i]]-=V,va[cur[i]^1]+=V;
            }
            for(i=cur[u];i;i=ne[i])if(va[i]&&d[la[i]]+1==d[u])break;
            if(i)cur[u]=i,pre[la[i]]=u,u=la[i];else{
                if(0==--nb[d[u]])break;
                for(i=cur[u]=fir[u],w=T;i;i=ne[i])if(d[la[i]]<w&&va[i])w=d[la[i]];
                ++nb[d[u]=w+1];if(u!=S)u=pre[u];
            }
        }
        return fl;
    }
}G;

7.费用流★★★★

费用流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct CostFlow{
    int S,T,h,t,tot,ans,fir[N],va[M],la[M],ne[M],q[N],d[N],fa[N],pre[N],co[M];bool v[N];
    #define CL(a) sizeof(a,0,sizeof a)
    void in(){CL(fir);CL(pre);CL(fa);tot=1;}
    void ins(int x,int y,int fl,int z){
        la[++tot]=y;ne[tot]=fir[x];va[tot]=fl;co[tot]=z;fir[x]=tot;
        la[++tot]=x;ne[tot]=fir[y];va[tot]=0;co[tot]=-z;fir[y]=tot;
    }
    bool spfa(){
        int i,x,y;
        for(memset(d,63,sizeof d),CL(v),d[S]=h=0,v[q[t=1]=S]=1;h^t;)
            for(i=fir[x=q[h=h%T+1]],v[x]=0;i;i=ne[i])
                if(d[x]+co[i]<d[y=la[i]]&&va[i]){
                    d[y]=d[fa[y]=x]+co[pre[y]=i];
                    if(!v[y])v[q[t=t%T+1]=y]=1;
                }
        return d[T]<1e9;
    }
    void end(){
        int i,p=1e9;
        for(i=T;i!=S;i=fa[i])p=min(p,va[pre[i]]);
        for(i=T;i!=S;i=fa[i])va[pre[i]]-=p,va[pre[i]^1]+=p,ans+=p*co[pre[i]];
    }
    int cal(){for(ans=0;spfa();end());return ans;}
}G;

8.tarjan★★☆

9.双连通分量★

10.Dorminator Tree★

11.LCA★★★☆

LCA
1
2
3
4
5
6
int lca(int x,int y){
    if(d[x]<d[y])swap(x,y);int t=d[x]-d[y],i;
    for(i=0;i<=17;i++)if(t>>i&1)x=F[x][i];
    for(i=17;i>=0;i--)if(F[x][i]!=F[y][i])x=F[x][i],y=F[y][i];
    return x==y?x:F[x][0];
}

12.树分治★★☆

13.基环树找环★★

14.最小树形图★

三、数据结构

1.并查集★★★★ 要路径压缩!

并查集
1
int gf(int x){return fa[x]==x?x:fa[x]=gf(fa[x]);}

2.可并堆★★★★ 返回x

可并堆
1
2
3
4
5
6
7
int merge(int x,int y){
    if(!x||!y)return x+y;
    if(a[x]>a[y])swap(x,y);
    c[x][1]=merge(c[x][1],y);
    swap(c[x][0],c[x][1]);
    return x;
}

3.树状数组★★★★ 二维注意

树状数组
1
2
void add(int x,int z){for(;x<=n;x+=x&-x)c[x]+=z;}
int query(int x){for(sum=0;x;x-=x&-x)sum+=c[x];return sum;}

4.线段树★★★★

5.fhqtreap★

6.Splay★★★☆

Splay
1
2
3
4
5
6
7
void R(int x){
    int y=fa[x],k=(c[y][0]==x);
    c[y][!k]=c[x][k];fa[c[x][k]]=y;
    fa[x]=fa[y];c[fa[y]][c[fa[y]][1]==y]=x;
    c[x][k]=y;fa[y]=x;ps(y);
}
void sy(int x,int &rt){for(;y=fa[x];R(x))if(fa[y])R((x==c[y][0])==(y==c[fa[y]][0])?y:x);ps(x);rt=x;}

7.主席树★★☆

8.树链剖分★★★☆

9.LCT★★★

10.K-D树★☆

四、字符串

1.KMP★★

2.Manacher★★

3.Tire★★

4.可持久化Tire★

5.AC自动机★★☆

6.SA★

7.SAM★★★☆

SAM
1
2
3
4
void add(int x){
    for(p=np,st[np=++cnt]=st[p]+1;p&&!c[p][x];p=F[p])c[p][x]=np;to[np]=i;pos[i]=np;
    if(!p)F[np]=1;else if(st[p]+1==st[q=c[p][x]])F[np]=q;else for(st[nq=++cnt]=st[p]+1,memcpy(c[nq],c[q],sizeof c[q]),F[nq]=F[q],F[q]=F[np]=nq;p&&c[p][x]==q;p=F[p])c[p][x]=nq;
}

8.PAM★★

五、数论&计算几何

1.扩展欧几里得★★★★

扩展欧几里得
1
void exgcd(LL a,LL b,LL &x,LL &y){if(!b){x=1;y=0;return;}exgcd(b,a%b,y,x);y-=x*(a/b);}

2.快速幂★★★★

快速幂
1
LL pow(LL a,LL b,LL p){for(sum=1,a%=p;b;a=a*a%p,b>>=1)if(b&1)sum=sum*a%p;return sum;}

3.BSGS★☆

4.线性筛★★★★

筛素数版
1
2
3
4
5
6
7
for(i=2;i<=n;i++){
        if(!f[i])p[++t]=i;
        for(j=1;j<=t&&i*p[j]<=n;j++){
            f[i*p[j]]=1;
            if(i%p[j]==0)break;
        }
 }

5.O(n)求逆元★★

O(n)求逆元
1
for(inv[1]=1,i=2;i<=m+1;i++)inv[i]=(P-P/i)*inv[P%i]%P;

6.O(lgn)求欧拉函数★★

7.Millar-Rabin★★

8.高斯消元★★★

高斯消元
1
2
3
4
for(i=1;i<=n;i++){
        for(t=a[i][i],j=1;j<=n+1;j++)a[i][j]/=t;
        for(j=1;j<=n;j++)if(i!=j)for(t=a[j][i],k=1;k<=n+1;k++)a[j][k]-=t*a[i][k];
    }

9.线性基★★☆

10.矩阵乘法★★★☆

矩阵乘法
1
2
3
4
5
6
7
8
9
JZ cheng(JZ a,JZ b){
    JZ c;memset(c.m,0,sizeof c.m);
    c.x=a.x;c.y=b.y;
    for(int i=1;i<=a.x;i++)
        for(int j=1;j<=b.y;j++)
            for(int k=1;k<=a.y;k++)
                c.m[i][j]=(c.m[i][j]+(a.m[i][k]%MOD)*(b.m[k][j]%MOD))%MOD;
    return c;
}

11.组合数递推★★★☆

组合数递推
1
for(i=0;i<=n;i++)for(c[i][0]=1,j=1;j<=n;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%M;

12.卢卡斯及扩展★★☆

卢卡斯
1
LL lucas(LL n,LL m,LL p){return !m?1:lucas(n/p,m/p,p)*C(n%p,m%p,p)%p;}

13.CRT★☆

14.FFT★★☆

15.NTT%998244353★★☆

16.多项式求逆★☆

17.计算几何★★☆

18.半平面交★

19.单纯形★★☆

Avatar_small
AP SSC Hindi Model P 说:
2022年9月19日 00:29

Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student, AP SSC Hindi Model Paper and the Board of Secondary Education, Andhra Pradesh (BSEAP) class 10th of SSC students also can download the AP 10th Hindi Model Paper 2023 Pdf with Solved question paper in chapter wise for all lessons of the course as AP SSC Hindi Model Paper 2023. Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter