BZOJ 3729 splay维护DFS序+博弈论

思路:
这像是 阶梯Nim之类的东西
我们 直接把sg函数 设成mod(L+1)的
一棵子树 向下的奇数层上的石子xor起来 就是答案
有加点和改值的操作 就splay维护一下

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1000050,inf=0x3f3f3f3f,NULLL=N-1;
int n,m,l,op,xx,yy,zz,cnt;
int sg[N],sg_odd[N],sg_even[N];
int first[N],next[N*2],v[N*2],tot;
int root,fa[N],ch[N][2],deep[N],d[N];
void add(int x,int y){v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void push_up(int x){
    int lson=ch[x][0],rson=ch[x][1];
    d[x]=min(deep[x],min(d[lson],d[rson]));
    sg_odd[x]=sg_odd[lson]^sg_odd[rson];
    sg_even[x]=sg_even[lson]^sg_even[rson];
    if(deep[x]&1)sg_odd[x]^=sg[x];
    else sg_even[x]^=sg[x];
}
void rotate(int p){
    int q=fa[p],y=fa[q],x=(ch[q][1]==p);
    ch[q][x]=ch[p][!x],fa[ch[q][x]]=q;
    ch[p][!x]=q,fa[q]=p,fa[p]=y;
    if(y)ch[y][ch[y][1]==q]=p;
    push_up(q);
}
void splay(int x,int tp){
    for(int y;y=fa[x];rotate(x)){
        if(y==tp)break;
        if(fa[y]!=tp){
            if((ch[y][0]==x)^(ch[fa[y]][0]==y))rotate(x);
            else rotate(y);
        }
    }push_up(x);
    if(!tp)root=x;
}
void dfs(int x,int y){
    if(y)deep[x]=deep[y]+1;
    if(root)fa[x]=root,ch[root][1]=x;
    splay(x,0);
    for(int i=first[x];~i;i=next[i]){
        if(v[i]!=y)dfs(v[i],x);
    }
}
int find(int x,int y){
    if(d[ch[x][0]]<=y)return find(ch[x][0],y);
    if(deep[x]<=y)return x;
    return find(ch[x][1],y);
}
int main(){
    memset(first,-1,sizeof(first));
    scanf("%d%d",&n,&l),l++;
    for(int i=1;i<=n;i++)scanf("%d",&sg[i]),sg[i]%=l;
    for(int i=1;i<n;i++)scanf("%d%d",&xx,&yy),add(xx,yy),add(yy,xx);
    d[0]=deep[0]=inf,deep[1]=1,dfs(1,0);
    fa[NULLL]=root,ch[root][1]=NULLL,splay(NULLL,0);
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%d",&op);
        if(op==1){
            scanf("%d",&xx),xx^=cnt;
            splay(xx,0);int temp=find(ch[xx][1],deep[xx]),ans=sg[xx];
            splay(temp,xx);
            if(deep[xx]&1)ans=sg_even[ch[temp][0]];
            else ans=sg_odd[ch[temp][0]];
            if(!ans)puts("GTY");
            else puts("MeiZ"),cnt++;
        }
        else if(op==2){
            scanf("%d%d",&xx,&yy),xx^=cnt,yy^=cnt;
            splay(xx,0);sg[xx]=yy%l;push_up(xx);
        }
        else{
            scanf("%d%d%d",&xx,&yy,&zz);
            xx^=cnt,yy^=cnt,zz^=cnt;zz%=l;
            sg[yy]=zz,deep[yy]=deep[xx]+1;
            splay(xx,0),fa[ch[xx][1]]=yy,fa[yy]=xx;
            ch[yy][1]=ch[xx][1],ch[xx][1]=yy;
            push_up(yy),push_up(xx);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值