311162: CF1943B. Non-Palindromic Substring

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Non-Palindromic Substringtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

A string $t$ is said to be $k$-good if there exists at least one substring$^\dagger$ of length $k$ which is not a palindrome$^\ddagger$. Let $f(t)$ denote the sum of all values of $k$ such that the string $t$ is $k$-good.

You are given a string $s$ of length $n$. You will have to answer $q$ of the following queries:

  • Given $l$ and $r$ ($l < r$), find the value of $f(s_ls_{l + 1}\ldots s_r)$.

$^\dagger$ A substring of a string $z$ is a contiguous segment of characters from $z$. For example, "$\mathtt{defor}$", "$\mathtt{code}$" and "$\mathtt{o}$" are all substrings of "$\mathtt{codeforces}$" while "$\mathtt{codes}$" and "$\mathtt{aaa}$" are not.

$^\ddagger$ A palindrome is a string that reads the same backwards as forwards. For example, the strings "$\texttt{z}$", "$\texttt{aa}$" and "$\texttt{tacocat}$" are palindromes while "$\texttt{codeforces}$" and "$\texttt{ab}$" are not.

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5$), the size of the string and the number of queries respectively.

The second line of each test case contains the string $s$. It is guaranteed the string $s$ only contains lowercase English characters.

The next $q$ lines each contain two integers, $l$ and $r$ ($1 \le l < r \le n$).

It is guaranteed the sum of $n$ and the sum of $q$ both do not exceed $2 \cdot 10^5$.

Output

For each query, output $f(s_ls_{l + 1}\ldots s_r)$.

ExampleInput
5
4 4
aaab
1 4
1 3
3 4
2 4
3 2
abc
1 3
1 2
5 4
pqpcc
1 5
4 5
1 3
2 4
2 1
aa
1 2
12 1
steponnopets
1 12
Output
9
0
2
5
5
2
14
0
2
5
0
65
Note

In the first query of the first test case, the string is $\mathtt{aaab}$. $\mathtt{aaab}$, $\mathtt{aab}$ and $\mathtt{ab}$ are all substrings that are not palindromes, and they have lengths $4$, $3$ and $2$ respectively. Thus, the string is $2$-good, $3$-good and $4$-good. Hence, $f(\mathtt{aaab}) = 2 + 3 + 4 = 9$.

In the second query of the first test case, the string is $\mathtt{aaa}$. There are no non-palindromic substrings. Hence, $f(\mathtt{aaa}) = 0$.

In the first query of the second test case, the string is $\mathtt{abc}$. $\mathtt{ab}$, $\mathtt{bc}$ and $\mathtt{abc}$ are all substrings that are not palindromes, and they have lengths $2$, $2$ and $3$ respectively. Thus, the string is $2$-good and $3$-good. Hence, $f(\mathtt{abc}) = 2 + 3 = 5$. Note that even though there are $2$ non-palindromic substrings of length $2$, we count it only once.

Output

题目大意:
一个字符串t如果存在至少一个长度为k的非回文子串,则称该字符串为k-好的。定义f(t)为字符串t的所有k-好的k值的和。

给定一个长度为n的字符串s,你需要回答q个关于以下查询的问题:
- 给定l和r(l
输入数据格式:
每个测试用例包含多个测试案例。第一行包含一个整数t(1≤t≤2⋅10^4)——测试案例的数量。接下来是测试案例的描述。
每个测试案例的第一行包含两个整数n和q(2≤n≤2⋅10^5,1≤q≤2⋅10^5),分别是字符串的大小和查询的数量。
每个测试案例的第二行包含字符串s。保证字符串s只包含小写英文字符。
接下来的q行,每行包含两个整数l和r(1≤l 保证n的总和和q的总和都不超过2⋅10^5。

输出数据格式:
对于每个查询,输出f(s_ls_{l + 1}…s_r)的值。

请注意,在计算f(t)时,即使存在多个相同长度的非回文子串,也只计算一次该长度的值。题目大意: 一个字符串t如果存在至少一个长度为k的非回文子串,则称该字符串为k-好的。定义f(t)为字符串t的所有k-好的k值的和。 给定一个长度为n的字符串s,你需要回答q个关于以下查询的问题: - 给定l和r(l

加入题单

上一题 下一题 算法标签: