#P12024. [USACO25OPEN] It's Mooin' Time III B

[USACO25OPEN] It's Mooin' Time III B

Description

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 NN (3N1053 \leq N \leq 10^5) containing lowercase alphabetic characters s1s2sNs_1s_2 \ldots s_N. Elsie considers a string tt containing three characters a moo if t2=t3t_2 = t_3 and t2t1t_2 \neq t_1.

A triplet (i,j,k)(i, j, k) is valid if i<j<ki < j < k and string sisjsks_i s_j s_k forms a moo. For the triplet, FJ performs the following to calculate its value:

  • FJ bends string ss 90-degrees at index jj
  • The value of the triplet is twice the area of Δijk\Delta ijk.

In other words, the value of the triplet is (ji)(kj)(j-i)(k-j).

Bessie asks you QQ (1Q31041 \leq Q \leq 3 \cdot 10^4) queries. In each query, she gives you two integers ll and rr (1lrN1 \leq l \leq r \leq N, rl+13r-l+1 \ge 3) and ask you for the maximum value among valid triplets (i,j,k)(i, j, k) such that lil \leq i and krk \leq r. If no valid triplet exists, output 1-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

The first line contains two integers NN and QQ.

The following line contains s1s2,sNs_1 s_2, \ldots s_N.

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

Output Format

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

Hint

For the first query, (i,j,ki,j,k) must satisfy 1i<j<k121 \le i < j < k \le 12. It can be shown that the maximum area of Δijk\Delta ijk for some valid (i,j,ki,j,k) is achieved when i=1i=1, j=8j=8, and k=12k=12. Note that s1s8s12s_1 s_8 s_{12} is the string "acc" which is a moo according to the definitions above. Δijk\Delta ijk will have legs of lengths 77 and 44 so two times the area of it will be 2828.

For the third query, (i,j,ki,j,k) must satisfy 4i<j<k84 \le i < j < k \le 8. It can be shown that the maximum area of Δijk\Delta ijk for some valid (i,j,ki,j,k) is achieved when i=4i=4, j=5j=5, and k=6k=6.

For the fourth query, there exists no (i,j,ki,j,k) satisfying 2i<j<k52 \le i < j < k \le 5 in which sisjsks_i s_j s_k is a moo so the output to that query is 1-1.

SCORING:

  • Inputs 2-3: N,Q50N,Q\le 50
  • Inputs 4-6: Q=1Q=1 and the singular query satisfies l=1l=1 and r=Nr=N
  • Inputs 7-11: No additional constraints