2066: [USACO 2025 US Open Contest, Bronze] Problem 3. It's Mooin' Time III

文件提交:无需freopen 内存限制:256 MB 时间限制:16.000 S
评测方式:普通裁判 命题人:
提交:1 解决:1

题目描述

Elsie is trying to describe her favorite USACO contest to Bessie, but Bessie is having trouble understanding why Elsie likes it so much. Elsie says "And It's mooin' time! Who wants a mooin'? Please, I just want to do USACO". 

Bessie still doesn't understand, so she transcribes Elsie's description in a string of length  () containing lowercase alphabetic characters . Elsie considers a string  containing three characters a moo if  and .

A triplet  is valid if  and string  forms a moo. For the triplet, FJ performs the following to calculate its value:

  • FJ bends string  90-degrees at index 
  • The value of the triplet is twice the area of . 

In other words, the value of the triplet is .

Bessie asks you  () queries. In each query, she gives you two integers  and  (, ) and ask you for the maximum value among valid triplets  such that  and . If no valid triplet exists, output . 

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

输入

The first line contains two integers N and Q. 

The following line contains .

The following  lines contain two integers  and , denoting each query.

输出

Output the answer for each query on a new line.

样例输入

12 5
abcabbacabac
1 12
2 7
4 8
2 5
3 10

样例输出

28
6
1
-1
12

提示

For the first query, (i, j, k) must satisfy 1 ≤  i < j < k ≤  12. It can be shown that the maximum area of  for some valid (i, j, k) is achieved when i = 1, j = 8, and k = 12. Note that ss8 s12 is the string "acc" which is a moo according to the definitions above.  will have legs of lengths  and  so two times the area of it will be . 

For the third query, () must satisfy . It can be shown that the maximum area of  for some valid () is achieved when , , and .

For the fourth query, there exists no () satisfying  in which  is a moo so the output to that query is .

来源/分类