模板

 

 0 线段树

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+444;
struct re{
    int val;
    int poi,deep;
}q[N];
int dep[N],sz[N],maxdep,now,n,m,ax,dfn[N];
vector<int>v[N],dian[N];
void dfs(int fath,int x){
    ax++;
    sz[x]=1; 
    dfn[x]=ax;
    dep[x]=dep[fath]+1;
    if(dep[x]>maxdep)maxdep=dep[x];
    dian[dep[x]].push_back(x);
    for(int i=0;i<v[x].size();i++){
        int dot=v[x][i];
        if(dot==fath)continue;
        dfs(x,dot);
        sz[x]+=sz[dot];
    }
}
bool cmp(re a,re b){
    return (dep[a.poi]+a.deep>dep[b.poi]+b.deep);}
ll addv[N<<2],sum[N<<2];
void pd(int o,int l,int r){
    if(addv[o]){
        addv[o*2]+=addv[o];
        addv[o*2+1]+=addv[o];
        int m=(r+l)/2;
        sum[o*2]+=(m-l+1)*addv[o];
        sum[o*2+1]+=(r-m)*addv[o];
        addv[o]=0; 
    }
} 
int ql,qr,va;
void mk(int o,int l,int r){
    if(ql<=l&&r<=qr){
        sum[o]+=va*(r-l+1); 
        addv[o]+=va;
    }else{    
        if(l==r)return ;
        int m=(l+r)/2;
        pd(o,l,r);
        if(ql<=m)mk(o*2,l,m);
        if(qr>m)mk(o*2+1,m+1,r);
        sum[o]=sum[o*2]+sum[o*2+1];
    }
}
ll zl,ans[N];
void qz(int o,int l,int r){
    if(ql<=l&&r<=qr){
        zl+=sum[o];
    }else{
        pd(o,l,r);
        int m=(r+l)/2;
        if(m<qr)qz(o*2+1,m+1,r);
        if(ql<=m)qz(o*2,l,m);
    }
} 
int main(){
    ios::sync_with_stdio(0);
    //freopen("p.in","r",stdin);
    //freopen("p.out","w",stdout);
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;    
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    cin>>m;
    dfs(0,1);
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        q[i].poi=a;
        q[i].deep=b;    
        q[i].val=c;
//        cout<<q[i].val<<endl; 
        //cout<<dep[a]+b<<' '<<dep[a]<<endl;
    }
    sort(q+1,q+1+m,cmp);
    now=maxdep;
    int t=1;
    while(t<=m){
        int az=q[t].deep+dep[q[t].poi];
        //az=min(az,maxdep);
        while(now>az){
            if(dian[now].size()==0){now--;continue;} 
            for(int i=0;i<dian[now].size();i++){
                int dot=dian[now][i];
                ql=qr=dfn[dot];
                zl=0;
                qz(1,1,n);
                ans[dot]=zl;
            }
            now--;
        }
        va=q[t].val;
        ql=dfn[q[t].poi];
        qr=dfn[q[t].poi]+sz[q[t].poi]-1;
        //cout<<q[t].poi<<' '<<t<<endl;
        mk(1,1,n);
        t++;
    }
    for(int i=1;i<=now;i++)
        for(int j=0;j<dian[i].size();j++){
            int dot=dian[i][j];
            ql=qr=dfn[dot];zl=0;
            qz(1,1,n);
            ans[dot]=zl;
        }
    for(int i=1;i<=n;i++)cout<<ans[i]<<endl;
    return 0;
}
View Code

 

bool cmpx(re a,re b){return a.x<b.x;}

重载运算符

 

1.快速幂

while(cnt){
        if(cnt&1)ans=ans*b;
        cnt=cnt>>1;
        b=b*b;
    }

2.矩阵快速幂(yuohua)

3.lca

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+999;
int fa[N][24];
vector<int> v[N];
int n;
void ad(int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
int dep[N],siz[N],zson[N][24];
void dfs(int fath,int x){
    dep[x]=dep[fath]+1;
    fa[x][0]=fath;
    siz[x]=1;
    zson[x][0]=-1;
    int maxer=0;
    for(int i=0;i<v[x].size();i++){
    int dot=v[x][i];
    if(dot!=fath){
        dfs(x,dot);
        siz[x]+=siz[dot];
        if(maxer<siz[dot]){maxer=siz[dot];zson[x][0]=dot;}
    }
    }
}
int lca(int x,int y){
    if(x==y)return x;
    if(dep[x]<dep[y])swap(x,y);
    // X
    for(int i=20;i>=0;i--)
    if(dep[x]-(1<<i)>=dep[y])x=fa[x][i];
    if(x==y)return x;
    if(dep[y]==1)return y;
    for(int i=20;i>=0;i--)
    if(fa[x][i]!=fa[y][i]){x=fa[x][i];y=fa[y][i];}
    return fa[x][0];
}
int q,root;
int main(){
    ios::sync_with_stdio(0);
    freopen("p.in","r",stdin);
    freopen("p.out","w",stdout);
    cin>>n>>q>>root;
    for(int i=1;i<=n-1;i++){
    int a,b;
    cin>>a>>b;
    ad(a,b);
        }
    dfs(0,root);
    for(int i=1;i<=20;i++)
        for(int j=1;j<=n;j++)
        fa[j][i]=fa[fa[j][i-1]][i-1];
    for(int i=1;i<=q;i++){
        int a,b;
        cin>>a>>b;
        cout<<lca(a,b)<<endl;
    }
    return 0;
}

 

5.system("diff -b -B 1.out 2.out")

 

 

6.SET

set<int> s[N];
set<int>::iterator it,it2,it3;
s.begin()
s.end() 
s.insert().first

 

posted @ 2018-09-22 09:54  周栎  阅读(138)  评论(0编辑  收藏  举报