Codeforces Round 1087 (Div. 2)
E
A Trivial String Problem
Codeforces
2200
原题
← DF →
翻译中…

Define $f(t)$ as the maximum number of parts $t$ can be split into such that each part is a non-empty prefix of $t$. In other words, $f(t)$ is the maximum positive integer $k$ that satisfies the following condition:

翻译中…
  • There exist $k$ strings $p_1,p_2,\ldots,p_k$, which are prefixes of $t$, such that $t=p_1+p_2+\ldots+p_k$. Here $+$ denotes the string concatenation.
翻译中…

You are given a string $s$ of length $n$, consisting of only lowercase English letters. Let $s[x..y]$ represent the substring$^{\text{∗}}$ of $s$ from the $x$-th to the $y$-th character (inclusive). You need to answer $q$ queries. The $i$-th query provides two integers $l_i$ and $r_i$, and you need to find

翻译中…

$$ \sum_{j=l_i}^{r_i} f(s[l_i..j]). $$

翻译中…

$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 10^6$, $1 \le q \le 100$).

翻译中…

The second line contains the string $s$ of length $n$, consisting of only lowercase English letters.

翻译中…

The $i$-th of the next $q$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$), representing the $i$-th query.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each query, output an integer representing the value of the expression.

翻译中…

Note

翻译中…

In the first test case, $f(\texttt{a})=1$.

翻译中…

In the second test case, $f(\texttt{a})+f(\texttt{aa})+f(\texttt{aaa})+f(\texttt{aaaa})+f(\texttt{aaaaa})=1+2+3+4+5=15$ and $f(\texttt{a})+f(\texttt{aa})+f(\texttt{aaa})=1+2+3=6$.

翻译中…

In the third test case, $f(\texttt{a})+f(\texttt{ab})+f(\texttt{abc})+f(\texttt{abcd})+f(\texttt{abcde})+f(\texttt{abcdef})=1+1+1+1+1+1=6$ and $f(\texttt{c})+f(\texttt{cd})+f(\texttt{cde})=1+1+1=3$.

翻译中…

样例

Input #1
6
1 1
a
1 1
5 2
aaaaa
1 5
2 4
6 2
abcdef
1 6
3 5
6 3
abaaba
1 6
1 3
2 6
7 3
abcabca
1 7
2 7
4 7
8 3
aababaac
1 8
2 8
3 7
Output #1
1
15
6
6
3
14
4
7
12
9
5
13
14
7
InputCopy OutputCopy
题目信息
题号
2209E
时间限制
4 s
内存限制
1024 MB
标签
暴力
动态规划
哈希
后缀结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。