FZU 2240 Daxia & Suneast's problem

时间:2023-03-09 09:03:30
FZU 2240 Daxia & Suneast's problem

博弈,$SG$函数,规律,线段树。

这个问题套路很明显,先找求出$SG$函数值是多少,然后异或起来,如果是$0$就后手赢,否则先手赢。修改操作和区间查询的话可以用线段树维护一下区间异或和。

数据那么大,一看就知道$SG$有规律......

先写个小数据的$SG$找规律:

bool f[];
int sg[]; int SG(int x)
{
memset(f,,sizeof f);
for(int i=x-;i>=x/;i--)
{
if(x-i>i) break;
f[sg[i]]=;
}
for(int i=;i<=;i++)
{
if(f[i]==) continue;
return i;
}
}

会发现是这样的东西:

LL SG(LL x)
{
if(x%==) return x/;
return SG(x/);
}

注意:$FZU$上$\% lld$会出问题,我用Visual C++交才过的。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} LL SG(LL x)
{
if(x%==) return x/;
return SG(x/);
} const int maxn=;
int n,m;
LL s[*maxn]; void build(int l,int r,int rt)
{
s[rt]=;
if(l==r)
{
scanf("%lld",&s[rt]);
s[rt]=SG(s[rt]);
return;
}
int m=(l+r)/;
build(l,m,*rt);
build(m+,r,*rt+);
s[rt]=s[*rt]^s[*rt+];
} void update(int p,LL v,int l,int r,int rt)
{
if(l==r) { s[rt]=v; return;} int m=(l+r)/;
if(p<=m) update(p,v,l,m,*rt);
else update(p,v,m+,r,*rt+); s[rt]=s[*rt]^s[*rt+];
} LL get(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return s[rt];
int m=(l+r)/; LL x1=,x2=;
if(L<=m) x1=get(L,R,l,m,*rt);
if(R>m) x2=get(L,R,m+,r,*rt+); return x1^x2;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
build(,n,);
for(int i=;i<=m;i++)
{
int p,L,R; LL x;
scanf("%d%lld%d%d",&p,&x,&L,&R);
x=SG(x); update(p,x,,n,);
LL y=get(L,R,,n,);
if(y) printf("daxia\n");
else printf("suneast\n");
}
}
return ;
}