【bzoj1036】【ZJOI2008】树的统计

题目描述

一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成
一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 
III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身

输入

输入的第一行为一个整数n,表示节点的个数。接下来n – 1行,每行2个整数a和b,表示节点a和节点b之间有
一条边相连。接下来n行,每行一个整数,第i行的整数wi表示节点i的权值。接下来1行,为一个整数q,表示操作
的总数。接下来q行,每行一个操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式给出。 
对于100%的数据,保证1<=n<=30000,0<=q<=200000;中途操作中保证每个节点的权值w在-30000到30000之间。
输出

对于每个“QMAX”或者“QSUM”的操作,每行输出一个整数表示要求输出的结果。
样例输入

4
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
QSUM 3 4
QSUM 2 1
CHANGE 1 5
QMAX 3 4
CHANGE 3 6
QMAX 3 4
QMAX 2 4
QSUM 3 4


样例输出

4
1
2
2
10
6
5
6
5
16



题解

树链剖分的模版题啊。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=30000+50;
const int inf=1e9;

int fir[maxn],to[maxn*2],nex[maxn*2],ecnt;
int n,q,x,y,w[maxn],cnt;
int wt[maxn],id[maxn],top[maxn],sz[maxn],dep[maxn],fa[maxn],son[maxn];
char op[10];

struct SegmentTree{
    int l,r,ma,v;
}st[maxn*4];

void add_edge(int u,int v){
    nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;
}

void dfs1(int x,int f,int deep){
    dep[x]=deep;
    fa[x]=f;
    sz[x]=1;
    int maxson=-1;
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==f) continue;
        dfs1(v,x,deep+1);
        sz[x]+=sz[v];
        if(sz[v]>maxson) maxson=sz[v],son[x]=v;
    }
}

void dfs2(int x,int topf){
    top[x]=topf;
    id[x]=++cnt;
    wt[cnt]=w[x];
    if(!son[x]) return ;
    dfs2(son[x],topf);
    for(int e=fir[x];e;e=nex[e]){
        int v=to[e];
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}

void pushup(int root){
    st[root].v=st[root*2].v+st[root*2+1].v;
    st[root].ma=max(st[root*2].ma,st[root*2+1].ma);
}

void build(int root,int l,int r){
    st[root].l=l;st[root].r=r;
    if(l==r) st[root].v=st[root].ma=wt[l];
    else{
        int m=l+r>>1;
        build(root*2,l,m);build(root*2+1,m+1,r);
        pushup(root);
    }
}

void change(int root,int x,int t){
    if(st[root].l==st[root].r) st[root].v=st[root].ma=t;
    else{
        int m=st[root].l+st[root].r>>1;
        if(m>=x) change(root*2,x,t);
        else change(root*2+1,x,t); 
        pushup(root);
    }
}

int q_max(int root,int l,int r){
    if(st[root].l>r||st[root].r<l) return -inf;
    if(st[root].l>=l&&st[root].r<=r) return st[root].ma;
    return max(q_max(root*2,l,r),q_max(root*2+1,l,r));
}

int q_sum(int root,int l,int r){
    if(st[root].l>r||st[root].r<l) return 0;
    if(st[root].l>=l&&st[root].r<=r) return st[root].v;
    return q_sum(root*2,l,r)+q_sum(root*2+1,l,r);
}

int Q_max(int x,int y){
    int f1=top[x],f2=top[y],ans=-inf;
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        ans=max(ans,q_max(1,id[f1],id[x]));
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans=max(ans,q_max(1,id[x],id[y]));
    return ans;
}

int Q_sum(int x,int y){
    int f1=top[x],f2=top[y],ans=0;
    while(f1!=f2){
        if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
        ans+=q_sum(1,id[f1],id[x]);
        x=fa[f1];f1=top[x];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans+=q_sum(1,id[x],id[y]);
    return ans;
}

template<typename T>void read(T& aa){
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
    if(cc=='-') ff=-1,cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    aa*=ff;
}

int main(){
    read(n);
    for(int i=1;i<n;i++){
        read(x),read(y);
        add_edge(x,y);
        add_edge(y,x);
    }
    for(int i=1;i<=n;i++) read(w[i]);
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,n);
    read(q);
    while(q--){
        cin>>op;
        if(op[0]=='C'){
            read(x),read(y);
            change(1,id[x],y);
        }
        else if(op[1]=='M'){
            read(x),read(y);
            printf("%d
",Q_max(x,y));
        }
        else{
            read(x),read(y);
            printf("%d
",Q_sum(x,y));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/rlddd/p/9475627.html