ABC255G 题解
DaiRuiChen007 · · 题解
Problem Link
题目大意
给定
n 堆石子的 Nim 游戏,有m 个限制表示:当前堆大小为x 时不能取y 个石子,求最终谁必胜。数据范围:
n,m\le 2\times 10^5 。
思路分析
考虑算 SG 函数,我们需要特殊处理
首先如果一个
因此用 map 存每个突变点
然后考虑突变点,我们要在
观察一下函数图像可以发现,对于所有非突变点,他们的 SG 函数值单调递增(这是显然的),且中间不存在断点:可以证明每个突变点取值一定严格变小,即
因此 map 维护。
时间复杂度
代码呈现
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=2e5+5;
map <ll,vector<ll>> lim;
map <ll,ll> F,c,q;
ll a[MAXN];
ll sg(ll x) {
auto it=*--F.upper_bound(x);
return it.second+x-it.first;
}
signed main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
for(int i=1;i<=m;++i) {
ll x,y; scanf("%lld%lld",&x,&y);
lim[x].push_back(x-y);
}
ll mx=0; F[0]=0;
for(auto &I:lim) {
ll x=I.first;
mx=max(mx,sg(x-1));
q.clear();
for(ll y:I.second) ++q[sg(y)];
for(auto it:q) {
if(it.second>=c[it.first]+1) {
++c[F[x]=it.first]; break;
}
}
mx=max(mx,sg(x)),F[x+1]=mx+1;
}
ll ans=0;
for(int i=1;i<=n;++i) ans^=sg(a[i]);
puts(ans?"Takahashi":"Aoki");
return 0;
}