块状树,每个块的根记录一下当前块内距块根为奇数距离的异或和和偶数距离的异或和,询问的时候讨论一下即可。

总的节点数可能超过50000。

#include<cstdio>
#include<cmath>
using namespace std;
#define N 100001
int n,m,L,a[N];
int en,v[N<<1],next[N<<1],first[N];
int e2,v2[N<<1],nex2[N<<1],firs2[N];
int sz,top[N],siz[N],fa[N],xorv[2][N],meiz,dep[N];
void AddEdge(int U,int V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
void AddEdg2(int U,int V)
{
v2[++e2]=V;
nex2[e2]=firs2[U];
firs2[U]=e2;
}
void makeblock(int U)
{
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
fa[v[i]]=U;
dep[v[i]]=dep[U]+1;
if(siz[top[U]]<sz)
{
++siz[top[U]];
top[v[i]]=top[U];
}
makeblock(v[i]);
}
}
void makeGoto(int U)
{
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
if(top[v[i]]!=top[U])
AddEdg2(top[U],v[i]);
makeGoto(v[i]);
}
}
void dfs_init(int U,bool d)
{
xorv[d][top[U]]^=a[U];
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&top[v[i]]==top[U])
dfs_init(v[i],d^1);
}
int ans;
void dfs_Goto(int U,bool d)
{
ans^=xorv[d][top[U]];
for(int i=firs2[U];i;i=nex2[i])
dfs_Goto(v2[i],d^((dep[v2[i]]-dep[U])&1));
}
void dfs_block(int U,bool d)
{
if(d) ans^=a[U];
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
if(top[v[i]]==top[U])
dfs_block(v[i],d^1);
else
dfs_Goto(v[i],d);
}
}
void query(int U)
{
ans=0;
if(U==top[U]) dfs_Goto(U,1);
else dfs_block(U,0);
}
void Update(int U,int x)
{
xorv[0][top[U]]=xorv[1][top[U]]=0;
a[U]=x%(L+1);
dfs_init(top[U],0);
}
void AddNode(int U,int V,int x)
{
++n;
fa[V]=U;
dep[V]=dep[U]+1;
if(siz[top[U]]<sz)
{
top[V]=top[U];
++siz[top[V]];
}
else
{
top[V]=V;
siz[V]++;
AddEdg2(top[U],V);
}
AddEdge(U,V);
a[V]=x%(L+1);
xorv[(dep[V]-dep[top[V]])&1][top[V]]^=a[V];
}
int main()
{
int x,y,c,op;
scanf("%d%d",&n,&L);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
top[i]=i;
siz[i]=1;
a[i]%=(L+1);
}
for(int i=1;i<n;++i)
{
scanf("%d%d",&x,&y);
AddEdge(x,y);
AddEdge(y,x);
}
sz=sqrt(n);
makeblock(1);
makeGoto(1);
for(int i=1;i<=n;++i)
if(top[i]==i)
dfs_init(i,0);
scanf("%d",&m);
for(;m;--m)
{
scanf("%d%d",&op,&x); x^=meiz;
if(op==1)
{
query(x);
if(ans)
{
++meiz;
puts("MeiZ");
}
else puts("GTY");
}
else if(op==2)
{
scanf("%d",&c); c^=meiz;
Update(x,c);
}
else
{
scanf("%d%d",&y,&c); y^=meiz; c^=meiz;
AddNode(x,y,c);
}
}
return 0;
}

【块状树】【博弈论】bzoj3729 Gty的游戏的更多相关文章

  1. [BZOJ3729]Gty的游戏

    [BZOJ3729]Gty的游戏 试题描述 某一天gty在与他的妹子玩游戏.妹子提出一个游戏,给定一棵有根树,每个节点有一些石子,每次可以将不多于L的石子移动到父节点,询问将某个节点的子树中的石子移动 ...

  2. 【块状树】bzoj3731 Gty的超级妹子树

    带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...

  3. 【块状树】bzoj3720 Gty的妹子树

    块状树.教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/将树按一定大小分块,分成许多子树,在每个子树的根节点记 ...

  4. BZOJ3729: Gty的游戏(伪ETT)

    题面 传送门 前置芝士 巴什博奕 \(Nim\)游戏的改版,我们现在每次最多只能取走\(k\)个石子,那么\(SG\)函数很容易写出来 \[SG(x)=mex_{i=1}^{\min(x,k)}SG( ...

  5. bzoj 3720: Gty的妹子树 块状树

    3720: Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 153[Submit][Status] Descr ...

  6. 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)

    未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 448  Solved: 150 Description ...

  7. 【kruscal】【最小生成树】【块状树】bzoj3732 Network

    跟去年NOIP某题基本一样. 最小生成树之后,就变成了询问连接两点的路径上的权值最大的边. 倍增LCA.链剖什么的随便搞. 块状树其实也是很简单的,只不过每个点的点权要记录成“连接其与其父节点的边的权 ...

  8. 【块状树】【树链剖分】bzoj1036 [ZJOI2008]树的统计Count

    很早之前用树链剖分写过,但是代码太长太难写,省选现场就写错了. #include<cstdio> #include<algorithm> #include<cstring ...

  9. 【块状树】【树链剖分】【线段树】bzoj3531 [Sdoi2014]旅行

    离线后以宗教为第一关键字,操作时间为第二关键字排序. <法一>块状树,线下ac,线上tle…… #include<cstdio> #include<cmath> # ...

随机推荐

  1. codevs 1191 线段树 区间更新(水)

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...

  2. egrep对于conf文件中去掉#注释,排除无用项

    [root@localhost conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf dd

  3. Windows下查看某个端口被哪个服务占用

    1.查看某个端口是否被占用 打开命令行,输入:netstat -ano | findstr "3306" 2.查看端口被哪个服务占用 tasklist | findstr “PID ...

  4. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  5. 【Foreign】朗格拉日计数 [暴力]

    朗格拉日计算 Time Limit: 10 Sec  Memory Limit: 128 MB Description Input Output 仅一行一个整数表示答案. Sample Input 5 ...

  6. bzoj 1067 特判

    这道题的大题思路就是模拟 假设给定的年份是x,y,首先分为4个大的情况,分别是 x的信息已知,y的信息已知 x的信息已知,y的信息未知 x的信息未知,y的情况已知 x的信息未知,y的情况未知 然后对于 ...

  7. js事件中绑定另一事件导致事件多次执行

    1.html代码 <input type="button" value="add"> <input type="button&quo ...

  8. 解决cursor未关闭造成的死锁

    参考:https://blog.csdn.net/zc474235918/article/details/72731363/ https://blog.csdn.net/zmx729618/artic ...

  9. Python 本地线程

    1. 本地线程,保证即使是多个线程,自己的值也是互相隔离. 2.普通对象演示 import threading import time class A(): pass a=A() def func(n ...

  10. C++类学习

    一.C++类的定义     C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性  protected: //行为或属性 private: / ...