ARC038C 茶碗と豆

Description

https://www.luogu.com.cn/problem/AT_arc038_c

Solution

首先观察到每个豆是独立的,这启发我们算出每个位置的 sg 值然后异或起来即可。

现在要做的操作是单点加入值,后缀区间求 mex。

一种解法是使用 P4137,另一篇题解有说。

还有一个很蠢又劣的线段树维护对顶堆做法,区间加入一个数表示这个数在这个区间中没有,堆相当于标记永久化。因为 mex 是单调递减的,查询时单点查询即可。复杂度是很劣的 $O(n\log ^2n)$。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 100004
#define put() putchar('\n')
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
using namespace std;
Tp void read(T &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
ll power(ll x,int y=mod-2,int p=mod) {
ll sum=1;x%=p;
while (y) {
if (y&1) sum=sum*x%p;
x=x*x%p;y>>=1;
}
return sum;
}
int n,a[maxn],c[maxn];
int las[maxn],ans,sg[maxn];
const int inf=1e9;
priority_queue<int,vector<int> ,greater<int> >q1[maxn<<2],q2[maxn<<2];
int f[maxn<<2];
void Update(int l,int r,int rt,int head,int tail,int k,int flag) {
if (head<=l&&r<=tail) {
if (flag==1) {
q1[rt].push(k);
}
else {
q2[rt].push(k);
}
while (!q1[rt].empty()&&!q2[rt].empty()&&q1[rt].top()==q2[rt].top()) q1[rt].pop(),q2[rt].pop();
if (l!=r) f[rt]=min(f[rt<<1],f[rt<<1|1]);else f[rt]=inf;
f[rt]=min(f[rt],!q1[rt].empty()?q1[rt].top():inf);
return ;
}
int mid=l+r>>1;
if (head<=mid) Update(l,mid,rt<<1,head,tail,k,flag);
if (tail>mid) Update(mid+1,r,rt<<1|1,head,tail,k,flag);
if (l!=r) f[rt]=min(f[rt<<1],f[rt<<1|1]);else f[rt]=inf;
f[rt]=min(f[rt],!q1[rt].empty()?q1[rt].top():inf);
}
int Query(int l,int r,int rt,int head,int tail) {
if (head<=l&&r<=tail) return f[rt];
int mid=l+r>>1,tmp1=inf,tmp2=inf,tmp;
if (head<=mid) tmp1=Query(l,mid,rt<<1,head,tail);
if (tail>mid) tmp2=Query(mid+1,r,rt<<1|1,head,tail);
tmp=min(min(tmp1,tmp2),!q1[rt].empty()?q1[rt].top():inf);
// gdb(l,r,rt,head,tail,tmp,tmp1,tmp2);
return tmp;
}
signed main(void){
int i;
read(n);
memset(f,0x3f,sizeof(f));
for (i=1;i<=n+1;i++) Update(0,n,1,0,n,i,1),las[i]=-1;
Update(0,n,1,1,n,0,1);las[0]=0;
for (i=1;i<n;i++) {
read(c[i]);read(a[i]);
sg[i]=Query(0,n,1,i-c[i],i-c[i]);
Update(0,n,1,las[sg[i]]+1,n,sg[i],0);
Update(0,n,1,i+1,n,sg[i],1);
las[sg[i]]=i;
if (a[i]&1) ans^=sg[i];
// gdb(i,sg[i],las[sg[i]]);
}
puts(ans?"First":"Second");
return 0;
}
//i=begin && g++ $i.cpp -o $i -std=c++14 && ./$i