[AGC064C] Erase and Divide Game
Genius_Star · · 题解
或许更好的阅读体验。
大牛逼题。
思路:
先考虑如果给
-
初始在根节点。
-
每次操作可以选择走左子树或者走右子树。
-
最先走到叶子的人赢。
于是可以直接在这个树上 dp,定义
于是可以
考虑一种数据结构方式来把这些区间的数构成的 01trie 隐式的表示出来,具体的,对于一个
然后考虑
最后的 trie 树是所有
容易想到按照深度分层,对于
然后考虑自底往上合并,设
于是你通过可持久化 trie 的维护方法把所有区间并的 trie 隐式的建出来了,在这上面 dp 即可,时空复杂度都是
注意合并是有细节的,有些根结点是空,是不能合并上去的,这样直接 dp 更新可能会有小问题。
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e4 + 10, M = N * 60 * 60, LW = 60;
const ll inf = (1ll << 60) - 1;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
struct Node{
int dp;
int ch[2];
}X[M];
int T, n, cnt;
int rt[LW];
ll l[N], r[N];
inline void pushup(int k){
if(X[X[k].ch[0]].dp && X[X[k].ch[1]].dp)
X[k].dp = 0;
else
X[k].dp = 1;
}
inline void insert(int p, ll x, int dep){
if(!dep){
X[p].dp = 1;
return ;
}
bool c = (x & 1);
if(!X[p].ch[c])
X[p].ch[c] = ++cnt;
insert(X[p].ch[c], x >> 1, dep - 1);
pushup(p);
}
inline void update(ll l, ll r, ll L, ll R, ll now, int dep){
if(L <= l && r <= R){
// cout << "ins: " << dep << ' ' << l << ' ' << r << ' ' << now << '\n';
insert(rt[dep], now, LW - dep);
return ;
}
ll mid = (l + r) >> 1;
if(L <= mid)
update(l, mid, L, R, now << 1, dep - 1);
if(R > mid)
update(mid + 1, r, L, R, now << 1 | 1, dep - 1);
}
inline int merge(int x, int y){
if(!x || !y)
return x | y;
int now = ++cnt;
X[now].ch[0] = merge(X[x].ch[0], X[y].ch[0]);
X[now].ch[1] = merge(X[x].ch[1], X[y].ch[1]);
pushup(now);
return now;
}
inline void solve(){
for(int i = 0; i <= cnt; ++i)
X[i] = {0, {0, 0}};
cnt = LW;
for(int i = 0; i < LW; ++i)
rt[i] = i + 1, pushup(rt[i]);
n = read();
while(n--){
ll l = read(), r = read();
update(0, inf, l, r, 0, LW);
}
// cout << X[rt[0]].dp << '\n';
for(int i = LW - 2; i >= 0; --i){
if(!X[rt[i + 1]].ch[0] && !X[rt[i + 1]].ch[1])
continue;
X[rt[i]].ch[0] = merge(X[rt[i]].ch[0], rt[i + 1]);
X[rt[i]].ch[1] = merge(X[rt[i]].ch[1], rt[i + 1]);
pushup(rt[i]);
}
// cout << X[rt[0]].dp << ' ' << X[X[rt[0]].ch[0]].ch[1] << '\n';
// for(int i = 1; i <= cnt; ++i){
// cout << i << ' ' << X[i].dp << ' ' << X[i].ch[0] << ' ' << X[i].ch[1] << '\n';
// }
if(X[rt[0]].dp)
puts("Takahashi");
else
puts("Aoki");
}
int main(){
T = read();
while(T--)
solve();
return 0;
}