跳到正文

题解

题解:P10662 BZOJ4202 石子游戏

XiaoZi_qwq约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年8月25日
收录
2026年4月22日
更新
2026年11月14日
篇幅
约 9 分钟
题目
P10662

版本

  1. v2当前
  2. v1

题目描述

给定一个 nn 个点的树,每一个点有点权 aia_i 。再给定一个数 mm。

一次游戏的定义如下:

两个玩家轮流行动,初始时 ii 号节点上有 aia_i 个石子。每一次行动可以选择一个根节点以外的节点,将该点上的石子往其父亲移动 11 到 mm 个。最后不能移动者输。

特别地,每次游戏独立。

有 tt 次操作,每次操作形如:

  • 询问在以 uu 点为根的子树内进行游戏,问先手是否必胜;

  • 修改一个点的点权;

  • 添加一个新点。

强制在线。

数据范围:n,t≤5×104n,t \le 5 \times 10^4。

分析

有 LCT 的做法,但是我不会;我们来点人人都会的分块做法。

先考虑不带修怎么做。

首先这个游戏形式就很像阶梯 Nim(因为如果移动到偶数深度的节点,那后手一定可以再移动到奇数深度的节点),然后发现对于每一个节点,它 SG 函数的值等于 ai mod (m+1)a_i \bmod (m+1)(证明可以自行搜素“巴什博弈“)。因此对于每个节点,我们只需要维护它子树内和它距离为奇数的节点的 SG 函数值的异或和。

然后再考虑修改。

看到 5×104 5 \times 10^4,容易想到带根号的算法,于是考虑时间轴分块(操作分块?)。

假设块长为 BB。

每 BB 次操作我们就暴力搜索一次来维护之前操作的贡献,时间复杂度 O(nqB)O(\frac{nq}{B})。

每一次询问中:因为已经预处理了整块的贡献,使用只需要处理散块的贡献即可。暴力枚举每一次在散块中的操作,如果当前操作的点在询问点的子树内且和询问的距离为奇数,那就维护贡献,时间复杂度 O(qB)O(qB)。

总时间复杂度为 O(nqB+qB)≥O(qn)O(\frac{nq}{B}+qB) \ge O(q\sqrt n),当且仅当 B=nB=\sqrt n 时,等号成立。

然后稍微注意一点细节即可。

代码

C++
#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;
}
原文
存档评论1 条抓取于