朴素的 dp 是简单的,设 表示从 到 游戏,初始石子个数为 ,是否能赢。
我们考虑直接维护 ,输和赢一段一段交替出现,最后一段是平局。
考虑维护 的差分,即相邻两段的分界线。
发现每次其实会给输的段的 做加法:,然后把所有段的输赢状态取反。
用差分描述:把第奇数个 往右平移 单位,把第偶数个 往右平移 单位,然后给位置 赋值成 。
考虑维护这些 的位置,我们需要维护奇偶两个全局加标记。
注意到有些平移后输的段可能满足 ,此时我们需要消去对应的两个 。
使用两个堆 ,分别维护奇偶两种,相邻的 之间的距离。若距离为 ,就可以消去。有个细节:如果下一段是平局,那么只需要消除后面那个 。
每次取出 或 进行更新即可。
我们还需要动态支持删除一些 ,考虑使用链表维护每个 的位置即可。
时间复杂度 。
#include<bits/stdc++.h>
#define ll long long
#define pir pair<ll,ll>
#define mkp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const ll maxn=1e6+10;
ll n,m,l[maxn],r[maxn],pre[maxn],nxt[maxn],vis[maxn],pos[maxn],tag0,tag1,tot=2,hd,x[maxn],len,g;
set<pir>st0,st1;
int main(){
// freopen("p.in","r",stdin);
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
scanf("%lld%lld",l+i,r+i);
}
pre[2]=1, nxt[1]=2;
pos[1]=0, pos[2]=l[n];
hd=1; g=0;
st0.insert(mkp(l[n],1));
for(ll i=n-1;i;i--){
if(g==0){
tag0+=l[i], tag1+=r[i];
while(!st1.empty()&&st1.begin()->fi+tag0-tag1<=0){
pir t=*st1.begin(); st1.erase(st1.begin());
ll x=t.se, y=nxt[x];
if(!nxt[y]){
nxt[x]=0;
continue;
}
if(!pre[x]) hd=nxt[y];
nxt[pre[x]]=nxt[y], pre[nxt[y]]=pre[x];
if(pre[x]) st0.erase(mkp(pos[x]-pos[pre[x]],pre[x]));
if(nxt[y]) st0.erase(mkp(pos[nxt[y]]-pos[y],y));
if(pre[x]&&nxt[y]) st0.insert(mkp(pos[nxt[y]]-pos[pre[x]],pre[x]));
}
++tot, pos[tot]=-tag1;
g^=1;
nxt[tot]=hd, pre[hd]=tot;
st1.insert(mkp(pos[hd]-pos[tot],tot)), hd=tot;
}
else{
tag1+=l[i], tag0+=r[i];
while(!st0.empty()&&st0.begin()->fi+tag1-tag0<=0){
pir t=*st0.begin(); st0.erase(st0.begin());
ll x=t.se, y=nxt[x];
if(!nxt[y]){
nxt[x]=0;
continue;
}
if(!pre[x]) hd=nxt[y];
nxt[pre[x]]=nxt[y], pre[nxt[y]]=pre[x];
if(pre[x]) st1.erase(mkp(pos[x]-pos[pre[x]],pre[x]));
if(nxt[y]) st1.erase(mkp(pos[nxt[y]]-pos[y],y));
if(pre[x]&&nxt[y]) st1.insert(mkp(pos[nxt[y]]-pos[pre[x]],pre[x]));
}
++tot, pos[tot]=-tag0;
g^=1;
nxt[tot]=hd, pre[hd]=tot;
st0.insert(mkp(pos[hd]-pos[tot],tot)), hd=tot;
}
}
ll u=hd;
while(u){
x[++len]=pos[u]+(g&1? tag1:tag0); g^=1;
u=nxt[u];
}
scanf("%lld",&m);
while(m--){
ll c; scanf("%lld",&c);
ll tmp=upper_bound(x+1,x+1+len,c)-x-1;
if(tmp==len) puts("Draw");
else puts((tmp^1)&1? "Alice":"Bob");
}
return 0;
}