QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 512 MB مجموع النقاط: 100 الصعوبة: [عرض] قابلة للهجوم ✓

#6759. String

الإحصائيات

Young Y is a university student who has recently been researching problems related to strings. Young Y has learned the following definitions regarding strings: Given a string $s[1 : n]$ of length $n$, we define its substring $s[l : r]$ ($1 \le l \le r \le n$) as the new string obtained by selecting $s[l], s[l + 1], \dots, s[r]$ and concatenating them in order. Given a string $s[1 : n]$ of length $n$, we define its reversed result $R(s)$ as the string obtained by concatenating $s[n], s[n - 1], \dots, s[1]$ in order, which is the string reversed. * Given two strings $a[1 : n]$ and $b[1 : n]$ both of length $n$, we define $a$ to be lexicographically smaller than $b$ if and only if there exists $1 \le i \le n$ such that for all $1 \le j < i$, $a[j] = b[j]$, and $a[i] < b[i]$.

After learning the above definitions, Young Y thought of the following problem: Given a string $s[1 : n]$ of length $n$. There are $q$ queries, each providing two parameters $i$ and $r$. You need to find how many $l$ satisfy the following conditions: $1 \le l \le r$. $s[i : i + l - 1]$ is lexicographically smaller than $R(s[i + l : i + 2l - 1])$.

Young Y would like your help to solve this problem.

Input

Read the data from the file string.in. There are multiple test cases. The first line of the input contains two integers $c$ and $t$, representing the test case ID and the number of test cases, respectively. $c = 0$ indicates that this test case is a sample. Then, each test case is provided sequentially. For each test case: The first line contains two positive integers $n$ and $q$, representing the string length and the number of queries. The second line contains a string $s$ of length $n$ consisting only of lowercase letters. The next $q$ lines each contain two positive integers $i$ and $r$, representing a query. It is guaranteed that $i + 2r - 1 \le n$.

Output

Output to the file string.out. For each query in each test case, output one integer per line, representing the number of $l$ that satisfy the conditions.

Examples

Input 1

0 2
9 3
abacababa
1 4
2 4
3 3
9 3
abaabaaba
1 4
2 4
3 3

Output 1

4
0
3
2
0
2

Note

For the first query of the first test case: When $l = 1$, $s[i : i + l - 1] = \text{a}$, $R(s[i + l : i + l + l - 1]) = \text{b}$. When $l = 2$, $s[i : i + l - 1] = \text{ab}$, $R(s[i + l : i + l + l - 1]) = \text{ca}$. When $l = 3$, $s[i : i + l - 1] = \text{aba}$, $R(s[i + l : i + l + l - 1]) = \text{bac}$. When $l = 4$, $s[i : i + l - 1] = \text{abac}$, $R(s[i + l : i + l + l - 1]) = \text{baba}$. In these four cases, $s[i : i + l - 1]$ is lexicographically smaller than $R(s[i + l : i + 2l - 1])$, so the answer is 4.

Examples 2

See string/string2.in and string/string2.ans in the contestant directory. The data range for this sample satisfies test case 5.

Examples 3

See string/string3.in and string/string3.ans in the contestant directory.

Examples 4

See string/string4.in and string/string4.ans in the contestant directory. The data range for this sample satisfies test cases 24 ~ 25.

Constraints

For all test cases, it is guaranteed that $1 \le t \le 5$, $1 \le n \le 10^5$, $1 \le q \le 10^5$, $1 \le i + 2r - 1 \le n$, and the string $s$ contains only lowercase letters.

Test Case ID $n$ $q$ Special Property
1 $\le 5$ $\le 5$ A
2 $\le 10$ $\le 10$
3 $\le 20$ $\le 20$ A
4 $\le 50$ $\le 50$
5 $\le 10^2$ $\le 10^2$
6 $\le 10^3$ $\le 10^3$
7 $\le 2,000$ $\le 2,000$ None
8 $\le 3,000$ $\le 3,000$
9 $\le 4,000$ $\le 4,000$
10 $\le 23,333$ $\le 23,333$ A
11 $\le 5 \times 10^4$ $\le 5 \times 10^4$
12 $\le 75,000$ $\le 75,000$ A
13 $\le 9 \times 10^4$ $\le 9 \times 10^4$
14 $\le 10^5$ $\le 10^5$
15 $\le 23,333$ $\le 23,333$ B
16 $\le 75,000$ $\le 75,000$
17 $\le 9 \times 10^4$ $\le 9 \times 10^4$
18 $\le 10^5$ $\le 10^5$
19 $\le 23,333$ $\le 23,333$ None
20 $\le 5 \times 10^4$ $\le 5 \times 10^4$
21 $\le 75,000$ $\le 75,000$
22 $\le 9 \times 10^4$ $\le 9 \times 10^4$
23 $\le 95,000$ $\le 95,000$
24 $\le 10^5$ $\le 10^5$
25
  • Special Property A: The string is guaranteed to contain only characters 'a' and 'b', and each character is chosen independently and with equal probability from 'a' and 'b'.
  • Special Property B: The string is guaranteed to have no two adjacent characters that are the same.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.