样例过了自己造的也对了
交上去0分WA
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int M=100010;
int add[4*M],pre[4*M],ls[4*M],rs[4*M];
int nex[M*2],head[M*2],cnt,to[M*2];
int n,q;
int wt[M],w[M],son[M],fa[M],top[M],dep[M],so,id[M],siz[M];
void jia(int x,int y)
{
to[cnt]=y;
nex[cnt]=head[x];
head[x]=cnt++;
}
void build(int p,int x,int y){
ls[p]=x;
rs[p]=y;
if(x==y)
{
return ;
}
int mid=x+y>>1;
build(p*2,x,mid);
build(p*2+1,mid+1,y);
pre[p]=pre[p*2]+pre[p*2+1];
}
void spread(int p){
if(add[p])
{
pre[p*2]+=add[p]*(rs[p*2]-ls[p*2]+1);
pre[p*2+1]+=add[p]*(rs[p*2]-ls[p*2]+1);
add[p*2]+=add[p];
add[p*2+1]+=add[p];
add[p]=0;
}
}
void change(int p,int x,int y,int z){
if(x<=ls[p]&&y>=rs[p])
{
add[p]+=z;
pre[p]+=z*(rs[p]-ls[p]+1);
return ;
}
spread(p);
int mid=ls[p]+rs[p]>>1;
if(x<=mid)change(p*2,x,y,z);
if(y>mid)change(p*2+1,x,y,z);
pre[p]=pre[p*2]+pre[p*2+1];
}
int ask(int p,int x,int y){
if(x<=ls[p]&&y>=rs[p])
return pre[p];
spread(p);
int mid=ls[p]+rs[p]>>1,ans=0;
if(x<=mid)ans+=ask(p*2,x,y);
if(y>mid)ans+=ask(p*2+1,x,y);
return ans;
}
void up(int x,int y,int z){
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
change(1,id[top[x]],id[x],z);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
change(1,id[x],id[y],z);
}
int qqson(int x){
return ask(1,id[x],id[x]+siz[x]-1);
}
void dfs1(int x,int f,int deep){
fa[x]=f;
dep[x]=deep;
siz[x]=1;
int maxson=-1;
for(int i=head[x];i!=-1;i=nex[i]){
int y=to[i];
if(y==f)continue;
dfs1(y,x,deep+1);
siz[x]+=siz[y];
if(maxson<siz[y])son[x]=y,maxson=siz[y];
}
}
void dfs2(int x,int topf){
id[x]=++so;
top[x]=topf;
if(!son[x])return;
dfs2(son[x],topf);
for(int i=head[x];i!=-1;i=nex[i]){
int y=to[i];
if(y==fa[x]||y==son[x])continue;
dfs2(y,y);
}
}
signed main()
{
cin>>n;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++){
int aaa,bbb;
cin>>aaa>>bbb;
aaa++;
bbb++;
jia(aaa,bbb);
jia(bbb,aaa);
}
dfs1(1,1,1);
dfs2(1,1);
build(1,1,n);
cin>>q;
for(int i=1;i<=q;i++)
{
char ff;
int u,v,d;
cin>>ff;
if(ff=='A'){
cin>>u>>v>>d;
u++;
v++;
up(u,v,d);
}
if(ff=='Q'){
cin>>u;
u++;
cout<<qqson(u)<<endl;
}
}
return 0;
}