题目描述
对子树整体异或或询问路径异或和
树链剖分
裸上
#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=200000+10;
int tree[maxn*4],ad[maxn*4];
int h[maxn],go[maxn*2],next[maxn*2],size[maxn],jump[maxn],fa[maxn][25],dfn[maxn],nfd[maxn],a[maxn],d[maxn];
int dl[maxn],sta[100];
int i,j,k,l,t,n,m,tot,top,ans,maxd,head,tail;
char ch;
int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
void add(int x,int y){
go[++tot]=y;
next[tot]=h[x];
h[x]=tot;
}
void dfs(){//get fa d size
int i,t,now;
dl[tail=1]=1;
d[1]=1;
head=0;
while (head<tail){
now=dl[++head];
t=h[now];
while (t){
if (!d[go[t]]){
d[go[t]]=d[now]+1;
fa[go[t]][0]=now;
dl[++tail]=go[t];
}
t=next[t];
}
}
fd(i,tail,1){
now=dl[i];
size[now]++;
if (fa[now][0]) size[fa[now][0]]+=size[now];
}
}
void dg(){//get dfn nfd jump
int i,j,k,l,t,now;
dl[tail=1]=1;
jump[1]=1;
dfn[1]=top=1;
nfd[1]=1;
head=0;
while (head<tail){
now=dl[++head];
k=dfn[now];
t=h[now];
j=0;
while (t){
if (d[go[t]]==d[now]+1&&(!j||size[go[t]]>size[j])) j=go[t];
t=next[t];
}
l=0;
if (j){
dfn[j]=k+1;
nfd[k+1]=j;
jump[j]=jump[now];
dl[++tail]=j;
l+=size[j];
}
t=h[now];
while (t){
if (d[go[t]]==d[now]+1&&go[t]!=j){
dfn[go[t]]=k+l+1;
nfd[k+l+1]=go[t];
jump[go[t]]=go[t];
dl[++tail]=go[t];
l+=size[go[t]];
}
t=next[t];
}
}
}
void build(int p,int l,int r){
if (l==r){
tree[p]=a[nfd[l]];
return;
}
int mid=(l+r)/2;
build((p<<1),l,mid);
build((p<<1)+1,mid+1,r);
tree[p]=tree[(p<<1)]^tree[(p<<1)+1];
}
char get(){
char ch=getchar();
while (ch!='C'&&ch!='Q') ch=getchar();
return ch;
}
int lca(int x,int y){
int j;
if (d[x]<d[y]) swap(x,y);
if (d[x]!=d[y]){
j=maxd;
while (j>=0){
if (d[fa[x][j]]>=d[y]) x=fa[x][j];
j--;
}
}
if (x==y) return x;
j=maxd;
while (j>=0){
if (fa[x][j]!=fa[y][j]){
x=fa[x][j];
y=fa[y][j];
}
j--;
}
return fa[x][0];
}
int mark(int p,int l,int r,int v){
ad[p]^=v;
if ((r-l+1)%2) tree[p]^=v;
}
void down(int p,int l,int r){
if (ad[p]){
int mid=(l+r)/2;
mark((p<<1),l,mid,ad[p]);
mark((p<<1)+1,mid+1,r,ad[p]);
ad[p]=0;
}
}
void change(int p,int l,int r,int a,int b,int v){
if (l==a&&r==b){
mark(p,l,r,v);
return;
}
down(p,l,r);
int mid=(l+r)/2;
if (b<=mid) change((p<<1),l,mid,a,b,v);
else if (a>mid) change((p<<1)+1,mid+1,r,a,b,v);
else{
change((p<<1),l,mid,a,mid,v);
change((p<<1)+1,mid+1,r,mid+1,b,v);
}
tree[p]=tree[(p<<1)]^tree[(p<<1)+1];
}
int query(int p,int l,int r,int a,int b){
if (l==a&&r==b) return tree[p];
down(p,l,r);
int mid=(l+r)/2;
if (b<=mid) return query((p<<1),l,mid,a,b);
else if (a>mid) return query((p<<1)+1,mid+1,r,a,b);
else return query((p<<1),l,mid,a,mid)^query((p<<1)+1,mid+1,r,mid+1,b);
}
void write(int x){
if (!x){
putchar('0');
putchar('\n');
return;
}
top=0;
while (x){
sta[++top]=x%10;
x/=10;
}
while (top){
putchar('0'+sta[top]);
top--;
}
putchar('\n');
}
int main(){
freopen("phase.in","r",stdin);freopen("phase.out","w",stdout);
n=read();
fo(i,1,n-1){
j=read();k=read();
add(j,k);add(k,j);
}
fo(i,1,n) a[i]=read();
dfs();
maxd=floor(log(n)/log(2));
fo(j,1,maxd)
fo(i,1,n)
fa[i][j]=fa[fa[i][j-1]][j-1];
dg();
build(1,1,n);
m=read();
while (m--){
ch=get();
if (ch=='C'){
j=read();k=read();
change(1,1,n,dfn[j],dfn[j]+size[j]-1,k);
}
else{
j=read();k=read();
l=lca(j,k);
ans=0;
while (1){
if (d[jump[j]]<=d[l]){
ans^=query(1,1,n,dfn[l],dfn[j]);
j=l;
break;
}
else{
ans^=query(1,1,n,dfn[jump[j]],dfn[j]);
j=fa[jump[j]][0];
}
}
while (1){
if (d[jump[k]]<=d[l]){
ans^=query(1,1,n,dfn[l],dfn[k]);
k=l;
break;
}
else{
ans^=query(1,1,n,dfn[jump[k]],dfn[k]);
k=fa[jump[k]][0];
}
}
ans^=query(1,1,n,dfn[l],dfn[l]);
write(ans);
}
}
}