Unique Substrings Query
Limits
1s, 128 MB
You will be given a string s of length n.
Consider substring s[i,n] (where i is the starting position and n is the ending position) of s as a string called Xi.
You need to perform m queries on that string. A query is defined as:
L R P Q
For each query, you need to calculate:
∑i=LRf(Xi,P,Q)
Where,
- f(Xi,P,Q) is the number of unique substrings of Xi of length between P and Q.
Input
The first line of the input is followed by two integers n (1≤n≤103) and m (1≤m≤106).
The second line contains the string s of only lowercase letters.
Each of the next m lines contains four integers L, R, P, Q (1≤L≤R≤n,1≤P≤Q≤n).
Output
For each query, output the value of ∑i=LRf(Xi,P,Q).
Sample
Input | Output |
---|
5 1
agree
1 5 1 1
| 11
|
All the suffixes of "agree" - X1="agree"
- X2="gree"
- X3="ree"
- X4="ee"
- X5="e"
Now we are calculating - f(X1,1,1)=4 ("a", "g", "r" and "e" are the unique substring of length 1)
- f(X2,1,1)=3 ("g", "r" and "e" are the unique substring of length 1)
- f(X3,1,1)=2 ("r" and "e" are the unique substring of length 1)
- f(X4,1,1)=1 ("e" is the unique substring of length 1)
- f(X5,1,1)=1 ("e" is the unique substring of length 1)
So, answer is 4 + 3 + 2 + 1 + 1 = 11. |