比赛 26暑假集训模拟赛2 评测结果 AAAAAAAAAAA
题目名称 It s Mooin Time III 最终得分 100
用户昵称 RpUtl 运行时间 1.384 s
代码语言 C++ 内存使用 14.34 MiB
提交时间 2026-07-02 10:19:57
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10; 
typedef long long ll;
set<int>pos[30];
int n, q, nxt[N][26];
char s[N];
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 + 1);
    for (int i = 1; i <= n; i++) {
        int c = s[i] - 'a';
        pos[c].insert(i);
        for (int j = 0; j < 26; j++) {
        	if (c != j) nxt[i][j] = i;
		}
    } 
    for (int j = 0; j < 26; j++) {
    	for (int i = n; i >= 1; i--) {
    		if (!nxt[i][j]) nxt[i][j] = nxt[i + 1][j];
		}
	}
    int l, r;
    ll x, y, z, ans;
    while (q--) {
        cin >> l >> r;
        ans = -1;
        for (int j = 0; j < 26; j++) {
        	auto it = pos[j].lower_bound(r + 1);
        	if (it != pos[j].begin()) {
                --it;
                y = (*it);
                int x = nxt[l][j];
                if (x && x <= y && y > l) {
                	int mid = (x + y) >> 1;
                    it = pos[j].lower_bound(mid);
                    if (it != pos[j].end()) {
                        z = (*it);
                        if (x < z && z < y) {
                            ans = max(ans, (z - x) * (y - z));
                        } 
                    }
                    if (it != pos[j].begin()) {
                        it--;
                        z = (*it);
                        if (x < z && z < y) {
                            ans = max(ans, (z - x) * (y - z));
                        } 
                    }
				}   
            }
		}
        cout << ans << '\n';
    }
    return 0;
}