BZOJ 3729 Gty的游戏

来源:互联网 发布:双系统切换软件 编辑:程序博客网 时间:2024/06/07 00:10

博弈论 + 平衡树

这题的模型就是NIM游戏+阶梯博弈

点这里查看阶梯博弈的解说
设地板层数为0,则阶梯博弈等价于奇数层的NIM游戏取石子。证明:若奇数层异或和>0,显然可以构造出必胜策略,如果=0,显然对手就可以构造出必胜策略。

推广到这题的树上也是一样。偶数层的石子都没啥用,对奇数层的石子做NIM游戏求所有奇数层节点值的异或和即可。同理于阶梯博弈,从奇挪到偶等价于取掉石子,从偶挪到奇则可以再挪到偶,等于没挪。具体证明类似阶梯博弈。

然后考虑这题有一个能拿的上限L,把这个点的函数值对(m+1)取模即可。然而这个我不会证。。。(何必追求理性愉悦?)

因为要支持加点和修改,做出欧拉序放到平衡树上,每一个节点维护子树最小值,以该最小值为根的子树内所有点的原树奇数层异或和,总异或和。

#include<cstdio>#define N 100005using namespace std;namespace runzhe2000{    const int INF = 1<<29;     int ecnt, n, m, Q, a[N], last[N], dep[N], fa[N], list[N*2], timer;    struct edge{int next, to;}e[N<<1];    int mabs(int x){return x<0?-x:x;}    void addedge(int a, int b)    {        e[++ecnt] = (edge){last[a], b};        last[a] = ecnt;    }    struct node *tot, *null, *root;    struct node // Splay     {        node *ch[2], *fa;        int mi, tot_sum, odd_sum, id;        int type(){return fa->ch[0]==this ? 0 : 1;}        void pushup()        {            mi = dep[ch[0]->mi] < dep[ch[1]->mi] ? ch[0]->mi : ch[1]->mi;            if(dep[id] < dep[mi]) mi = id;            tot_sum = a[id] ^ ch[0]->tot_sum ^ ch[1]->tot_sum;            odd_sum = ((dep[id] ^ dep[mi]) & 1) ? 0 : a[id];             odd_sum ^= ((dep[mi] ^ dep[ch[0]->mi]) & 1) ? ch[0]->tot_sum ^ ch[0]->odd_sum : ch[0]->odd_sum;            odd_sum ^= ((dep[mi] ^ dep[ch[1]->mi]) & 1) ? ch[1]->tot_sum ^ ch[1]->odd_sum : ch[1]->odd_sum;        }        void rotate()        {            node *f = fa; int d = type();            (fa = f->fa) != null ? fa->ch[f->type()] = this : 0;            (f->ch[d] = ch[!d]) != null ? ch[!d]->fa = f : 0;            ch[!d] = f; f->fa = this; f->pushup();        }        void splay(node *to)        {            if(to == null) root = this;            for(; fa != to; )            {                if(fa->fa == to) rotate();                else                {                    if(type() == fa->type()) fa->rotate();                    else rotate();                    rotate();                }            }             pushup();        }    }mem[N*2], *beg[N], *end[N];    void showtree(node *p)    {        if(p == null) return;        printf("%lld : %lld %lld fa = %lld totsum = %d oddsum = %d mi = %d\n",p-mem,p->ch[0]-mem,p->ch[1]-mem,p->fa-mem,p->tot_sum,p->odd_sum,p->mi);        showtree(p->ch[0]); showtree(p->ch[1]);    }     void init()    {        null = tot = mem;        null->ch[0] = null->ch[1] = null->fa = null;        null->id = null->mi = null->odd_sum = null->tot_sum = 0;    }    node *newnode()    {        node *p = ++tot; *p = *null; return p;    }    node *insert_right(int id, node *p)    {        if(p == null)        {            node *q = newnode();            q->id = q->mi = id;            q->odd_sum = q->tot_sum = a[id];            q->ch[1] = newnode();            q->ch[1]->fa = q;            beg[id] = q; end[id] = q->ch[1];            return q;        }        p->ch[1] = insert_right(id, p->ch[1]);        p->ch[1] -> fa = p;         p->pushup();        return p;    }     node *build(int l, int r)    {        if(l > r)return null;        int mid = (l+r)>>1;        node *p = newnode();        if(list[mid] > 0) // beg        {            p->id = p->mi = list[mid];            p->odd_sum = p->tot_sum = a[list[mid]];            beg[list[mid]] = p;        }        else // end        {            p->id = p->mi = p->odd_sum = p->tot_sum = 0;            end[-list[mid]] = p;                }        p->ch[0] = build(l,mid-1); if(p->ch[0] != null) p->ch[0]->fa = p;        p->ch[1] = build(mid+1,r); if(p->ch[1] != null) p->ch[1]->fa = p;        p->pushup();        return p;    }    void dfs(int x)    {        list[++timer] = x;         for(int i = last[x]; i; i = e[i].next)        {            int y = e[i].to;            if(y == fa[x])continue;            fa[y] = x; dep[y] = dep[x] + 1; dfs(y);        }         list[++timer] = -x;    }    void main()    {        scanf("%d%d",&n,&m);        for(int i = 1; i <= n; i++)            scanf("%d",&a[i]), a[i] %= (m+1);        dep[0] = INF;        for(int i = 1, u, v; i < n; i++)        {            scanf("%d%d",&u,&v);            addedge(u, v);            addedge(v, u);        }        init(); dfs(1);        root = build(1,timer);        scanf("%d",&Q);        int cnt = 0;        for(; Q--; )        {            int opt, u, v, x, y;            scanf("%d",&opt);            if(opt == 1) // query v            {                scanf("%d",&v); v ^= cnt;                beg[v]->splay(null);                end[v]->splay(root);                node *p = end[v]->ch[0];                int SG = p->odd_sum;                puts(SG ? ++cnt, "MeiZ" : "GTY");            }            else if(opt == 2) //modify x -> y            {                scanf("%d%d",&x,&y); x ^= cnt, y ^= cnt;                a[x] = y % (m+1);                beg[x]->splay(null);            }            else // add v under u             {                scanf("%d%d",&u,&v); u ^= cnt, v ^= cnt;                scanf("%d",&a[v]); a[v] ^= cnt;                dep[v] = dep[u] + 1; fa[v] = u; a[v] %= (m+1);                end[u]->splay(null);                insert_right(v,root->ch[0]);                root->pushup();            }        }    }}int main(){    runzhe2000::main();    return 0;} 
0 0
原创粉丝点击