比赛 2025.4.12 评测结果 AWWAAAWTWTT
题目名称 It s Mooin Time III 最终得分 36
用户昵称 彭欣越 运行时间 5.596 s
代码语言 C++ 内存使用 3.64 MiB
提交时间 2025-04-12 11:31:51
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010;
ll n,q,ans;
vector<int>t[110];
string s;
int main () {
	freopen("Time.in","r",stdin);
	freopen("Time.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> q;
	cin >> s;
	for (int i=0;i<n;i++) {
		t[s[i]-'a'].push_back(i);
	}
	while (q--) {
		int l,r;
		int a,b,c;
		cin >> l >> r;
		l--,r--;
		ans=-1;
		for (int i=0;i<26;i++) {
		    a=lower_bound(t[i].begin(),t[i].end(),l)-t[i].begin();
		    if (a>=t[i].size()||a>=r-1) continue;
		    for (int k=0;k<26;k++) {
		    	if (i==k||t[k].size()<=1) continue;
		    	c=upper_bound(t[k].begin(),t[k].end(),r)-t[k].begin()-1;
		    	if (t[k][c-1]<=t[i][a]) continue;
		    	b=upper_bound(t[k].begin(),t[k].end(),(t[k][c]+t[i][a])/2)-t[k].begin();
		    	if (a<b) {
					ans=max(ans,1LL*(t[k][c]-t[k][b])*(t[k][b]-t[i][a]));
				}
				b--;
				if(b>=0&&t[i][a]<t[k][b]){
					ans=max(ans,1LL*(t[k][c]-t[k][b])*(t[k][b]-t[i][a]));
				}
			} 
		}
		cout << ans <<endl;
	}
	return 0;
}