It's Mooin' Time III

시간 제한2초메모리 제한2048 MB

요약
각 질의 구간 [l, r]에서 s_j=s_k이고 s_j != s_i인 i<j<k에 대해 (j-i)(k-j)의 최댓값을 구하고, 없으면 -1을 출력한다.
난이도

어려움10점 중 9점

유형
분할 정복, 세그먼트 트리, 누적 합
정답자
아직 제출이 없습니다

문제

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 (3≤N≤1053 \leq N \leq 10^5) containing lowercase alphabetic characters s_1s_2…s_Ns\_1s\_2 \ldots s\_N. Elsie considers a string tt containing three characters a moo if t_2=t_3t\_2 = t\_3 and t_2≠t_1t\_2 \neq t\_1.

A triplet (i,j,k)(i, j, k) is valid if i<j<ki < j < k and string s_is_js_ks\_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 (j−i)(k−j)(j-i)(k-j).

Bessie asks you QQ (1≤Q≤3⋅1041 \leq Q \leq 3 \cdot 10^4) queries. In each query, she gives you two integers ll and rr (1≤l≤r≤N1 \leq l \leq r \leq N, r−l+1≥3r-l+1 \ge 3) and ask you for the maximum value among valid triplets (i,j,k)(i, j, k) such that l≤il \leq i and k≤rk \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++).

입력

The first line contains two integers NN and QQ.

The following line contains s_1s_2,…s_Ns\_1 s\_2, \ldots s\_N.

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

출력

Output the answer for each query on a new line.

예제1

  1. 예제 1

    입력
    12 5
    abcabbacabac
    1 12
    2 7
    4 8
    2 5
    3 10
    
    예상 출력
    28
    6
    1
    -1
    12