| 比赛 |
26暑假集训模拟赛2 |
评测结果 |
AAAAAAAAAAA |
| 题目名称 |
It s Mooin Time III |
最终得分 |
100 |
| 用户昵称 |
djyqjy |
运行时间 |
0.691 s |
| 代码语言 |
C++ |
内存使用 |
6.52 MiB |
| 提交时间 |
2026-07-02 09:17:17 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pir pair<int,int>
#define fi first
#define se second
using namespace std;
inline int re()
{
char c=getchar();
int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int N=100010;
int n,q;
char c[N];
vector<int> st[N];
int query(int l,int r)
{
int sepos=n+1;
for(int i=1;i<=26;i++)
{
if(c[l]-'a'+1==i) continue;
int p=lower_bound(st[i].begin(),st[i].end(),l)-st[i].begin();
if(p!=st[i].size()&&st[i][p]<=r) sepos=min(sepos,st[i][p]);
}
if(sepos==n+1) return -1;
int ans=-1;
for(int i=1;i<=26;i++)
{
int pz=upper_bound(st[i].begin(),st[i].end(),r)-st[i].begin();pz--;
int px=sepos;
if(c[l]-'a'+1!=i) px=l;
if(pz<=0||st[i][pz-1]<px) continue;
int py=lower_bound(st[i].begin(),st[i].end(),(px+st[i][pz]+1)/2)-st[i].begin();
if(py!=st[i].size()&&py<pz) ans=max(ans,(st[i][pz]-st[i][py])*(st[i][py]-px));
py--;if(py>=0&&px<=st[i][py]) ans=max(ans,(st[i][pz]-st[i][py])*(st[i][py]-px));
}
return ans;
}
signed main()
{
freopen("Time.in","r",stdin);
freopen("Time.out","w",stdout);
n=re();q=re();
scanf("%s",c+1);
for(int i=1;i<=n;i++) st[c[i]-'a'+1].pb(i);
for(int i=1;i<=q;i++)
{
int l=re(),r=re();
printf("%lld\n",query(l,r));
}
return 0;
}