模板

· · 算法·理论

ST表

[https://www.luogu.com.cn/problem/P3865]

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
const int N=1e5+10;
int n,m,a[N],st[N][20];
int quary(int l,int r){
    int len=log2(r-l+1);
    int a=st[l][len];
    int b=st[r-(1<<len)+1][len];
    return max(a,b);
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        st[i][0]=a[i];
    }
    for(int i=1;(1<<i)<=n;i++){
        for(int j=1;j+(1<<i)-1<=n;j++){
            int len=(1<<(i-1));
            st[j][i]=max(st[j][i-1],st[j+len][i-1]);
        }
    }
    while(m--){
        int a,b;
        cin>>a>>b;
        cout<<quary(a,b)<<endl;
    }
    return 0;****
}

SPFA

[https://www.luogu.com.cn/problem/P3385]

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
int T,n,m,q,dis[2005],cnt[2005]; 
bool st[2005];
struct node{
    int v,w;
};
vector<node>adj[2005];
bool spfa(int s){
    memset(cnt,0,sizeof cnt);
    queue<int>q;
    q.push(s);
    st[s]=1;
    dis[s]=0;
    while(!q.empty()){
        int h=q.front();
        q.pop();
        st[h]=0;
        if(cnt[h]>n) return 1; 
        for(node x:adj[h]){
            if(dis[x.v]>dis[h]+x.w){ 
                dis[x.v]=dis[h]+x.w; 
                cnt[x.v]=cnt[h]+1; 
                if(!st[x.v]){
                    st[x.v]=1;
                    q.push(x.v); 
                }
            }
        }

    }
    return 0; 
}
signed main(){
    cin >> T;
    while(T--){
        memset(adj,0,sizeof adj);
        memset(st,0,sizeof st);
        memset(dis,INF,sizeof dis);
        cin >> n >> m;
        while(m--){ 
            int u,v,w;
            cin>>u>>v>>w;
            if(w>=0){
                adj[u].push_back({v,w});
                adj[v].push_back({u,w});
            }   
            else adj[u].push_back({v,w});
        }
        int num=spfa(1);
        if(num==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

最近公共祖先

(https://www.luogu.com.cn/problem/P3379)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
const int N=5e5+10;
int n,m,s,f[N][100],d[N*2];
vector<int>z[N*2];
void dfs(int x,int fa){
    d[x]=d[fa]+1;
    f[x][0]=fa;
    for(auto i:z[x]){
        if(i!=fa) dfs(i,x);
    }
}
void build(){
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i<=n;i++){
            f[i][j]=f[f[i][j-1]][j-1];
        }
    }
}
int lca(int x,int y){
    if(d[x]>d[y]) swap(x,y);
    for(int i=30;i>=0;i--){
        if(d[f[y][i]]>=d[x])  y=f[y][i];
    }
    if(x==y) return x;
    for(int i=30;i>=0;i--){
        if(f[x][i]!=f[y][i]){
            x=f[x][i];
            y=f[y][i];
        }
    }
    return f[x][0];
}
void solve(){
    cin>>n>>m>>s;
    for(int i=1;i<=n-1;i++){
        int x,y; cin>>x>>y;
        z[x].push_back(y);
        z[y].push_back(x);
    }
    dfs(s,0); build();
    while(m--){
        int a,b; cin>>a>>b;
        cout<<lca(a,b)<<endl;
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}

哈希表

(https://www.luogu.com.cn/problem/P3370)

#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 200010;
const int P = 131;
int n;
string str[N];
int h[1510], p[N];
map<int,int>mp;
signed main()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> str[i];
        str[i]=" "+str[i];
    }
    p[0] = 1;
    for(int i=1 ; i<=n ; i++)
    {
        p[i] = p[i-1] * P;
        for(int j=1 ; j<str[i].size() ; j++)
        {
            h[j] = h[j-1] * P + str[i][j];
        }
        int k = str[i].size()-1;
        mp[h[k]]++;
    }
   cout<<mp.size();
}

线段树

(https://www.luogu.com.cn/problem/P3372)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N=1e7+10;
int n,m,a[N];
struct node{
    int l,r,sum,txt;
}t[N*4];
void build(int l,int r,int k){
    t[k].l=l; t[k].r=r;
    if(l==r){
        t[k].sum=a[l];
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
    t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
void lazy(int k,int v){
    int tmp=t[k].r-t[k].l+1;
    t[k].sum+=v*tmp;
    t[k].txt+=v;
}
void pushdown(int k){
    lazy(2*k,t[k].txt);
    lazy(2*k+1,t[k].txt);
    t[k].txt=0;
}
void change(int l,int r,int v,int k){
    if(t[k].l>=l&&t[k].r<=r){
        lazy(k,v);
        return ;
    }
    if(t[k].txt!=0) pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(l<=mid) change(l,r,v,k*2);
    if(r>mid) change(l,r,v,k*2+1);
    t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
int query(int l,int r,int k){
    int res=0;
    if(t[k].l>=l&&t[k].r<=r) return t[k].sum;
    if(t[k].txt!=0) pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(l<=mid) res+=query(l,r,k*2);
    if(r>mid) res+=query(l,r,k*2+1);
    return res;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    build(1,n,1);
    while(m--){
        int op,x,y,k;
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            change(x,y,k,1);
        }
        else{
            cin>>x>>y;
            cout<<query(x,y,1)<<endl;
        }
    }
    return 0;
}

线性筛素数

(https://www.luogu.com.cn/problem/P3383)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl "\n"
int n,m;
vector<int>v;
bool st[100000010];
void shai(int n){
    for(int i=2;i<=n;i++){
        if(st[i]==1) continue;
        v.push_back(i);
        for(int j=i+i;j<=n;j+=i){
            st[j]=1;
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    shai(n);
    while(m--){
        int x;
        cin>>x;
        cout<<v[x-1]<<endl;
    }
    return 0;
}

dijistar

(https://www.luogu.com.cn/problem/P4779)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N=2e5+10;
using pii=pair<int,int>;
int n,m,s;
vector<pii>z[N];
int d[N],st[N];
void dij(int x)
{
    memset(d,INF,sizeof d);
    memset(st,0,sizeof st);
    priority_queue<pii,vector<pii>,greater<pii>>q;
    q.push({0,x});
    d[x]=0;
    while(q.size())
    {
        auto t=q.top();
        q.pop();
        if(st[t.second]==1) continue;
        st[t.second]=1;
        for(auto i:z[t.second])
        {
            if(d[i.first] > d[t.second] + i.second)
            {
                d[i.first] = d[t.second] + i.second;
                q.push({d[i.first],i.first});
            }
        }
    }
}
signed main()
{
    cin>>n>>m>>s;
    while(m--)
    {
        int u,v,w;
        cin>>u>>v>>w;
        z[u].push_back({v,w});
    }
    dij(s);
    for(int i=1;i<=n;i++)
    {
        cout<<d[i]<<" ";
    }
}

拓扑排序

(https://www.luogu.com.cn/problem/B3644)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 110;
int n;
vector<int>ans,v[N];
int ru[N];
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        while(1)
        {
            int t;
            cin>>t;
            if(t==0) break;
            v[i].push_back(t);
            ru[t]++;
        }
    }
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        if(ru[i]==0) q.push(i);
    }
    while(q.size())
    {
        int f=q.front();
        q.pop();
        ans.push_back(f);
        for(auto i:v[f])
        {
            ru[i]--;
            if(ru[i]==0) q.push(i);
        }
    }
    for(auto i:ans)
    {
        cout<<i<<" ";
    }
    return 0;
} 

最小生成树

(https://www.luogu.com.cn/problem/P3366)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 10000;
int n,m;
struct as{
    int l,r,w;
};
int f[N];
vector<as>v;
int find(int x)
{
    if(x!=f[x]) f[x]=find(f[x]);
    return f[x];
}
int cmp(as x,as y)
{
    return x.w < y.w;
}
int ans=0;
void kk()
{
    sort(v.begin(),v.end(),cmp);
    for(int i=1;i<=n;i++) f[i]=i;
    int cnt=0;
    for(auto i:v)
    {
        if(cnt==n-1) break;
        int e1=find(i.l);
        int e2=find(i.r);
        if(e1==e2) continue;
        ans+=i.w;
        f[e1]=e2;
        cnt++;
    }
    if(cnt==n-1) cout<<ans;
    else cout<<"orz";
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        v.push_back({a,b,c});
    }
    kk(); 
}

快速幂

(https://www.luogu.com.cn/problem/P1226)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
int a,b,p;
int pows(int a,int b){
    int ans=1;
    while(b){
        if(b%2==1) ans=ans*a;
        ans%=p;
        a%=p;
        a*=a;
        a%=p;
        b/=2;
    }
    return ans;
}
signed main(){
    cin>>a>>b>>p;
    int ans=pows(a,b);
    cout<<a<<"^"<<b<<" mod "<<p<<"="<<ans;
    return 0;
}

二分查找

(https://www.luogu.com.cn/problem/U438352)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 2e5+10;
struct as{
    int num,w;
}a[N];
int n,k;
int cmp(as x,as y)
{
    return x.num<y.num;
}
int check(int mid)
{
    if(a[mid].num >= k) return 1;
    else return 0;
}
int ans;
signed main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].num;
        a[i].w=i;
    }
    sort(a+1,a+1+n,cmp);
    a[0].num=-1e9;
    a[n+1].num=1e9; 
    int l=0,r=n+1;
    while(l+1<r)
    {
        int mid=(l+r)/2;
        if(check(mid)==1) r=mid;
        else l=mid; 
    }
    if(a[r].num==k) cout<<r-1;
    else cout<<"-1";
    return 0;
}

KMP

(https://www.luogu.com.cn/problem/P3375)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f
const int N=1000010;
const int P=131;
string s1, s2;
int h1[N], h2[N], p[N];
int s[N];
int f[N];
int get(int l,int r,int h[],int p[])
{
    return h[r]-h[l-1]*p[r-l+1];
}
signed main()
{
    h1[0]=0;
    p[0]=1,s[0]=1;
    cin >> s1 >> s2;
    int len1=s1.size();
    int len2=s2.size();
    for(int i=0;i<len1;i++)
    {
        p[i+1]=p[i]*P;
        h1[i+1]=h1[i]*P+s1[i];
    }
    for(int i=0;i<len2;i++)
    {
        h2[i+1]=h2[i]*P+s2[i];
    }
    int t=h2[len2];
    for(int i=1;i<=len1-len2+1;i++)
    {
        if(get(i,i+len2-1,h1,p)==t)
        {
            cout<<i<<endl;
        }
    }
    f[1]=0;
    cout<<f[1]<<" ";
    for(int i=2;i<=len2;i++)
    {
        bool flag=1;
        for(int j=f[i-1];j>=0;j--)
        {
            if(get(1,j+1,h2,p)==get(i-j,i,h2,p))
            {
                if(j==f[i-1])
                {
                    f[i]=f[i-1]+1;
                }
                else f[i]=j+1;
                flag=0;
                cout<<f[i]<<" ";
                break; 
            }
        }
        if(flag)
        {
            f[i]=0;
            cout<<f[i]<<" ";
        }
    }
} 

Manacher

https://www.luogu.com.cn/problem/P3805

#include<bits/stdc++.h>
using namespace std;
const int N=11000002;
int n,p[2*N],mi,r,an,le;
char s[N],t[2*N];
signed main(){
    scanf("%s",s+1);
    le=strlen(s+1);
    t[++n]='$';
    t[++n]='#';
    for(int i=1;i<=le;i++){
        t[++n]=s[i];
        t[++n]='#';
    }   
    t[++n]='!';
    for(int i=2;i<n;i++){
        if(i<=r){
            p[i]=min(p[2*mi-i],r-i+1);
        }
        else p[i]=1;
        while(t[i-p[i]]==t[i+p[i]]){
            p[i]++;
        }
        if(i+p[i]>r){
            mi=i;
            r=i+p[i]-1;
        }
        an=max(an,p[i]);
    }
    cout<<an-1;
    return 0;
}

AC 自动机

(https://www.luogu.com.cn/problem/P3808)

#include<bits/stdc++.h>
#define N 500010
using namespace std;
queue<int>q;
struct Aho_Corasick_Automaton{
    int c[N][26],val[N],fail[N],cnt;
    void ins(char *s){
        int len=strlen(s);int now=0;
        for(int i=0;i<len;i++){
            int v=s[i]-'a';
            if(!c[now][v])c[now][v]=++cnt;
            now=c[now][v];
        }
        val[now]++;
    }
    void build(){
        for(int i=0;i<26;i++)if(c[0][i])fail[c[0][i]]=0,q.push(c[0][i]);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=0;i<26;i++)
            if(c[u][i])fail[c[u][i]]=c[fail[u]][i],q.push(c[u][i]);
            else c[u][i]=c[fail[u]][i];
        }
    }
    int query(char *s){
        int len=strlen(s);int now=0,ans=0;
        for(int i=0;i<len;i++){
            now=c[now][s[i]-'a'];
            for(int t=now;t&&~val[t];t=fail[t])ans+=val[t],val[t]=-1;
        }
        return ans;
    }
}AC;
int n;char p[1000005];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%s",p),AC.ins(p);
    AC.build();
    scanf("%s",p);int ans=AC.query(p);
    printf("%d\n",ans);
    return 0;
}

Floyd

(https://www.luogu.com.cn/problem/B3647)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
int n,m;
int d[5010][5010];
void f(){
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j) d[i][j]=0;
            else d[i][j]=INF;
        }
    }
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        d[u][v]=min(d[u][v],w);
        d[v][u]=min(d[v][u],w);
    }
    f();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<d[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

并查集

(https://www.luogu.com.cn/problem/P3367)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 2e5+10;
int n,m;
int f[N];
int find(int x)
{
    if(f[x]!=x)
    {
        f[x]=find(f[x]);
    }
    return f[x];
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) f[i]=i;
    while(m--)
    {
        int z,x,y;
        cin>>z>>x>>y;
        if(z==1)
        {
            f[find(x)]=find(y);
        }
        if(z==2)
        {
            if(find(x)==find(y)) cout<<"Y"<<endl;
            else cout<<"N"<<endl;
        }
    }   
    return 0;
}

树状数组

(https://www.luogu.com.cn/problem/P3374)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 5e5+10;
int n,m,a[N],s[N];
int lowbit(int x)
{
    return x & -x;
}
void add(int x,int k)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        s[i]+=k;
    }
}
int get(int x)
{
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        ans+=s[i];
    }
    return ans;
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        add(i,a[i]);
    }
    while(m--)
    {
        int op;
        cin>>op;
        if(op==1)
        {
            int x,k;
            cin>>x>>k;
            add(x,k);
        }
        else 
        {
            int x,y;
            cin>>x>>y;
            int ans=get(y)-get(x-1);
            cout<<ans<<endl;
        }
    } 
    return 0;
}

单调栈

(https://www.luogu.com.cn/problem/P5788)

#include<cstdio>
#include<stack>
#include<algorithm>
#define mp make_pair
using namespace std;
stack<long long> S;
long long a[5000005],ans;
int main(){
    long long n;
    scanf("%lld",&n);
    for(long long i=1;i<=n;++i) scanf("%lld",&a[i]);
    a[n+1]=10086001100860011ll;
    for(long long i=1;i<=n+1;++i)
    {
        if(S.empty() || a[S.top()]>=a[i])   S.push(i);
        else
        {
            while(S.size() && a[S.top()]<a[i])
            {
                long long Top=S.top();
                ans+=(i-Top-1);
                S.pop();
            }
            S.push(i);
        }
    }
    printf("%lld",ans);
    return 0;
}

字典树

(https://www.luogu.com.cn/problem/P8306)

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f3f
#define endl '\n'
int n,q,t[3000005][80],tot=0,cnt[3000010];
char s[3000005];
int get(char x){
    if(x>='A'&&x<='Z')
        return x-'A';
    else if(x>='a'&&x<='z')
        return x-'a'+26;
    else
        return x-'0'+52;
}
void build(char s[]){
    int len=strlen(s),p=0;
    for(int i=0;i<len;i++){
        int ch=get(s[i]);
        if(!t[p][ch]) t[p][ch]=++tot;
        p=t[p][ch];
        cnt[p]++;
    }
}
int find(char s[]){
    int len=strlen(s),p=0;
    for(int i=0;i<len;i++){
        int ch=get(s[i]);
        if(!t[p][ch]) return 0;
        p=t[p][ch];
    } 
    return cnt[p];
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int T; cin>>T;
    while(T--){
        for(int i=0;i<=tot;i++)
            for(int j=0;j<=122;j++)
                t[i][j]=0;  
        for(int i=0;i<=tot;i++)
            cnt[i]=0;
        tot=0;
        cin>>n>>q;
        for(int i=1;i<=n;i++){
            cin>>s;
            build(s);
        } 
        while(q--){
            cin>>s;
            cout<<find(s)<<endl;
        }
    }
    return 0;
}

树的遍历

DFS

void dfs(int x){ 
    vis1[x]=1;
    for(int i=0;i<e[x].size();i++){
        int point=s[e[x][i]].v;
        if(!vis1[point]){
            dfs(point);
        }
    }
}

BFS

void bfs(int x){  
    queue <int> q;
    q.push(x);
    vis2[x]=1;
    while(!q.empty()){
        int fro=q.front();
        for(int i=0;i<e[fro].size();i++){
            int point=s[e[fro][i]].v;
            if(!vis2[point]){
                q.push(point); 
                vis2[point]=1;
            }
        }
        q.pop();
    }
}

逆序对+剪枝

(https://www.luogu.com.cn/problem/P1908)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
const int N=5e5+100;
int n;
int a[N],s[N];
vector<int>all;
int lowbit(int x){
    return x & -x;
}
void add(int x,int k){
    for(int i=x;i<=n;i+=lowbit(i)){
        s[i]+=k;
    }
}
int find(int x){
    int p=lower_bound(all.begin(),all.end(),x)-all.begin();
    return p+1;
}
int get(int x){
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i)){
        ans+=s[i];
    }
    return ans;
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        all.push_back(a[i]);
    }
    sort(all.begin(),all.end());
    all.erase(unique(all.begin(),all.end()),all.end());
    int ans=0;
    for(int i=1;i<=n;i++){
        int t=find(a[i]);
        ans+=get(n)-get(t);
        add(t,1);
    }
    cout<<ans;
    return 0;
}

单调队列

(https://www.luogu.com.cn/problem/P1886)

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
struct node{
    int t,q;
}p;
int a[N],b[N],n,k;
deque<node>minn,maxn;
int main(){
    cin>>n>>k;
    for(int i=1,x;i<=n;i++){
        cin>>x;
        p.t=x,p.q=i;
        while((!minn.empty())&&minn.back().t<=x){
            minn.pop_back();
        }
        minn.push_back(p);
        while((!maxn.empty())&&maxn.back().t>=x){
            maxn.pop_back();
        }
        maxn.push_back(p);
        while((!minn.empty())&&minn.front().q<=i-k){
            minn.pop_front();
        }
        while((!maxn.empty())&&maxn.front().q<=i-k){
            maxn.pop_front();
        }
        if(i>=k){
            a[i-k]=maxn.front().t;
            b[i-k]=minn.front().t;
        }
    }
    for(int i=0;i<=n-k;i++)
    {
        cout<<a[i]<<' ';
    }
    cout<<endl;
    for(int i=0;i<=n-k;i++)
    {
        cout<<b[i]<<' ';
    }
}