#U2425OB3. It's Mooin' Time III B
It's Mooin' Time III B
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 N (3≤N≤10⁵) containing lowercase alphabetic characters s₁s₂…sₙ. Elsie considers a string t containing three characters a moo if t₂=t₃ and t₂≠t₁.
A triplet (i,j,k) is valid if i<j<k and string sᵢsⱼsₖ forms a moo. For the triplet, FJ performs the following to calculate its value:
- FJ bends string s 90-degrees at index j
- The value of the triplet is twice the area of Δijk.
In other words, the value of the triplet is **(j−i)(k−j)**.
Bessie asks you Q (1≤Q≤3⋅10⁴) queries. In each query, she gives you two integers l and r (1≤l≤r≤N, r−l+1≥3) and ask you for the maximum value among valid triplets (i,j,k) such that l≤i and k≤r. If no valid triplet exists, output −1.
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++).
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains two integers N and Q. The following line contains s₁s₂…sₙ.
The following Q lines contain two integers l and r, denoting each query.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the answer for each query on a new line.
SAMPLE INPUT:
plaintext
12 5
abcabbacabac
1 12
2 7
4 8
2 5
3 10
SAMPLE OUTPUT:
plaintext
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 Δijk for some valid (i,j,k) is achieved when i=1, j=8, and k=12. Note that s₁s₈s₁₂ is the string "acc" which is a moo according to the definitions above. Δijk will have legs of lengths 7 and 4 so two times the area of it will be 28. For the third query, (i,j,k) must satisfy 4≤i<j<k≤8. It can be shown that the maximum area of Δijk for some valid (i,j,k) is achieved when i=4, j=5, and k=6.
For the fourth query, there exists no (i,j,k) satisfying 2≤i<j<k≤5 in which sᵢsⱼsₖ is a moo so the output to that query is −1.
SCORING:
- Inputs 2-3: N,Q≤50
- Inputs 4-6: Q=1 and the singular query satisfies l=1 and r=N
- Inputs 7-11: No additional constraints
Problem credits: Chongtian Ma