[树链剖分] BZOJ1036: [ZJOI2008]树的统计Count

来源:互联网 发布:车辆识别摄像头软件 编辑:程序博客网 时间:2024/04/27 03:35

题意

给出一棵树,单调修改点权,询问路径点权和与点权最大值。

解析

树链剖分模板题,线段树只需要支持单点修改+区间询问即可。

#include<cstdio>#include<algorithm>using namespace std;const int maxn=30005,maxe=60005;int n,Q,w[maxn],fir[maxn],nxt[maxe],son[maxe],tot;int sz[maxn],hvy[maxn],pre[maxn],d[maxn],top[maxn],pos[maxn],c[maxn];struct node{ int L,R,sum,Max; };struct segment{    node seg[maxn*4];    void maintain(int p){        seg[p].sum=seg[p<<1].sum+seg[p<<1|1].sum;        seg[p].Max=max(seg[p<<1].Max,seg[p<<1|1].Max);    }    void build(int p,int L,int R,int* Arr){        seg[p].L=L; seg[p].R=R;        if(L==R){ seg[p].Max=seg[p].sum=*(Arr+L); return; }        int mid=(L+R)>>1;        build(p<<1,L,mid,Arr); build(p<<1|1,mid+1,R,Arr);        maintain(p);    }    void Updata(int p,int pos,int val){        if(pos<seg[p].L||seg[p].R<pos) return;        if(pos==seg[p].L&&seg[p].R==pos){ seg[p].sum=seg[p].Max=val; return; }        Updata(p<<1,pos,val); Updata(p<<1|1,pos,val);        maintain(p);     }    int Query_sum(int p,int L,int R){        if(R<seg[p].L||seg[p].R<L) return 0;        if(L<=seg[p].L&&seg[p].R<=R) return seg[p].sum;        if(seg[p].L==seg[p].R) return 0;        return Query_sum(p<<1,L,R)+Query_sum(p<<1|1,L,R);    }    int Query_max(int p,int L,int R){        if(R<seg[p].L||seg[p].R<L) return -1e+9;        if(L<=seg[p].L&&seg[p].R<=R) return seg[p].Max;        if(seg[p].L==seg[p].R) return -1e+9;        return max(Query_max(p<<1,L,R),Query_max(p<<1|1,L,R));    }} T;void add(int x,int y){    son[++tot]=y; nxt[tot]=fir[x]; fir[x]=tot;}void dfs(int x){    sz[x]=1; hvy[x]=0;    for(int j=fir[x];j;j=nxt[j]) if(son[j]!=pre[x]){        pre[son[j]]=x; d[son[j]]=d[x]+1;        dfs(son[j]);        sz[x]+=sz[son[j]];        if(sz[son[j]]>sz[hvy[x]]) hvy[x]=son[j];    }}void build_chain(int x,int tp){    c[++c[0]]=w[x]; pos[x]=c[0]; top[x]=tp;    if(hvy[x]) build_chain(hvy[x],tp);    for(int j=fir[x];j;j=nxt[j]) if(son[j]!=pre[x])     if(son[j]!=hvy[x]) build_chain(son[j],son[j]);}int getsum(int x,int y){    int res=0;    while(top[x]!=top[y]){        if(d[top[x]]<d[top[y]]) swap(x,y);        res+=T.Query_sum(1,pos[top[x]],pos[x]);        x=pre[top[x]];    }    if(d[x]>d[y]) swap(x,y);    return res+T.Query_sum(1,pos[x],pos[y]);}int getmax(int x,int y){    int res=-1e+9;    while(top[x]!=top[y]){        if(d[top[x]]<d[top[y]]) swap(x,y);        res=max(res,T.Query_max(1,pos[top[x]],pos[x]));        x=pre[top[x]];    }    if(d[x]>d[y]) swap(x,y);    return max(res,T.Query_max(1,pos[x],pos[y]));   }int main(){    scanf("%d",&n);    for(int i=1;i<=n-1;i++){        int x,y; scanf("%d%d",&x,&y);        add(x,y); add(y,x);    }    for(int i=1;i<=n;i++) scanf("%d",&w[i]);    dfs(1);     build_chain(1,1);    T.build(1,1,c[0],c);    scanf("%d",&Q);    while(Q--){        char pd[10]; scanf("%s",pd);        if(pd[0]=='C'){            int x,val; scanf("%d%d",&x,&val);            T.Updata(1,pos[x],val);        } else        if(pd[1]=='S'){            int x,y; scanf("%d%d",&x,&y);            printf("%d\n",getsum(x,y));        } else        if(pd[1]=='M'){            int x,y; scanf("%d%d",&x,&y);            printf("%d\n",getmax(x,y));        }    }    return 0;}
0 0
原创粉丝点击