俩操作:增加路径上的点的权值。查询子树的权值和。
想了想似乎只能树链剖分了。。好久没写链剖+数据结构了TAT
一开始没开LL炸了一发(明明有想到的。。我果然是傻逼= =
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
struct zs{
int too,pre;
}e[maxn<<];int tot,last[maxn];
int lc[maxn<<],rc[maxn<<],tt;
ll sm[maxn<<],tag[maxn<<];
int dfn[maxn],sz[maxn],fa[maxn],top[maxn],dep[maxn],tim;
int i,j,k,n,m,x,y,lca,v; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
} inline void dfs1(int x){
sz[x]=,dep[x]=dep[fa[x]]+;
for(int i=last[x];i;i=e[i].pre)
dfs1(e[i].too),sz[x]+=sz[e[i].too];
}
inline void dfs2(int x,int chain){
top[x]=chain,dfn[x]=++tim;int i,mx=;
for(i=last[x];i;i=e[i].pre)if(sz[e[i].too]>sz[mx])mx=e[i].too;
if(!mx)return;
dfs2(mx,chain);
for(i=last[x];i;i=e[i].pre)if(e[i].too!=mx)dfs2(e[i].too,e[i].too);
}
inline int getlca(int a,int b){
while(top[a]!=top[b]){
if(dep[top[a]]<dep[top[b]])swap(a,b);
a=fa[top[a]];
}
return dep[a]<dep[b]?a:b;
} inline void insert(int a,int b){e[++tot].too=b,e[tot].pre=last[a],last[a]=tot;} inline void pushdown(int x,int a,int b,int mid,int lc,int rc){
tag[lc]+=tag[x],tag[rc]+=tag[x],sm[lc]+=tag[x]*(mid-a+),sm[rc]+=tag[x]*(b-mid);
tag[x]=;
}
#define upd(x) sm[x]=sm[lc[x]]+sm[rc[x]];
inline void add(int x,int a,int b,int l,int r,int v){
// printf("add:%d %d %d %d--%d %d\n",x,a,b,l,r,v);
if(l<=a&&r>=b){tag[x]+=v,sm[x]+=(ll)(b-a+)*v;/*,printf("! %d\n",sm[x]);*/return;}
int mid=(a+b)>>;
if(tag[x])pushdown(x,a,b,mid,lc[x],rc[x]);
if(l<=mid)add(lc[x],a,mid,l,r,v);
if(r>mid) add(rc[x],mid+,b,l,r,v);
upd(x);//printf(" %d--%d %d %d %d\n",a,b,sm[x],sm[lc[x]],sm[rc[x]]);
}
inline ll query(int x,int a,int b,int l,int r){
if(l<=a&&r>=b)return sm[x];
int mid=(a+b)>>;
if(tag[x])pushdown(x,a,b,mid,lc[x],rc[x]);
if(r<=mid)return query(lc[x],a,mid,l,r);else
if(l>mid)return query(rc[x],mid+,b,l,r);else
return query(lc[x],a,mid,l,r)+query(rc[x],mid+,b,l,r);
}
inline void build(int a,int b){
tt++;
if(a==b)return;
int mid=(a+b)>>,x=tt;
lc[x]=tt+,build(a,mid),rc[x]=tt+,build(mid+,b);
} inline void ADD(int x,int y,int v){
while(top[x]!=top[y]){
add(,,n,dfn[top[x]],dfn[x],v);
x=fa[top[x]];
}
add(,,n,dfn[y],dfn[x],v);
}
/*inline ll QUERY(int x,int y){
ll sum=0;
while(top[x]!=top[y]){
sum+=query(1,1,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
return sum+query(1,1,n,dfn[y],dfn[x]);
}*/ int main(){
n=read();
for(i=;i<n;i++)x=read()+,y=read()+,fa[y]=x,insert(x,y);
dfs1(),dfs2(,);
build(,n);char id;
for(m=read();m;m--){
for(id=getchar();id<'A'||id>'Z';id=getchar());
if(id=='A'){
x=read()+,y=read()+,v=read(),
lca=getlca(x,y);//printf(" %d %d lca:%d\n",x,y,lca);
ADD(x,lca,v),ADD(y,lca,v),add(,,n,dfn[lca],dfn[lca],-v);
}else{
x=read()+;
//ll sum=QUERY(x,lca)+QUERY(y,lca)-query(1,1,n,dfn[lca],dfn[lca]);
ll sum=query(,,n,dfn[x],dfn[x]+sz[x]-);
printf("%lld\n",sum);
}
}
return ;
}