#P6152. [集训队作业2018] 后缀树节点数

[集训队作业2018] 后缀树节点数

Description

Given a string PP of length nn, there are mm queries. Each query gives two parameters l,rl, r and asks for the number of nodes in the suffix tree built from the substring P[l,r]P[l,r].

If you do not know what a suffix tree is, you can also understand it as building a suffix automaton on the reversed string of [l,r][l,r].

Note that in this problem, the root node of the suffix tree is not included in the answer.

Input Format

In this problem, the string is represented by numbers. Different numbers represent different characters, and the same number represents the same character. The numbers are in the range [0,n][0,n].

The first line contains two positive integers nn and mm, representing the length of the string and the number of queries. The second line contains nn non-negative integers separated by spaces, representing the string. The next mm lines each contain two positive integers l,rl, r, representing a query: ask for the number of nodes in the suffix tree of the substring [l,r][l,r].

Output Format

Output mm lines. Each line contains one positive integer representing the answer.

7 2
2 1 3 1 3 1 4
1 7
4 5
10
2

Hint

For 30%30\% of the testdata: n,m3×103n,m\le 3\times 10^3.

For 100%100\% of the testdata: n105n\le 10^5, m3×105m\le 3\times 10^5, and each number in the string is n\le n

Translated by ChatGPT 5