rt,应该是线段树锅了,但是不知道哪里有问题。。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((t[x].l+t[x].r)>>1)
#define dbg(x) cout<<#x<<": "<<x<<"\n"
inline ll read(){ll x=0,f=1;char c=getchar();while(c<48||c>57){if(c==45)f=0;c=getchar();}while(c>47&&c<58)x=(x<<3)+(x<<1)+(c^48),c=getchar();return f?x:-x;}
const ll mod=1e9+7,maxn=250005,maxt=505;
ll n,m,fa[maxn],dep[maxn],dfn[maxn],top[maxn],siz[maxn],big[maxn],tot;
vector<ll>son[maxn];
struct node{
ll l,r,val,tag;
}t[maxn<<2];
inline void pushup(ll x){
t[x].val=t[ls].val+t[rs].val;
}
inline void pushdown(ll x){
if(t[x].tag==-1)return;
t[ls].tag=t[x].tag,t[rs].tag=t[x].tag;
t[ls].val=t[x].tag*(mid-t[x].l+1);
t[rs].val=t[x].tag*(t[x].r-mid);
t[x].tag=-1;
}
inline void build(ll x,ll l,ll r){
t[x].l=l,t[x].r=r;
if(l==r){
t[x].val=1,t[x].tag=-1;
return;
}
build(ls,l,mid);
build(rs,mid+1,r);
pushup(x);
}
inline void update(ll x,ll l,ll r,ll k){
if(l<=t[x].l&&t[x].r<=r){
t[x].val=k*(t[x].r-t[x].l+1);
t[x].tag=k;
return;
}
pushdown(x);
if(l<=mid)update(ls,l,r,k);
if(r>mid)update(rs,l,r,k);
pushup(x);
}
inline ll query(ll x,ll l,ll r){
if(l<=t[x].l&&t[x].r<=r)return t[x].val;
pushdown(x);
ll ans=0;
if(l<=mid)ans+=query(ls,l,r);
if(r>mid)ans+=query(rs,l,r);
return ans;
}
inline void dfs1(ll u,ll pre){
fa[u]=pre,dep[u]=dep[pre]+1,siz[u]=1;
for(auto v:son[u]){
if(v==pre)continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[big[u]])big[u]=v;
}
}
inline void dfs2(ll u,ll pre){
dfn[u]=++tot,top[u]=pre;
if(big[u])dfs2(big[u],pre);
for(auto v:son[u]){
if(v==fa[u]||v==big[u])continue;
dfs2(v,v);
}
}
inline void modify(ll u,ll v){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
update(1,dfn[top[u]],dfn[u],0);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
update(1,dfn[u],dfn[v],0);
}
inline ll ask(ll u,ll v){
ll ans=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
ans+=query(1,dfn[top[u]],dfn[u]);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
ans+=query(1,dfn[u],dfn[v]);
return ans;
}
signed main(){
n=read();
for(ll i=1;i<n;++i){
ll u=read(),v=read();
son[u].push_back(v);
son[v].push_back(u);
}
dfs1(1,0),dfs2(1,1);build(1,1,n);
m=read();
for(ll i=1;i<=n+m-1;++i){
char op;cin>>op;
if(op=='A'){
ll u=read(),v=read();
modify(u,v);
}
else{
ll u=read();
printf("%lld\n",ask(1,u));
}
}
return 0;
}