#P6292. 区间本质不同子串个数

区间本质不同子串个数

Problem Description

Given a string SS of length nn consisting only of lowercase letters, there are mm queries. Each query asks how many essentially different non-empty substrings are contained in the string formed by the LL-th to the RR-th characters of SS.

Two strings a,ba, b are defined to be the same if and only if a=b|a|=|b| and for all i[1,a]i\in[1,|a|], we have ai=bia_i=b_i.

Input Format

The first line contains a string SS of length nn.

The second line contains an integer mm, indicating the number of queries.

The next mm lines each contain two integers li,ril_i, r_i, describing the ii-th query.

Output Format

Output mm lines, each containing an integer, representing the answer to the ii-th query.

aababc
3
1 2
2 4
3 6
2
5
9

Hint

Explanation for Sample 1

  • For the first query, the string is aa\texttt{aa}, and it contains a\texttt{a} and aa\texttt{aa}, for a total of 22 essentially different substrings.
  • For the second query, the string is aba\texttt{aba}, and it contains $\texttt{a},\texttt{b},\texttt{ab},\texttt{ba},\texttt{aba}$, for a total of 55 essentially different substrings.
  • For the third query, the string is babc\texttt{babc}, and it contains a\texttt{a},b\texttt{b},c\texttt{c},ab\texttt{ab},ba\texttt{ba},bc\texttt{bc},bab\texttt{bab},abc\texttt{abc},babc\texttt{babc}, for a total of 99 essentially different substrings.

Constraints

  • For 20%20\% of the testdata, n3×103n\leq 3\times 10^3 and m3×103m\leq 3\times 10^3.
  • For 100%100\% of the testdata, 1n1051\leq n\leq 10^5, 1m2×1051\leq m\leq 2\times 10^5, 1lirin(i[1,m])1\leq l_i\leq r_i\leq n(i\in[1,m]).

Translated by ChatGPT 5