Description
题目大意就是:1. 换根。2. 对$LCA(u,v)$的子树修改。3. 求$x$子树的权值和。
如果没有操作一,这就是一道沙茶题对吧。。。
于是考虑换根的影响。
具体可以看这题:链接。
但是那个换根后的$LCA(u,v)$怎么解?
其实换完根的$LCA(u,v)$就是以$1$为根的$LCA(u,v),LCA(u,root),LCA(v,root)$中深度最大的那个。
这题就愉快地解决了。。。
原来写的那个板子有点$BUG$,导致我这题$WA$了无数发。。。
药丸。。。
附代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x) b[x].data
#define SIGN(x) b[x].c
#define LSIDE(x) b[x].l
#define RSIDE(x) b[x].r
#define WIDTH(x) (RSIDE(x)-LSIDE(x)+1)
#define MAXN 300010
using namespace std;
int n,m,c=1,d=1,root=1;
int val[MAXN],head[MAXN],deep[MAXN],son[MAXN],size[MAXN],fa[MAXN],id[MAXN],pos[MAXN],top[MAXN];
struct Tree{
int next,to;
}a[MAXN<<1];
struct Segment_Tree{
long long data,c;
int l,r;
}b[MAXN<<2];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
inline void add(int x,int y){
a[c].to=y;a[c].next=head[x];head[x]=c++;
a[c].to=x;a[c].next=head[y];head[y]=c++;
}
void dfs1(int rt){
son[rt]=0;size[rt]=1;
for(int i=head[rt];i;i=a[i].next){
int will=a[i].to;
if(!deep[will]){
deep[will]=deep[rt]+1;
fa[will]=rt;
dfs1(will);
size[rt]+=size[will];
if(size[son[rt]]<size[will])son[rt]=will;
}
}
}
void dfs2(int rt,int f){
id[rt]=d++;pos[id[rt]]=rt;top[rt]=f;
if(son[rt])dfs2(son[rt],f);
for(int i=head[rt];i;i=a[i].next){
int will=a[i].to;
if(will!=fa[rt]&&will!=son[rt])dfs2(will,will);
}
}
inline void pushup(int rt){
DATA(rt)=DATA(LSON)+DATA(RSON);
}
inline void pushdown(int rt){
if(!SIGN(rt)||LSIDE(rt)==RSIDE(rt))return;
SIGN(LSON)+=SIGN(rt);
DATA(LSON)+=SIGN(rt)*WIDTH(LSON);
SIGN(RSON)+=SIGN(rt);
DATA(RSON)+=SIGN(rt)*WIDTH(RSON);
SIGN(rt)=0;
}
void buildtree(int l,int r,int rt){
LSIDE(rt)=l;RSIDE(rt)=r;SIGN(rt)=0;
if(l==r){
DATA(rt)=val[pos[l]];
return;
}
int mid=l+r>>1;
buildtree(l,mid,LSON);
buildtree(mid+1,r,RSON);
pushup(rt);
}
void update(int l,int r,long long c,int rt){
if(l<=LSIDE(rt)&&RSIDE(rt)<=r){
SIGN(rt)+=c;
DATA(rt)+=c*WIDTH(rt);
return;
}
pushdown(rt);
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)update(l,r,c,LSON);
if(mid<r)update(l,r,c,RSON);
pushup(rt);
}
long long query(int l,int r,int rt){
long long ans=0;
if(l<=LSIDE(rt)&&RSIDE(rt)<=r)return DATA(rt);
pushdown(rt);
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)ans+=query(l,r,LSON);
if(mid<r)ans+=query(l,r,RSON);
return ans;
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
return x;
}
int check(int x){
if(x==root)return -1;
if(id[x]<=id[root]&&id[root]<=id[x]+size[x]-1){
int y=root;
while(deep[y]>deep[x]){
if(fa[top[y]]==x)return top[y];
y=fa[top[y]];
}
return son[x];
}
return 0;
}
void update_subtree(int x,int k){
int y=check(x);
if(y==-1)update(1,n,k,1);
else if(y==0)update(id[x],id[x]+size[x]-1,k,1);
else{
update(1,n,k,1);
update(id[y],id[y]+size[y]-1,-k,1);
}
}
void query_subtree(int x){
long long s;
int y=check(x);
if(y==-1)s=query(1,n,1);
else if(y==0)s=query(id[x],id[x]+size[x]-1,1);
else s=query(1,n,1)-query(id[y],id[y]+size[y]-1,1);
printf("%lld
",s);
}
void work(){
int f,x,y,k;
while(m--){
f=read();x=read();
if(f==1)root=x;
else if(f==2){
y=read();k=read();
int lca=LCA(x,y),lca1=LCA(x,root),lca2=LCA(y,root);
if(deep[lca1]>deep[lca])lca=lca1;
if(deep[lca2]>deep[lca])lca=lca2;
update_subtree(lca,k);
}
else query_subtree(x);
}
}
void init(){
int x,y;
n=read();m=read();
for(int i=1;i<=n;i++)val[i]=read();
for(int i=1;i<n;i++){
x=read();y=read();
add(x,y);
}
deep[1]=1;
dfs1(1);
dfs2(1,1);
buildtree(1,n,1);
}
int main(){
init();
work();
return 0;
}