bzoj2819

来源:互联网 发布:小米淘宝授权店 编辑:程序博客网 时间:2024/06/05 23:58

/还是树链剖分裸题。。。。。。

#include<stdio.h>#include<vector>#define MAXN 500005using namespace std;long father[MAXN][21];long dep[MAXN];long stack[MAXN*3];long top=0;long head[MAXN];long p[MAXN*2];long next[MAXN*2];long tot=0;long n,q;long dfstop=0;long cnt[MAXN*2];long st[MAXN];long ed[MAXN];long stone[MAXN];long two[31];long lowbit(long x){return x&-x;}void line(long a,long b){    tot++;p[tot]=b;next[tot]=head[a];head[a]=tot;}void change(long a,long b){    long qs=stone[a];stone[a]=b;b^=qs;    for(long i=st[a];i<=dfstop;i+=lowbit(i))cnt[i]^=b;    for(long i=ed[a];i<=dfstop;i+=lowbit(i))cnt[i]^=b;}long sumxor(long a,long b){    long ans=0;a=st[a];b=st[b];    for(long u=b;u;u-=lowbit(u))ans^=cnt[u];    for(long u=a-1;u;u-=lowbit(u))ans^=cnt[u];    return ans;}long TW(long x){long ans=0;while(x){ans++;x/=2;}return ans-1;}#define pry printf("yeah!\n")#define pr(x,y) printf("%ld %ld\n",x,y)long lca(long a,long b){    if(dep[a]<dep[b])    {        long k=a;a=b;b=k;    }    while(dep[a]>dep[b])    {        a=father[a][TW(lowbit(dep[a]-dep[b]))];    }    while(a!=b)    {        long tmp=0;        for(long j=0;two[j]<=dep[a]-1;j++)        if(father[a][j]!=father[b][j]&&father[a][j+1]==father[b][j+1])        {            tmp=j;break;        }        a=father[a][tmp];        b=father[b][tmp];    }    return a;}char getnext(){    char p=getchar();    while(p!='C'&&p!='Q')p=getchar();    return p;}int main(){    scanf("%ld",&n);two[0]=1;for(long i=1;i<=30;i++)two[i]=two[i-1]*2;    for(long i=1;i<=n;i++)scanf("%ld",&stone[i]);    for(long i=1;i<=n-1;i++)    {        long a,b;scanf("%ld%ld",&a,&b);        line(a,b);line(b,a);    }    father[1][0]=0;stack[++top]=1;dep[1]=1;    while(top)    {        long now=stack[top];top--;        dfstop++;        if(!st[now])        {            st[now]=dfstop;            stack[++top]=now;            for(long u=head[now];u;u=next[u])            if(father[now][0]!=p[u])            {                father[p[u]][0]=now;                dep[p[u]]=dep[now]+1;                stack[++top]=p[u];            }            for(long i=1;two[i]<=dep[now]-1;i++)            father[now][i]=father[father[now][i-1]][i-1];        }        else        {            ed[now]=dfstop;        }    }    for(long i=1;i<=n;i++)    {        long p=stone[i];stone[i]=0;        change(i,p);    }    //for(long i=1;i<=dfstop;i++)printf("%ld ",cnt[i]);    scanf("%ld",&q);    for(long i=1;i<=q;i++)    {        char opt=getnext();long u,v;scanf("%ld%ld",&u,&v);        if(opt=='C')change(u,v);        else        {            long lc=lca(u,v);            long ans=0;            ans^=sumxor(lc,u);            ans^=sumxor(lc,v);            ans^=stone[lc];            if(ans)printf("Yes\n");else printf("No\n");        }    }    return 0;}


0 0