思路:
因为长度小于等于10,我们可以对于每个长度开一个set存每个长度中符合要求的两个下标i,j
之后用二分lowerbound对于每个长度去查找即可 (还有这题数据也弱了,测了一下很多代码都是超时的)
#include<bits/stdc++.h>
using namespace std;
string s;
int n,m;
set<pair<int,int>>se[11];
void solve(){
int l,r;
cin>>l>>r;
for(int i=10;i>=1;i--){
auto it=se[i].lower_bound({l,l});
if(it==se[i].end())continue;
if(it->first>=l&&it->second<=r){
cout<<i<<'\n';
return;
}
}
cout<<"0\n";
}
signed main() {
ios::sync_with_stdio(false),cin.tie(0);
cin>>n>>s;
s="1"+s;
cin>>m;
int cnt=0;
for(int i=1;i<=n;i++){//预处理
int ans=1;
for(int j=i;j<=min(n,i+9);j++){
if(s[j]=='1')ans++;
else ans--;
if(ans<=0)break;
if(ans==1)se[j-i+1].insert({i,j});
}
}
while(m--){
solve();
}
}