题解:P12073 [OOI 2025] Alice, Bob, and two arrays.
直接写出两个数组
遍历第一个玩家放置的字符,并计算
我们按降序进行遍历,先遍历
观察发现,如果我们进行了一次移动,一定会到达位置
找出包含
我们想找到所有满足
否则,即没有能转移到的必败位置,那么除
最后一个结论是,如果通过一个新字符进行转移(即除
利用这些结论,按以下步骤进行操作。
-
仅针对块开头的位置存储
{f} 。 -
从后往前遍历,为这些位置计算
{f} 。 -
遍历"其他"字符的转移情况。
-
如果有一个转移到达必败位置,那么位置
(x, y) 是必胜位置。 -
否则,通过字符
{a}_{x} 进行转移,直到进入一个新的块。
时间复杂度
采取和
考虑所有颜色为
我们会在当前对角线上移动,直到一个可以转移到必败状态的对(通过"其他"字符转移)。因此,我们只需存储这条对角线上一个可以转移到必败状态的对。
实现可以将询问离线,对每种字符开一个 map,维护区间赋值和单点查询,时间复杂度
/*
* /$$ /$$
* |__/ |__/
* /$$$$$$$$ /$$ /$$$$$$$$ /$$ /$$$$$$
* |____ /$$/| $$|____ /$$/| $$ /$$__ $$
* /$$$$/ | $$ /$$$$/ | $$| $$ \ $$
* /$$__/ | $$ /$$__/ | $$| $$ | $$
* /$$$$$$$$| $$ /$$$$$$$$| $$| $$$$$$$
* |________/|__/|________/|__/ \____ $$
* | $$
* | $$
* |__/
*/
#include<bits/stdc++.h>
#define mk make_pair
using namespace std;
mt19937 engine(chrono::steady_clock().now().time_since_epoch().count());
const int inf=2e9;
const int MAXN=1605;
const int MAXM=1e6+5;
int N,n,M,m,K,Q;
pair<int,int>a[MAXN],b[MAXN];
int calcA(int L)
{
return lower_bound(a,a+n+1,mk(L,0))-a;
}
int calcB(int L)
{
return lower_bound(b,b+m+1,mk(L,0))-b;
}
struct SubA {
static const int maxN=5e3+5;
bool f[maxN][maxN];
int A[maxN],B[maxN],pA[MAXN],pB[MAXN];
void init()
{
int last=0;
for(int i=1;i<=n;i++) {
for(int j=last+1;j<=last+a[i].first;j++) {
A[j]=a[i].second;
}
last+=a[i].first;
}
last=0;
for(int i=1;i<=m;i++) {
for(int j=last+1;j<=last+b[i].first;j++) {
B[j]=b[i].second;
}
last+=b[i].first;
}
}
void solve()
{
for(int i=N;i>=0;i--) {
for(int j=0;j<=K;j++) pA[j]=pB[j]=0;
for(int j=N;j>i;j--) pA[A[j]]=j;
int sum=0;
for(int j=M;j>=0;j--) {
f[i][j]=bool(sum);
int c=B[j];
sum-=(pA[c]&&pB[c]&&(!f[pA[c]][pB[c]]));
pB[c]=j;
sum+=(pA[c]&&pB[c]&&(!f[pA[c]][pB[c]]));
}
}
while(Q--) {
int x,y;
cin>>x>>y;
cout<<(f[x][y]?"Yes":"No")<<"\n";
}
}
}subA;
struct SubB {
bool f[MAXN][MAXN];
int aR[MAXN][MAXN],bR[MAXN][MAXN],pA[MAXN],pB[MAXN],lst[MAXN],g[MAXN][MAXN];
void init()
{
for(int i=1;i<=n;i++) {
a[i].first+=a[i-1].first;
}
for(int i=1;i<=m;i++) {
b[i].first+=b[i-1].first;
}
for(int i=0;i<=K;i++) lst[i]=n+1;
for(int i=n;i>=0;i--) {
for(int j=0;j<=K;j++) aR[i][j]=lst[j];
lst[a[i].second]=i;
}
for(int i=0;i<=K;i++) lst[i]=m+1;
for(int i=m;i>=0;i--) {
for(int j=0;j<=K;j++) bR[i][j]=lst[j];
lst[b[i].second]=i;
}
}
bool calc(int x,int y,int idA,int idB)
{
if(idA==n+1||idB==m+1) return true;
if(x==a[idA].first&&y==b[idB].first) return bool(g[idA][idB]);
int c=(x!=a[idA].first?a[idA].second:b[idB].second);
if(g[idA][idB]-(aR[idA][c]!=n+1&&bR[idB][c]!=m+1&&!f[aR[idA][c]][bR[idB][c]])) return true;
int xL=a[idA].first-x+1,yL=b[idB].first-y+1,px=aR[idA][c],py=bR[idB][c];
if(xL<yL) return calc(a[px-1].first+1,y+xL,px,idB)^(xL&1);
else if(xL>yL) return calc(x+yL,b[py-1].first+1,idA,py)^(yL&1);
else return calc(a[px-1].first+1,b[py-1].first+1,px,py)^(yL&1);
}
void solve()
{
for(int i=n;i>=0;i--) {
int sum=0;
for(int j=1;j<=K;j++) pA[j]=pB[j]=0;
for(int j=n;j>i;j--) pA[a[j].second]=j;
for(int j=m;j>=0;j--) {
g[i][j]=sum;
if(a[i].second==b[j].second) f[i][j]=calc(i?a[i-1].first+1:0,j?b[j-1].first+1:0,i,j);
int c=b[j].second;
sum-=(pA[c]&&pB[c]&&!f[pA[c]][pB[c]]);
pB[c]=j;
sum+=(pA[c]&&pB[c]&&!f[pA[c]][pB[c]]);
}
}
while(Q--) {
int x,y;
cin>>x>>y;
cout<<(calc(x,y,calcA(x),calcB(y))?"Yes":"No")<<"\n";
}
}
}subB;
struct SubC {
struct Data {
map<int,pair<int,int>>Map;
Data() {
Map[inf]=mk(0,0);
Map[-inf]=mk(0,0);
}
auto split(int x)
{
auto it=Map.lower_bound(x);
if((it->first)==x) return it;
auto y=((--it)->second);
return Map.emplace(x,y).first;
}
void modify(int l,int r,pair<int,int>x)
{
if(l>r) return;
auto R=split(r+1),L=split(l);
Map.erase(L,R);
Map[l]=x;
}
auto query(int x)
{
return (--Map.lower_bound(x+1))->second;
}
}p[MAXN];
struct info {
int x,y;
}pq[MAXM];
vector<int>nq[MAXN][MAXN];
bool ans[MAXM],f[MAXN][MAXN],g[MAXN];
int preA[MAXN][MAXN],preB[MAXN][MAXN],pA[MAXN],pB[MAXN];
void init()
{
for(int i=1;i<=n;i++) {
for(int j=1;j<=K;j++) {
preA[i][j]=preA[i-1][j];
}
preA[i][a[i].second]+=a[i].first;
}
for(int i=1;i<=m;i++) {
for(int j=1;j<=K;j++) {
preB[i][j]=preB[i-1][j];
}
preB[i][b[i].second]+=b[i].first;
}
for(int i=1;i<=n;i++) {
a[i].first+=a[i-1].first;
}
for(int i=1;i<=m;i++) {
b[i].first+=b[i-1].first;
}
for(int i=1;i<=Q;i++) {
cin>>pq[i].x>>pq[i].y;
nq[calcA(pq[i].x+1)][calcB(pq[i].y+1)].emplace_back(i);
}
for(int i=1;i<=K;i++) {
int sA=preA[n][i],sB=preB[m][i];
p[i].modify(-sB,sA-sB,mk(0,sB));
p[i].modify(sA-sB,sA,mk(sA,0));
}
}
bool calc(int x,int y,int idA,int idB)
{
int c=a[idA].second,sA=preA[idA-1][c]+x-a[idA-1].first,sB=preB[idB-1][c]+y-b[idB-1].first,pos=sA-sB;
auto tmp=p[c].query(pos);
int qA=tmp.first,qB=tmp.second;
if(qA-sA>qB-sB) return (qA+sA)&1;
else return (qB+sB)&1;
}
void solve()
{
for(int i=1;i<=n+1;i++) f[i][m+1]=true;
for(int i=1;i<=m+1;i++) f[n+1][i]=true;
for(int i=n;i>=1;i--) {
int sum=0;
for(int j=1;j<=K;j++) {
g[j]=true;
pA[j]=n+1,pB[j]=m+1;
}
for(int j=n;j>i;j--) pA[a[j].second]=j;
for(int j=m;j>=1;j--) {
int c=b[j].second;
sum-=(!g[c]);
if(a[i].second==b[j].second) {
if(sum) {
p[c].modify((preA[i-1][c]+1)-preB[j][c],preA[i][c]-(preB[j-1][c]+1),mk(preA[i-1][c],preB[j-1][c]));
for(auto id:nq[i][j]) ans[id]=true;
f[i][j]=true;
}
else {
for(auto id:nq[i][j]) ans[id]=calc(pq[id].x,pq[id].y,i,j);
f[i][j]=calc(a[i-1].first+1,b[j-1].first+1,i,j);
}
}
else assert(nq[i][j].empty());
pB[c]=j;
g[c]=f[pA[c]][pB[c]];
sum+=(!g[c]);
}
}
for(int i=1;i<=Q;i++) {
cout<<(ans[i]?"Yes":"No")<<"\n";
}
}
}subC;
int main()
{
// freopen("array.in","r",stdin);
// freopen("array.out","w",stdout);
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>N>>n>>M>>m>>K>>Q;
for(int i=1;i<=n;i++) {
cin>>a[i].first>>a[i].second;
}
for(int i=1;i<=m;i++) {
cin>>b[i].first>>b[i].second;
}
if(N<=5000&&M<=5000) {
subA.init();
subA.solve();
return 0;
}
if(n<=800&&m<=800) {
subB.init();
subB.solve();
return 0;
}
subC.init();
subC.solve();
return 0;
}