题意:中文
思路:和最简单的HDU 3966 一样,我们只需要用线段树维护两个值就行了,一个最大值一个和,然后注意一下的就是有的题目是点权有的题目是边权
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=30010; int fa[maxn],siz[maxn],son[maxn],w[maxn],p[maxn],dep[maxn],fp[maxn],Rank[maxn],A[maxn]; //fa为父节点,siz为子节点中siz最大的,dep为深度,son为重儿子,w表示在线段树中的位置 int num[maxn<<2],max1[maxn<<2]; int tree_id,n; vector<int>G[maxn]; void dfs1(int u,int ff,int deep){ son[u]=0;fa[u]=ff;siz[u]=1;dep[u]=deep; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==ff) continue; dfs1(v,u,deep+1); siz[u]+=siz[v]; if(siz[v]>siz[son[u]]) son[u]=v; } } void dfs2(int u,int ff){ w[u]=++tree_id;p[u]=ff;Rank[w[u]]=u; if(son[u]) dfs2(son[u],ff); else return ; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v!=fa[u]&&v!=son[u]) dfs2(v,v); } } void pushup(int node){ num[node]=num[node<<1]+num[node<<1|1]; max1[node]=max(max1[node<<1],max1[node<<1|1]); } void buildtree(int le,int ri,int node){ if(le==ri){ num[node]=max1[node]=A[Rank[le]]; return ; } int t=(le+ri)>>1; buildtree(le,t,node<<1); buildtree(t+1,ri,node<<1|1); pushup(node); } void update(int pos,int val,int le,int ri,int node){ if(le==ri){ num[node]=max1[node]=val; return ; } int t=(le+ri)>>1; if(pos<=t) update(pos,val,le,t,node<<1); else update(pos,val,t+1,ri,node<<1|1); pushup(node); } int querysum(int l,int r,int le,int ri,int node){ if(l<=le&&ri<=r) return num[node]; int t=(le+ri)>>1,ans=0; if(l<=t) ans+=querysum(l,r,le,t,node<<1); if(r>t) ans+=querysum(l,r,t+1,ri,node<<1|1); return ans; } int querymax(int l,int r,int le,int ri,int node){ if(l<=le&&ri<=r) return max1[node]; int t=(le+ri)>>1,ans=-inf; if(l<=t) ans=max(ans,querymax(l,r,le,t,node<<1)); if(r>t) ans=max(ans,querymax(l,r,t+1,ri,node<<1|1)); return ans; } int getsum(int u,int v){ int f1=p[u],f2=p[v],tmp=0; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(u,v); } tmp+=querysum(w[f1],w[u],1,n,1); u=fa[f1];f1=p[u]; } if(dep[u]>dep[v]) swap(u,v); tmp+=querysum(w[u],w[v],1,n,1); return tmp; } int getmax(int u,int v){ int f1=p[u],f2=p[v],tmp=-inf; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(u,v); } tmp=max(tmp,querymax(w[f1],w[u],1,n,1)); u=fa[f1];f1=p[u]; } if(dep[u]>dep[v]) swap(u,v); tmp=max(tmp,querymax(w[u],w[v],1,n,1)); return tmp; } int main(){ char str[10]; int u,v,q; while(scanf("%d",&n)!=-1){ for(int i=0;i<maxn;i++) G[i].clear(); memset(son,0,sizeof(son));tree_id=0; for(int i=0;i<n-1;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } for(int i=1;i<=n;i++) scanf("%d",&A[i]); dfs1(1,1,0); dfs2(1,1); buildtree(1,n,1); scanf("%d",&q); while(q--){ scanf("%s",str); if(str[0]=='C'){ scanf("%d%d",&u,&v); update(w[u],v,1,n,1); }else if(str[0]=='Q'&&str[1]=='S'){ scanf("%d%d",&u,&v); int fsum=getsum(u,v); printf("%d\n",fsum); }else{ scanf("%d%d",&u,&v); int fmax=getmax(u,v); printf("%d\n",fmax); } } } return 0; }
时间: 2024-10-16 04:20:18