Count the palindromic substrings fully contained in each query interval of a lowercase string.
Hard8String matchingSegment treeSortingMathNo attempts yetTime limit2sMemory limit64 MBThe organizers wanted to hold a practice contest but had nothing prepared, so they took the two words that came to mind, palindrome and data structure, and built a problem out of them.
Seunghyun made a string S that uses only lowercase letters. For integers i and j with 1≤i≤j≤∣S∣, let S[i..j] be the substring formed by joining the i-th character of S, the (i+1)-th character, and so on up to the j-th character.
Each query is a pair of integers a and b. For that query, count the palindromes that lie inside S[a..b]. In other words, count the pairs (x,y) with a≤x≤y≤b such that S[x..y] is a palindrome. Two occurrences of the same substring at different starting positions count separately.
The first line contains the string S that Seunghyun made. S uses only the lowercase letters a through z, and its length satisfies 1≤∣S∣≤100000.
The second line contains the number of queries Q (1≤Q≤300000). Each of the next Q lines contains two integers ai and bi (1≤ai≤bi≤∣S∣).
For each query, print the number of palindromes inside S[ai..bi] on its own line, in the order the queries are given.
A string T=t1t2t3⋯tn is a palindrome when reading it from the front gives the same string as reading it from the back, that is, when t1t2t3⋯tn−1tn=tntn−1⋯t3t2t1 holds. A string of length 1 is always a palindrome.
∣S∣ is the length of the string S.