| 比赛 |
26暑假集训模拟赛2 |
评测结果 |
AAAAAAAAAAA |
| 题目名称 |
It s Mooin Time III |
最终得分 |
100 |
| 用户昵称 |
KKZH |
运行时间 |
1.706 s |
| 代码语言 |
C++ |
内存使用 |
32.36 MiB |
| 提交时间 |
2026-07-02 10:12:11 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m;
int las[N][30],fir[N][30],fi[N][30];
map <int,int> mp;
vector <int> v[30];
string s;
signed main(){
freopen("Time.in","r",stdin);
freopen("Time.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m>>s;
for(int j=1;j<=26;j++){
fi[n+1][j]=n+1;
fir[n+1][j]=n+1;
}
for(int i=1;i<=n;i++){
v[s[i-1]-'a'+1].push_back(i);
mp[i]=v[s[i-1]-'a'+1].size()-1;
for(int j=1;j<=26;j++) las[i][j]=las[i-1][j];
las[i][s[i-1]-'a'+1]=i;
// for(int j=1;j<=26;j++){
// cout<<las[i][j]<<" ";
// }
// cout<<'\n';
}
for(int i=n;i>=1;i--){
for(int j=1;j<=26;j++){
if(s[i-1]-'a'+1==j){
fi[i][j]=i;
}else fi[i][j]=fi[i+1][j];
// cout<<fi[i][j]<<" ";
}
// cout<<'\n';
}
for(int i=n;i>=1;i--){
for(int j=1;j<=26;j++){
if(s[i-1]-'a'+1==j){
fir[i][j]=fir[i+1][j];
}else fir[i][j]=i;
// cout<<fi[i][j]<<" ";
}
// cout<<'\n';
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=26;j++){
// cout<<fir[i][j]<<" ";
// }
// cout<<'\n';
// }
int l,r;
for(int i=1;i<=m;i++){
cin>>l>>r;
long long ans=-1;
for(int j=1;j<=26;j++){
int x=fir[l][j],z=las[r][j];
// cout<<i<<" "<<j<<" "<<x<<" "<<z<<'\n';
if(x>=z-1||x>=r-1||z<=l+1||x<l||z>r||v[j].size()<2) continue;
int L=l,R=r;
L=mp[fi[x][j]],R=mp[z];
// cout<<L<<" "<<R<<'\n';
while(L<R-1){//
// cout<<L<<" "<<R<<'\n';
int mid=(L+R)/2;
if(v[j][mid]*2<=(x+z)){
L=mid;
}else R=mid;
}//
// cout<<1;
L=v[j][L];
// cout<<l<<'\n';
if(L==z||L==x) continue;
int y=mp[L];
ans=max(ans,1ll*(L-x)*(z-L));
if(y<v[j].size()-2){
int u=v[j][y+1];
ans=max(ans,1ll*(u-x)*(z-u));
}
}
if(ans==0) ans--;
cout<<ans<<'\n';
// cout<<i<<'\n';
}
return 0;
}