bzoj1036--树链剖分

时间:2022-10-30 16:00:14

模板题。。。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 30001
#define INF 1000000000
inline char Nc(){
static char buf[],*p1=buf,*p2=buf;
if(p1==p2){
p2=(p1=buf)+fread(buf,,,stdin);
if(p1==p2)return EOF;
}
return *p1++;
}
inline void Read(int& x){
char c=Nc(),b=;
for(;c<''||c>'';c=Nc())if(c=='-')b=-;
for(x=;c>=''&&c<='';x=x*+c-,c=Nc());x*=b;
}
inline void Read(char* s){
int i=;
char c=Nc();
while(c!=' ')s[i]=c,i++,c=Nc();
}
int Len;
char s[];
inline void Print(int x){
if(x<){
putchar('-');
x=-x;
}
if(!x){
putchar('');putchar('\n');
return;
}
for(Len=;x;x/=)s[++Len]=x%;
for(;Len;Len--)putchar(s[Len]+);
putchar('\n');
}
struct Edge{
int To,Next;
}e[N<<];
inline int _Max(int x,int y){
return x>y?x:y;
}
int h[N],i,j,k,Num,n,m,T,a[N],w[N],x,y,w2[N],Top[N],Size[N],Son[N],f[N],Sum[N<<],Max[N<<],Deep[N];
char S[];
inline void Add(int x,int y){
e[++Num].To=y;e[Num].Next=h[x];h[x]=Num;
}
void Dfs1(int x,int Fa){
f[x]=Fa;Size[x]=;Deep[x]=Deep[Fa]+;
for(int i=h[x];i;i=e[i].Next)
if(e[i].To!=Fa){
Dfs1(e[i].To,x);
Size[x]+=Size[e[i].To];
if(Size[e[i].To]>Size[Son[x]])Son[x]=e[i].To;
}
}
void Dfs2(int x,int Tmp){
w[x]=++m;w2[m]=x;Top[x]=Tmp;
if(Son[x])Dfs2(Son[x],Tmp);
for(int i=h[x];i;i=e[i].Next)
if(e[i].To!=f[x]&&e[i].To!=Son[x])Dfs2(e[i].To,e[i].To);
}
void Build(int Node,int l,int r){
if(l==r){
Sum[Node]=Max[Node]=a[w2[l]];
return;
}
int Mid=(l+r)>>;
Build(Node<<,l,Mid);
Build(Node<<|,Mid+,r);
Sum[Node]=Sum[Node<<]+Sum[Node<<|];
Max[Node]=_Max(Max[Node<<],Max[Node<<|]);
}
void Update(int Node,int l,int r,int x,int y){
if(l==r){
Sum[Node]=Max[Node]=y;
return;
}
int Mid=(l+r)>>;
if(x<=Mid)Update(Node<<,l,Mid,x,y);else Update(Node<<|,Mid+,r,x,y);
Sum[Node]=Sum[Node<<]+Sum[Node<<|];
Max[Node]=_Max(Max[Node<<],Max[Node<<|]);
}
int Querysum(int Node,int l,int r,int L,int R){
if(l>R||r<L)return ;
if(l>=L&&r<=R)return Sum[Node];
int Mid=(l+r)>>;
return Querysum(Node<<,l,Mid,L,R)+Querysum(Node<<|,Mid+,r,L,R);
}
int Querymax(int Node,int l,int r,int L,int R){
if(l>R||r<L)return -INF;
if(l>=L&&r<=R)return Max[Node];
int Mid=(l+r)>>;
return _Max(Querymax(Node<<,l,Mid,L,R),Querymax(Node<<|,Mid+,r,L,R));
}
int Querysum_tree(int u,int v){
int Ans=;
while(Top[u]!=Top[v])
if(Deep[Top[u]]<Deep[Top[v]]){
Ans+=Querysum(,,m,w[Top[v]],w[v]);
v=f[Top[v]];
}else{
Ans+=Querysum(,,m,w[Top[u]],w[u]);
u=f[Top[u]];
}
if(w[u]<w[v])Ans+=Querysum(,,m,w[u],w[v]);else Ans+=Querysum(,,m,w[v],w[u]);
return Ans;
}
int Querymax_tree(int u,int v){
int Ans=-INF;
while(Top[u]!=Top[v])
if(Deep[Top[u]]<Deep[Top[v]]){
Ans=_Max(Ans,Querymax(,,m,w[Top[v]],w[v]));
v=f[Top[v]];
}else{
Ans=_Max(Ans,Querymax(,,m,w[Top[u]],w[u]));
u=f[Top[u]];
}
if(w[u]<w[v])Ans=_Max(Ans,Querymax(,,m,w[u],w[v]));else Ans=_Max(Ans,Querymax(,,m,w[v],w[u]));
return Ans;
}
int main()
{
Read(n);
for(i=;i<n;i++){
Read(x);Read(y);
Add(x,y);
Add(y,x);
}
for(i=;i<=n;i++)Read(a[i]);
Dfs1(,);
Dfs2(,);
Build(,,m);
Read(T);
while(T--){
Read(S);Read(x);Read(y);
if(S[]=='C')Update(,,m,w[x],y);else
if(S[]=='S')Print(Querysum_tree(x,y));else Print(Querymax_tree(x,y));
}
return ;
}

bzoj1036