题解:P15139 [SWERC 2025] Expansion Plan 2
前置芝士
切比雪夫距离:\(d_{\infty}=\max\left(\left|x\right|,\left|y\right|\right)\),表示 \(8\) 邻域扩展的“层数”;
曼哈顿距离:\(d_1=\left|x\right|+\left|y\right|\),表示正交扩展的“步数”。
思路
目标点 \(\left(x,y\right)\) 被解锁的充要条件:设查询子串的长度为 \(k=r-l+1\)(总操作次数),其中包含 \(c_4\) 个 \(4\)、\(c_8\) 个 \(8\)(\(c_4+c_8=k\)),则:
- 切比雪夫距离:\(d_{\infty}\le k\)
每次操作(无论 \(4\) 还是 \(8\))都会让已解锁区域的切比雪夫距离上限加一,\(k\) 次操作后,切比雪夫距离超过 \(k\) 的点必然无法解锁。
- 曼哈顿距离:\(d_1\le c_4+2\times c_8\)
操作 \(4\) 仅能让曼哈顿距离上限加一(正交扩展),操作 \(8\) 能让曼哈顿距离上限加二(对角扩展),总曼哈顿距离上限为两类操作的贡献之和。
同时满足这两个条件,目标点就会被解锁。
代码
#include<bits/stdc++.h>
#define lcm(x,y) x/__gcd(x,y)*y
#define lb(x) (x&-x)
#define str to_string
#define endl '\n'
#define endr '\r'
#define emp_ '\0'
using namespace std;
#define int long long
using ll=long long;
namespace Fast{
namespace Fread{const int SZ=1<<20;char buf[SZ],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SZ,stdin);if(S==TT)return endl;}return *S++;}}
namespace Fwrite{const int SZ=1<<20;char buf[SZ],*S=buf,*TT=buf+SZ;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct R{
template<typename T>
R&operator>>(T&from){from=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')from=(from<<3)+(from<<1)+(c^48),c=getchar();from*=f;return*this;}
R&operator>>(double&from){from=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(long double&from){from=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(__float128&from){from=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(char&c){c=getchar();while(c==' '||c==endl||c==endr)c=getchar();return*this;}
R&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c==endl||c==endr)c=getchar();while(c!=' '&&c!=endl&&c!=endr)str[len++]=c,c=getchar();str[len]=emp_;return*this;}
R(){}
}cin;
struct W{
const int S=6;
typedef int M;
template<typename T>
W&operator<<(T from){if(from==0){putchar('0');return*this;}if(from<0)putchar('-'),from=-from;static short sta[40];short top=0;while(from>0)sta[++top]=from%10,from/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(long double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(__float128 from){if(from<0)putchar('-'),from=-from;M _=from;from-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(char c){putchar(c);return*this;}
W&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W(){}
}cout;
}
}
// using namespace Fast;
// #define cin FastIO::cin
// #define cout FastIO::cout
const double EPS=1e-6,PAI=acos(-1.0);
const int MAX=2e5+5,mod=1e9+7,MOD=998244353;
int n,_,pre[MAX]; // pre统计字符串中前 i 个字符里 4 的数量,可以 O(1)查询任意子串中 4 的个数;
string s;
void solve(){
cin>>n>>_>>s;
for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(s[i-1]=='4'?1:0);
while(_--){
int l,r,x,y;
cin>>l>>r>>x>>y;
int k=r-l+1; // 总操作次数
int c4=pre[r]-pre[l-1]; // 子串中4的数量
int c8=k-c4; // 子串中8的数量
int q=max(abs(x),abs(y)); // 切比雪夫距离
int m=abs(x)+abs(y); // 曼哈顿距离
if(q<=k&&m<=c4+2*c8)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
signed main(){
int t=1;
// cin>>t;
while(t--)solve();
return 0;
}

浙公网安备 33010602011771号