模版

· · 算法·理论

分治

分治:分而治之, 将复杂问题分成两个或多个相同或相似的子问题,再将子问题分成更小的子问题

二分

二分查找,二分答案

保证序列是单调的,有单调性

#include<bits/stdc++.h>
using namespace std;

int a[1111];
int n,x;

int main(){
    cin>>n>>x;
    for(int i=1;i<=n;++i) cin>>a[i];
    sort(a+1,a+n+1);
    int l,r;
    l=1;r=n;//l==r时可以认为结束了
//保证答案始终处于[l,r]以内,循环以l等于r结束,
//每次二分的中间值mid会归属于左右区间其中之一

    //查找a数组中第一个大于等于x的数
    while(l<r){
        int mid=(l+r)>>1;//为什么不用整数除法:当区间的范围有负数的时候,整数除法无法正常工作
                        //-3/2=-1 向0取整 (-3)>>1=-2 向下取整
        if(a[mid]>=x) r=mid;
        else l=mid+1;
    }
    cout<<l<<" "<<a[l]<<endl;

    l=1,r=n;
    //查找a数组中最后一个小于等于x的数 第一个大于x的数
    while(l<r){
        int mid=(l+r+1)>>1;
//        int mid=(l+r)>>1;
                        //当r-l=1时,[3,4] mid=l;
                        //如果接下来进入了l=mid这个分支,区间无法改变,死循环
                        //如果进入了r=mid-1这个分支,发现r<l,达不到要求
        if(a[mid]<=x) l=mid;
        else r=mid-1;
    }
    cout<<l<<" "<<a[l]<<endl;

//实数域的二分,确定好精度即可
    double eps=1e-8;
    double ll=0.000;rr=100.000;
    while(ll+eps<rr){//如果精度无法确切表示,
//    for(int _=0;_<100;++_){ 2^100=10^30
        double midd=(ll+rr)/2;
        if(calc(midd)) rr=midd;
        else ll=midd;
    }

    return 0;
}

c++11内置函数:

lower_bound(...) 第一个大于等于的

upper_bound(...) 第一个大于的

二分答案
二分一定是要有一个单调性的

答案满足单调性

1.找答案单调性;

2.找到一个算法(复杂度低(满足题目));

例题:OpenJ_NOI - CH0111-01 查找最接近的元素

AC代码:

#include<bits/stdc++.h>
using namespace std;
long long int n,m,a[100001],b[10001];

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    long long int l,r,mid;
    for(int i=0;i<m;i++){
        if(b[i]>=a[n]){
            cout<<a[n]<<endl;
        }
        else if(b[i]<=a[1]){
            cout<<a[1]<<endl;
        }
        else{
            l=1;
            r=n;
            while(l<r){
                mid=(l+r)>>1;
                if(a[mid]>=b[i]){
                    r=mid;
                }else{
                    l=mid+1;
                }
            }
            if((a[l]-b[i])>=(b[i]-a[l-1])){
                cout<<a[l-1];
            }
            else{
                cout<<a[l];
            }
            if(i!=m-1){
                cout<<endl;
            }
        }
    }
    return 0;
}

三分

例题:LibreOJ - 10013 曲线

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const ld eps=1e-12;
const int MAXN=1e5+10;
const int MAXM=1e6+10;
struct node{
    int a,b,c;
}A[MAXN];
int T,n;
double l,r;

double f(double x){
    double res=-inf;
    for(int i=1;i<=n;i++){
        res=max(res,A[i].a*x*x+A[i].b*x+A[i].c);
    }
    return res;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d %d %d",&A[i].a,&A[i].b,&A[i].c);
        }
        l=0.0,r=1000.0;
        while(l<r-eps){
            double mid1=l+(r-l)/3;
            double mid2=r-(r-l)/3;
            if(f(mid1)>f(mid2)) l=mid1;
            else r=mid2;
        }
        printf("%.4lf\n",f(l)); 
    }
    return 0;
} 

搜索

深度优先搜索 dfs

//OpenJ_NOI - CH0205-1388 Lake Counting
#include<bits/stdc++.h> 
using namespace std;
int k,n,xa,ya,xb,yb,num=0;
char a[110][110]={};
int vis[110][110]={0};
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool pd=false;
void dfs(int xa,int ya,int xb,int yb){
    num++;
    /*if(xa==xb&&ya==yb){
        return ;
    }*/
    for(int i=0;i<4;i++){
        int u=xa+dir[i][0];
        int v=ya+dir[i][1];
        if(u>=0&&u<n&&v>=0&&v<n&&vis[u][v]==0&&a[u][v]=='.'){
            vis[u][v]=1;
            if(u==xb&&v==yb&&!pd){
                cout<<"YES\n";
                pd=true;
                return ;
            }
            dfs(u,v,xb,yb);
        }
    }
}
int main(){
    cin>>k;
    for(int o=0;o<k;o++){
        num=0;
        memset(vis,0,sizeof vis);
        pd=false;
        cin>>n;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>a[i][j];
            }
        }
        cin>>xa>>ya>>xb>>yb;
        if(xa==xb&&ya==yb){
            cout<<"YES\n";
            continue;
        } 
        if(a[xa][ya]=='#'||a[xb][yb]=='#'){
            cout<<"NO"<<"\n";
            continue;
        }
        vis[xa][ya]=1;
        dfs(xa,ya,xb,yb);
        if(pd==false){
            cout<<"NO\n";
        }
    }
    return 0;
}

宽度优先搜索 bfs

很多很多图论算法的原型

和dfs类似的是,也是一种盲目搜索,不考虑结果可能在的位置,把整张图都搜一遍,直到找到结果

dfs 优先考虑深度,整棵搜索树比较稀疏的时候,dfs搜索层数过多,效率低下

有的时候需要考虑 连通性问题 最短路问题等问题,这个时候可以考虑用bfs

bfs优先考虑的是每种状态和初始状态之间的距离, 和初始状态越接近的情况,会越早被考虑到

利用队列实现,queue<int> q; 特点是先进先出 q.empty() 如果为空,返回真 front() 返回队首元素 priority_queue 可以用 top() pop() 弹出队首 push() 在末尾加入一个元素 size() back()

bfs步骤: 1.把起始点放入队列中 2.从队列中取出队首的点 3.找出与此点邻接的且尚未被遍历的点,进行标记,然后全部放入队列中 4.回到第二步,重复操作,直到队列为空

能够得出的结论:每个节点入队和出队次数都是1次,所以遍历整张图的时间复杂度不会太大

dfs可以处理出点s到点t的所有路径条数 bfs可以处理出点s到点t的最短路径

例题:OpenJ_NOI - CH0205-2753 走迷宫

AC代码:

//OpenJ_NOI - CH0205-2753 走迷宫
#include<bits/stdc++.h>
using namespace std;

int r,c,vis[50][50]={0};
char a[50][50];

struct note{
    int x,y,len;
};

queue<note> q;

int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int main(){
    cin>>r>>c;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            cin>>a[i][j];
        }
    }
    note w;
    w.x=0,w.y=0,w.len=0;
    q.push(w);
    vis[0][0]=1;
    while(!q.empty()){
        note s=q.front();
        int slen=s.len;
        q.pop();
        if(s.x==r-1&&s.y==c-1){
            printf("%d",s.len+1);
            return 0;
        }
        for(int i=0;i<4;i++){
            int sx=s.x+dir[i][0];
            int sy=s.y+dir[i][1];
            if(sx>=0&&sx<r&&sy>=0&&sy<c&&vis[sx][sy]==0&&a[sx][sy]=='.'){
                q.push({sx,sy,slen+1});
                vis[sx][sy]=1;
            }
        }
    }
    return 0;
}

图论

最小生成树

Kruskal

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=1e3+10;
const int MAXM=1e4+10;

struct edge{
    int u,v,w;
}g[MAXM];

int n,m,k,fa[MAXN],num,ans,tot,cnt;

void ADD(int u,int v,int w){
    num++;
    g[num].u=u;
    g[num].v=v;
    g[num].w=w;
    return ;
}

int getfa(int x){
    if(fa[x]==x){
        return x;
    }
    return fa[x]=getfa(fa[x]);
}

void merge(int x,int y){
    x=getfa(x);
    y=getfa(y);
    fa[x]=y;
    return ;
}

bool cmp(edge A,edge B){
    return A.w<B.w;
}

void kruskal(){
    ans=0;
    tot=0;
    for(int i=1;i<=m;i++){
        int xx=getfa(g[i].u),yy=getfa(g[i].v);
        if(xx!=yy){
            merge(xx,yy);
            tot++;
            ans+=g[i].w;
            if(tot==n-k){
                cout<<ans<<"\n";
                return ;
            }
        }
    }
    cout<<"No Answer\n";
    return ;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>k;
    if(n==k){
        cout<<"0\n";
        return 0;
    }
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    num=0;
    for(int i=1,ex1,ex2,ex3;i<=m;i++){
        cin>>ex1>>ex2>>ex3;
        ADD(ex1,ex2,ex3);
    }
    sort(g+1,g+m+1,cmp);
    kruskal();
    return 0;
}

最短路

SPFA
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const ld eps=1e-12;
const int MAXN=2e3+10;
const int MAXM=3e3+10;
struct edge{
    int to,cost;
};
int T,n,m,dis[MAXN],cnt[MAXN];
bool vis[MAXN];
vector<edge> G[MAXN];
queue<int> q;

bool SPFA(int n,int s){
    for(int i=1;i<=n+1;i++){
        dis[i]=inf;
    }
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i].to,w=G[u][i].cost;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                cnt[v]=cnt[u]+1;
                if(cnt[v]>=m) return 0;
                if(!vis[v]){
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return 1;
}

void init_(){
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++){
        G[i].clear();
    }
    while(!q.empty()) q.pop();
    return ;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m;  
        init_();
        for(int i=1,ex1,ex2,ex3;i<=m;i++){
            cin>>ex1>>ex2>>ex3;
            if(ex3>=0){
                G[ex1].push_back({ex2,ex3});
                G[ex2].push_back({ex1,ex3});
            }
            else{
                G[ex1].push_back({ex2,ex3});
            }
        }
        if(!SPFA(n,1)){
            cout<<"YES\n";
        }
        else{
            cout<<"NO\n";
        }
    }
    return 0;
}
dijkstra

首先定义"松弛"操作:

对于一个已经确定最短路径的点u, 及其一个邻居v, 有dist[v] = min(dist[v], dist[u] + cost[u][v])

接下来递归定义dijkstra算法的过程:

对于某一个时刻, 在当前已经确定最短路径的点集和其出边中, 选择最小的一个松弛, 则松弛对应的顶点的最短路径就被确定为松弛的结果

int cost[MAX_V][MAX_V];//cost[u][v]为边(u,v)的长度,不存在这条边时设为INF
int d[MAX_V];//顶点s出发到各顶点的最短距离
int vis[MAX_V];//已经使用过的图
int V;//顶点数

void dijkstra(int s){
    fill(d,d+V,INF);
    fill(vis,vis+V,0);
    d[s]=0;
    while(true){
        int v=-1;
        for(int u=0;u<V;++u){//从尚未使用过的顶点中选择一个距离s最近的顶点
            if(!vis[u]&&(v==-1||d[u]<d[v])) v=u;
        }
        if(v==-1) break;
        vis[u]=1;
        for(int u=0;u<V;++u){
            d[u]=min(d[u],d[v]+cost[u][v]);
        }
    }
}
dijkstra堆优化
struct edge{
    int to,cost;
};
typedef pair<int,int> P;//两个变量分别表示d[i]和i 
int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s){//出发点s
//  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> > que;//指定greater<pair<int,int>>参数,堆按照first从小到大的顺序取出值
    priority_queue<P,vector<P>,greater<P> > que;//指定greater<P>参数,堆按照first从小到大的顺序取出值
    fill(d,d+V,inf);
    d[s]=0;
    que.push(P(0,s));
    while(!que.empty()){
        P p=que.top();
        que.pop();
        int v=p.second;//距离已经确定了最短路的顶点最近的顶点
        if(vis[v]) continue;
        //p.first当前v距离s的最短路长度 
        if(d[v]<p.first) continue;//同一个顶点可能跟随不同的边入队多次 
        for(int i=0;i<G[v].size();++i){
            edge e=G[v][i];//v直接相连的边 
            if(d[e.to]>d[v]+e.cost){//e.to是v的邻居,e.cost是v到e.to的边的长度 
                d[e.to]=d[v]+e.cost;//松弛 
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

时间复杂度:O(V+ElogV)

floyd
int d[MAX_V][MAX_V];
int V;

void floyd(){
    for(int k=0;k<V;++k){
        for(int i=0;i<V;++i){
            for(int j=0;j<V;++j){
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
}

强连通分量

有向图的Tarjan算法

题目:迷宫城堡 HDU - 1269

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const int MAXN=1e4+20;
const int MAXM=1e5+20;
int n,m,index,num;
int dfn[MAXN],low[MAXN],V[MAXN];
bool book[MAXN];
vector<int> G[MAXN];
stack<int> st;
void tarjan(int u){
    dfn[u]=low[u]=++index;
    st.push(u);
    book[u]=1;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(book[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u]){
        num++;
        int j;
        do{
            j=st.top();
            st.pop();
            book[j]=0;
        }while(j!=u);
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    while(cin>>n>>m){
        if(n==0&&m==0) break;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }   
        for(int i=1,ex1,ex2;i<=m;i++){
            cin>>ex1>>ex2;
            G[ex1].push_back(ex2);
        }
        memset(dfn,0,sizeof(dfn));
        memset(book,0,sizeof(book));
        index=0,num=0;
        for(int i=1;i<=n;i++){
            if(!dfn[i]){
                tarjan(i);
            }
        }
        if(num>1) cout<<"No\n";
        else cout<<"Yes\n";
    }
    return 0;
} 

树上问题

lca(最近公共祖先)

模板:P3379 【模板】最近公共祖先(LCA)

洛谷

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f;
const int mod=1e9+7;
const ld eps=1e-12;
const int MAXN=5e5+10;
const int MAXM=1e6+10;
int n,m,s,dep[MAXN],f[MAXN][35],a,b;
vector<int> G[MAXN];
void dfs(int u,int fa){
    f[u][0]=fa;
    dep[u]=dep[fa]+1;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==fa) continue;
        dfs(v,u);
    }
    return ;
}
int lca(int u,int v){
    if(dep[u]<dep[v]) swap(u,v);
    for(int i=22;i>=0;i--){
        if(dep[f[u][i]]>=dep[v]){
            u=f[u][i];
        }
    }
    if(u==v) return u;
    for(int i=22;i>=0;i--){
        if(f[u][i]!=f[v][i]){
            u=f[u][i];
            v=f[v][i];
        }
    }
    return f[u][0];
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>s;
    for(int i=1,ex1,ex2;i<n;i++){
        cin>>ex1>>ex2;
        G[ex1].push_back(ex2);
        G[ex2].push_back(ex1);
    }
    dfs(s,s);
    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];
        }
    }
    for(int i=1;i<=m;i++){
        cin>>a>>b;
        cout<<lca(a,b)<<"\n";
    }
    return 0; 
}

数据结构

并查集

//HDU-1213 How Many Tables
#include<bits/stdc++.h>
using namespace std;
int m,n,q;
int t;
int fa[1010]={};
int getfa(int x){
    if(fa[x]==x){
        return x;
    }
    return fa[x]=getfa(fa[x]);
}
void Merge(int x,int y){
    int xx=getfa(x);
    int yy=getfa(y);
    if(xx!=yy)  fa[xx]=yy;
}
int query(int x,int y){
    int xx=getfa(x);
    int yy=getfa(y);
    return  xx==yy;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i){
            fa[i]=i;
        }
        for(int i=0,a=0,b=0;i<m;++i){
            scanf("%d%d",&a,&b);
            Merge(a,b);
        }
        int cnt=0;
        for(int i=1;i<=n;++i){
            if(fa[i]==i){
                cnt++;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

树状数组

//洛谷 P3374
#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=5e5+10;
int c[4*MAXN];
int n,m;
ll ans,res;
void add(int x,int k){//单点修改 
    for(int i=x;i<=n;i+=lowbit(i)){
        c[i]+=k;
    }
    return ;
}
int search(int l,int r){//区间查询 
    res=0;
    for(int i=r;i;i-=lowbit(i)){
        res+=c[i];
    }
    for(int i=l-1;i;i-=lowbit(i)){
        res-=c[i];
    }
    return res;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1,ex;i<=n;i++){
        cin>>ex;
        add(i,ex);
    }
    for(int i=1,ex1,ex2,ex3;i<=m;i++){
        cin>>ex1>>ex2>>ex3;
        if(ex1==1){
            add(ex2,ex3);
        }
        else{
            cout<<search(ex2,ex3)<<endl;
        }
    }
    return 0;
}
//洛谷 P3368
#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=5e5+10;
int a[MAXN];
int n,m;
ll ans,res,c[4*MAXN],d[4*MAXN]={0};
void update(int x,int k){
    for(int i=x;i<=n;i+=lowbit(i)){
        c[i]+=k;
    }
    return ; 
}
void add(int x,int y,int k){//区间修改
    update(x,k);
    update(y+1,-k);
    return ;
}
ll ask(int x){//单点查询 
    res=0;
    for(int i=x;i;i-=lowbit(i)){
        res+=c[i];
    }
    return res;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    memset(c,0,sizeof(c));
    cin>>n>>m;
    a[0]=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        d[i]=a[i]-a[i-1];
        update(i,d[i]);
    }
    for(int i=1,ex1,ex2,ex3,ex4;i<=m;i++){
        cin>>ex1;
        if(ex1==1){
            cin>>ex2>>ex3>>ex4; 
            add(ex2,ex3,ex4);
        }
        else{
            cin>>ex2;
            cout<<ask(ex2)<<endl;
        }
    }
    return 0;
}

线段树

#include<bits/stdc++.h>
//#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=1e5+10;
const int MAXM=2e6+10;
int n,m,a[MAXN],x,y,k;
ll w[MAXN<<2],lzy[MAXN<<2];

void pullup(int u){
    w[u]=w[(u<<1)]+w[(u<<1)|1];
    return ;
}
void build(int u,int L,int R){
    if(L==R){
        w[u]=a[L];
    }
    else{
        int MID=(L+R)>>1;
        build((u<<1),L,MID);
        build((u<<1)|1,MID+1,R);
        pullup(u);
    }
    return ;
} 
void update(int u,int L,int R,int p,int k){
    if(L==R){
        w[u]=k;
    }
    else{
        int M=(L+R)>>1;
        if(p<=M){
            update((u<<1),L,M,p,k);
        }else{
            update((u<<1)|1,M+1,R,p,k);
        }
        pullup(u);
    }
    return ;
}
ll query(int u,int L,int R,int p){
    if(L==R){
        return w[u];
    }
    else{
        int M=(L+R)>>1; 
        if(p<=M){
            return query((u<<1),L,M,p);
        }else{
            return query((u<<1)|1,M+1,R,p);
        }
    }
}
void maketag(int u,int L,int R,int k){
    w[u]+=(R-L+1)*k;
    lzy[u]+=k;
    return ;
}
void pulldown(int u,int L,int R){
    int MID=(L+R)>>1;
    maketag((u<<1),L,MID,lzy[u]);
    maketag((u<<1)|1,MID+1,R,lzy[u]);
    lzy[u]=0;
    return ;
}
void update(int u,int L,int R,int l,int r,int k){
    if(l<=L&&R<=r){
        maketag(u,L,R,k);
    }
    else if(!(L>r||R<l)){
        pulldown(u,L,R);
        int MID=(L+R)>>1;
        update((u<<1),L,MID,l,r,k);
        update((u<<1)|1,MID+1,R,l,r,k);
        pullup(u);
    }
    return ;
}
ll query(int u,int L,int R,int l,int r){
    if(l<=L&&R<=r){
        return w[u];
    }
    else if(!(L>r||R<l)){
        pulldown(u,L,R);
        int MID=(L+R)>>1;
        return query((u<<1),L,MID,l,r)+query((u<<1)|1,MID+1,R,l,r);
    }
    else{
        return 0;
    }
}
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,1,n);
    for(int i=1,ex;i<=m;i++){
        cin>>ex;
        if(ex==1){
            cin>>x>>y>>k;
            update(1,1,n,x,y,k);
        }else{
            cin>>x>>y;
            cout<<query(1,1,n,x,y)<<"\n";
        }
    }
    return 0;
}

ST表

模板题:P3865 【模板】ST 表 & RMQ 问题

洛谷

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=1e5+10;
const int MAXM=1e6+10;
int n,m,a[MAXN],f[MAXN][20],lg[MAXN],x,y;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    lg[1]=0;
    for(int i=2;i<=n;i++){
        lg[i]=lg[i>>1]+1;
    }
    for(int i=1;i<=n;i++){
        cin>>f[i][0];
    }
    for(int j=1;j<=lg[n];j++){
        for(int i=1;i<=n-(1<<j)+1;i++){
            f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
        }
    }
    for(int i=1;i<=m;i++){
        cin>>x>>y;
        int l=lg[y-x+1];
        cout<<max(f[x][l],f[y-(1<<l)+1][l])<<"\n"; 
    }
    return 0;
}

字符串

Manacher

模板题:P3805 【模板】Manacher 洛谷

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAXN=1e7+1e6+20;
const int MAXM=1e6+10;
const ld eps=1e-12;
const int mod=998244353;

string str,s;
int len,ans;
int p[MAXN*2];

void manacher(string t){
    int r=0,c=0;
    for(int i=0;i<len;i++){
        if(i<r) p[i]=min(p[(c<<1)-i],r-i);
        else p[i]=1;
        while(t[i-p[i]]==t[i+p[i]]) p[i]++;
        if(i+p[i]>r){
            r=i+p[i];
            c=i;
        }
    }
    return ;
}   

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>str;
    s="^#";
    for(int i=0;i<str.length();i++){
        s+=str[i];
        s+="#";
    }
    s+="~";
    len=s.length();
    manacher(s);
    ans=0;
    for(int i=0;i<len;i++){
        ans=max(p[i]-1,ans);
    }
    cout<<ans;
    return 0;
}

数学

快速幂

不带模:
ll qpow(ll x,ll y){
    ll res=1;
    while(y>0){
        if(y&1){
            res=res*x;
        } 
        x=x*x;
        y>>=1;
    }
    return res;
}
带模:
ll qpow(ll x,ll y){
    ll res=1;
    res=(res%mod+mod)%mod;
    while(y>0){
        if(y&1){
            res=(res*x)%mod;
        } 
        x=(x*x)%mod;
        y>>=1;
    }
    return res%mod;
}

埃氏筛法

线性筛法:

vector<int> pri;
bool not_prime[MAXN];

void pre(int n){
    for(int i=2;i<=n;++i){
        if(!not_prime[i]){
            pri.push_back(i);
        }
        for(int pri_j : pri){
            if(i*pri_j>n) break;
            not_prime[i*pri_j]=1;
            if(i%pri_j==0){
                break;
            }
        }
    }
}

杂项

快读&&快写

//P10615
#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n,ans,x;
int in(){
    int k=0,f=1;
    char c=getchar_unlocked();
    while(c<'0'||c>'9'){
        if(c=='-') f=-1;
        c=getchar_unlocked();
    }
    while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar_unlocked();
    return k*f;
}
void out(int x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x+'0');
    else out(x/10),putchar(x%10+'0');
}
signed main(){
    int n=in(),sum=0;
    while(n--) sum+=in();
    out(sum);
    return 0;
}