题意: 链修改,子树和查询
直接树剖就好了
#include<bits/stdc++.h>
#include<cstdio>
#define N 100005
#define M 200005
#define ll long long
using namespace std;
int first[N],next[M],to[M],tot;
int sign,siz[N],son[N];
int n,m,dep[N],id[N],top[N],fa[N];
struct Node{ll val,tag;}t[N<<2];
int read(){
int cnt=0,f=1;char ch=0;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt*f;
}
/*--------------------------------dfs-----------------------------------*/
void add(int x,int y){
next[++tot]=first[x],first[x]=tot,to[tot]=y;
}
void dfs1(int u,int f){
siz[u]=1;
for(int i=first[u];i;i=next[i]){
int t=to[i]; if(t==f) continue;
dep[t]=dep[u]+1 , fa[t]=u;
dfs1(t,u); siz[u] += siz[t];
if(siz[son[u]]<siz[t]) son[u]=t;
}
}
void dfs2(int u,int Top){
id[u]=++sign , top[u]=Top;
if(son[u]) dfs2(son[u],Top);
for(int i=first[u];i;i=next[i]){
int t=to[i]; if(t==fa[u]||t==son[u]) continue;
dfs2(t,t);
}
}
/*-----------------------------------线段树-------------------------------------*/
void Pushdown(int x,int l,int r){
if(t[x].tag){
int mid=(l+r)>>1;
t[x<<1].val += (ll)(mid-l+1) * t[x].tag;
t[x<<1].tag += t[x].tag;
t[x<<1|1].val += (ll)(r-mid) * t[x].tag;
t[x<<1|1].tag +=t[x].tag;
t[x].tag=0;
}
}
void Pushup(int x){
t[x].val = t[x<<1].val + t[x<<1|1].val;
}
void update(int x,int L,int R,int val,int l,int r){
if(L<=l && r<=R){
t[x].val += (ll)(r-l+1) * val;
t[x].tag += val; return;
}
Pushdown(x,l,r);
int mid=(l+r)>>1;
if(L<=mid) update(x<<1,L,R,val,l,mid);
if(R>mid) update(x<<1|1,L,R,val,mid+1,r);
Pushup(x);
}
ll quary(int x,int L,int R,int l,int r){
if(L<=l && r<=R) return t[x].val;
Pushdown(x,l,r);
int mid=(l+r)>>1; ll ans=0;
if(L<=mid) ans+=quary(x<<1,L,R,l,mid);
if(R>mid) ans+=quary(x<<1|1,L,R,mid+1,r);
return ans;
}
/*----------------------------------树剖-------------------------------------*/
void Update(int x,int y,int val){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
update(1,id[top[x]],id[x],val,1,n);
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
update(1,id[y],id[x],val,1,n);
}
int main(){
n=read();
for(int i=1;i<n;i++){
int x=read()+1,y=read()+1;
add(x,y),add(y,x);
}
dep[1]=1 , dfs1(1,0) , dfs2(1,1);
m=read(); char s[3];
while(m--){
scanf("%s",s);
if(s[0]=='A'){
int u=read()+1,v=read()+1,d=read();
Update(u,v,d);
}
if(s[0]=='Q'){
int x=read()+1;
printf("%lld\n",quary(1,id[x],id[x]+siz[x]-1,1,n));
}
}return 0;
}