码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ2819: Nim

时间:2018-03-02 23:11:42      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:get   tar   设计   online   wap   change   ...   tchar   cst   

Description

著名游戏设计师vfleaking,最近迷上了Nim。普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取。谁不能取谁输。这个游戏是有必胜策略的。于是vfleaking决定写一个玩Nim游戏的平台来坑玩家。
为了设计漂亮一点的初始局面,vfleaking用以下方式来找灵感:拿出很多石子,把它们聚成一堆一堆的,对每一堆编号1,2,3,4,...n,在堆与堆间连边,没有自环与重边,从任意堆到任意堆都只有唯一一条路径可到达。然后他不停地进行如下操作:

1.随机选两个堆v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略,如果有,vfleaking将会考虑将这些石子堆作为初始局面之一,用来坑玩家。
2.把堆v中的石子数变为k。

由于vfleaking太懒了,他懒得自己动手了。请写个程序帮帮他吧。

Input

 第一行一个数n,表示有多少堆石子。
接下来的一行,第i个数表示第i堆里有多少石子。
接下来n-1行,每行两个数v,u,代表v,u间有一条边直接相连。
接下来一个数q,代表操作的个数。
接下来q行,每行开始有一个字符:
如果是Q,那么后面有两个数v,u,询问若在v到u间的路径上的石子堆中玩Nim游戏,是否有必胜策略。
如果是C,那么后面有两个数v,k,代表把堆v中的石子数变为k。

对于100%的数据:
1≤N≤500000, 1≤Q≤500000, 0≤任何时候每堆石子的个数≤32767
其中有30%的数据:
石子堆组成了一条链,这3个点会导致你DFS时爆栈(也许你不用DFS?)。其它的数据DFS目测不会爆。

注意:石子数的范围是0到INT_MAX

Output

对于每个Q,输出一行Yes或No,代表对询问的回答。

Sample Input

【样例输入】
5
1 3 5 2 5
1 5
3 5
2 5
1 4
6
Q 1 2
Q 3 5
C 3 7
Q 1 2
Q 2 4
Q 5 3

Sample Output

Yes
No
Yes
Yes
Yes
 
 
前晚AKCqhzdy大佬一边码这道题一边哀嚎:这什么毒瘤题啊!!
他一直在喊:树剖维护博弈,哈哈哈哈笑死真·丧心病狂出题人,然后这个晚上来凑个热闹
如果说我这几年唯一不是靠背的就是博弈了
简单啊
异或和是不是零啊
结果他喊着怎么又RE又TLE
哈哈哈多谢discuss大佬
 
代码如下:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
inline int read()
{
    int f=1,x=0;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 f*x;
}
struct edge
{
    int x,y,next;
}a[1110000];int len,last[510000];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}
int fa[510000],tot[510000],son[510000],dep[510000];
int n,m;
inline void pre_tree_node(int x)
{
    son[x]=0;tot[x]=1;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa[x])
        {
            fa[y]=x;
            dep[y]=dep[x]+1;
            pre_tree_node(y);
            if(tot[y]>tot[son[x]])son[x]=y;
            tot[x]+=tot[y];
        }
    }
}
int top[510000],ys[510000],z;
int tp;
inline void pre_tree_edge(int x)
{
    int tmp=tp;
    top[x]=tp;ys[x]=++z;
    if(son[x]!=0)pre_tree_edge(son[x]);
    for(int k=last[x];k;k=a[k].next)
        if(a[k].y!=son[x] && a[k].y!=fa[x])
        {
            tp=a[k].y;
            pre_tree_edge(a[k].y);
            tp=tmp;
        }
}
struct node
{
    int lc,rc,l,r,c;
}tr[1110000];int trlen;
inline void bt(int l,int r)
{
    int now=++trlen;
    tr[now].l=l;tr[now].r=r;tr[now].c=0;
    tr[now].lc=tr[now].rc=-1;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=trlen+1;bt(l,mid);
        tr[now].rc=trlen+1;bt(mid+1,r);
    }
}
inline void change(int now,int p,int c)
{
    if(tr[now].l==tr[now].r){tr[now].c=c;return ;}
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(p<=mid)change(lc,p,c);
    else change(rc,p,c);
    tr[now].c=tr[lc].c^tr[rc].c;
}
inline int findsum(int now,int l,int r)
{
    if(tr[now].l==l && tr[now].r==r)return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid)return findsum(lc,l,r);
    else if(mid+1<=l)return findsum(rc,l,r);
    else return findsum(lc,l,mid)^findsum(rc,mid+1,r);
}
int sol(int x,int y)
{
    int ans=0,tx=top[x],ty=top[y];
    while(tx!=ty)
    {
        if(dep[tx]>dep[ty])swap(tx,ty),swap(x,y);
        ans^=findsum(1,ys[ty],ys[y]);
        y=fa[ty];ty=top[y];
    }
    if(x==y)return ans^findsum(1,ys[x],ys[x]);
    else
    {
        if(dep[x]>dep[y])swap(x,y);
        return ans^findsum(1,ys[x],ys[y]);
    }
}
int col[510000];
char ss[10];
int main()
{
    n=read();
    for(int i=1;i<=n;i++)col[i]=read();
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<n;i++)
    {
        int x=read(),y=read();
        ins(x,y);ins(y,x);
    }
    fa[1]=0;dep[1]=0;pre_tree_node(1);
    z=0;tp=1;pre_tree_edge(1);
    trlen=0;bt(1,z);
    for(int i=1;i<=n;i++)change(1,ys[i],col[i]);
    m=read();
    while(m--)
    {
        scanf("%s",ss+1);
        int u=read(),v=read();
        if(ss[1]==Q)
        {
            if(sol(u,v)==0)printf("No\n");
            else printf("Yes\n");
        }
        else change(1,ys[u],v);
    }
    return 0;
}

by_lmy

BZOJ2819: Nim

标签:get   tar   设计   online   wap   change   ...   tchar   cst   

原文地址:https://www.cnblogs.com/MT-LI/p/8495245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!