题解:P10662 BZOJ4202 石子游戏
XiaoZi_qwq · · 题解
题目描述
给定一个
一次游戏的定义如下:
两个玩家轮流行动,初始时
特别地,每次游戏独立。
有
-
询问在以
u 点为根的子树内进行游戏,问先手是否必胜; -
修改一个点的点权;
-
添加一个新点。
强制在线。
数据范围:
分析
有 LCT 的做法,但是我不会;我们来点人人都会的分块做法。
先考虑不带修怎么做。
首先这个游戏形式就很像阶梯 Nim(因为如果移动到偶数深度的节点,那后手一定可以再移动到奇数深度的节点),然后发现对于每一个节点,它 SG 函数的值等于
然后再考虑修改。
看到
假设块长为
每
每一次询问中:因为已经预处理了整块的贡献,使用只需要处理散块的贡献即可。暴力枚举每一次在散块中的操作,如果当前操作的点在询问点的子树内且和询问的距离为奇数,那就维护贡献,时间复杂度
总时间复杂度为
然后稍微注意一点细节即可。
代码
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define Inf (1ll<<60)
#define For(i,s,t) for(int i=s;i<=t;++i)
#define Down(i,s,t) for(int i=s;i>=t;--i)
#define ls (i<<1)
#define rs (i<<1|1)
#define bmod(x) ((x)>=mod?(x)-mod:(x))
#define lowbit(x) ((x)&(-(x)))
#define End {printf("NO\n");exit(0);}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
inline void ckmx(int &x,int y){x=(x>y)?x:y;}
inline void ckmn(int &x,int y){x=(x<y)?x:y;}
inline void ckmx(ll &x,ll y){x=(x>y)?x:y;}
inline void ckmn(ll &x,ll y){x=(x<y)?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline ll min(ll x,ll y){return x<y?x:y;}
inline ll max(ll x,ll y){return x>y?x:y;}
namespace IO{
char ibuf[1<<20],*is,*ie;
#define gc() (is == ie ? (ie = ibuf + fread(is = ibuf, 1, 1 << 20, stdin), is == ie ? EOF : *is++) : *is++)
template<typename T>
void read(T &x){
x=0;
int f = 1;
char c = gc();
while(c < '0' || c > '9') f = (c == '-') ? -1 : 1, c = gc();
while(c >= '0' && c <= '9') x = x * 10 + (c & 15), c = gc();
x*=f;
}
char obuf[1<<20],*os=obuf,*oe=obuf+(1<<20)-1;
void flush(){
fwrite(obuf,1,os-obuf,stdout);
os=obuf;
}
void pc(char c){
*os++=c;
if(os==oe) flush();
}
template<typename T>
void write(T x){
if(x<=9){
putc(x+'0');
return;
}
write(x/10);
putc(x+'0');
}
#undef gc
}
using IO::read;using IO::write;using IO::flush;using IO::pc;
const int N=1e5+1000,SZ=230,Sz=224;
int n,m,q,mod,a[N],b[N];
bool vis[N];
struct Edge{int from,to;}e[N<<1];
int num,h[N];
void add(int f,int t){e[++num].from=h[f],e[num].to=t,h[f]=num;}
int sz[N],dep[N],dfn[N],dfu,f[N],F[SZ][SZ];
int val[N][2];
void dfs(int u,int fa){
dfn[u]=++dfu,sz[u]=1,vis[u]=1,f[u]=0;
dep[u]=dep[fa]+1;
val[u][0]=a[u];
val[u][1]=0;
for(int i=h[u];i;i=e[i].from){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
sz[u]+=sz[v];
val[u][0]=val[u][0]^val[v][1];
val[u][1]=val[u][1]^val[v][0];
}
}
int cnt=0,p[N];
int find(int x){return p[x]=(p[x]==x)?x:find(p[x]);}
struct Act{int u,val;}pt[N];
void rebuild(){
For(i,1,cnt) b[pt[i].u]=a[pt[i].u]=pt[i].val;
dfu=cnt=0;
dfs(1,0);
}
int lstans,res;
void solve(int p){
res=val[p][1];
For(i,1,cnt){
int u=pt[i].u,x=pt[i].val;
if(dep[u]%2==dep[p]%2) continue;
/*
本题最大细节:关于询问到在散块中加入的点怎么办
答案是:
因为对于这样的询问,可能对答案有影响的修改一定是来自新加入的点;
那么我们预处理每一个新点的 k 级祖先,且只记录是新点的祖先
询问时 O(1) 即可。
时间复杂度分析:
一个新点至多预处理其 O(\sqrt n) 级祖先,全局至多加 n 个新点
时间复杂度 O(n\sqrt n)
*/
if(!vis[p]){
if(vis[u] || dep[u]<=dep[p] || F[i][dep[u]-dep[p]]!=p)
continue;
}
else if(dfn[u]<dfn[p] || dfn[p]+sz[p]<=dfn[u])
continue;
res^=b[u]^x;
b[u]=x;
}
For(i,1,cnt)
b[pt[i].u]=a[pt[i].u];
printf(res ? "Yes\n":"No\n");
// if(res) pc('Y'),pc('e'),pc('s');
// else pc('N'),pc('o');
// pc('\n');
lstans+=(res!=0);
}
int main()
{
read(n),read(m),mod=m+1;
For(i,1,n) read(a[i]),a[i]%=mod,b[i]=a[i];
int u,v;
For(i,2,n) read(u),read(v),add(u,v),add(v,u);
read(q);
rebuild();
int opt,x;
For(i,1,q){
if(i%Sz==0) rebuild();//暴力重构
read(opt),read(u),u^=lstans;
if(opt==1)
solve(u);
else if(opt==2)
read(x),x=(x^lstans)%mod,pt[++cnt]=(Act){u,x};
else if(opt==3){
read(v),read(x);
v=v^lstans,x=(x^lstans)%mod;
dep[v]=dep[u]+1,f[v]=u;
add(u,v),add(v,u),dfn[v]=dfn[u];
pt[++cnt]=(Act){v,x};
int w=v,tim=0;
while(w) F[cnt][tim]=w,w=f[w],++tim;
}
}
// flush();
return 0;
}