#P9482. [NOI2023] 字符串

[NOI2023] 字符串

Problem Description

Xiao Y is a college student who has recently been studying problems related to strings.

Xiao Y learned the following definitions about strings:

  • Given a string s[1:n]s[1: n] of length nn, we define its substring s[l:r]s[l: r] (1lrn1 \leq l \leq r \leq n) as the new string obtained by taking s[l],s[l+1],,s[r]s[l], s[l+1], \dots, s[r] and concatenating them in order.
  • Given a string s[1:n]s[1: n] of length nn, we define its reversed result R(s)R(s) as the string obtained by concatenating s[n],s[n1],,s[1]s[n], s[n-1], \dots, s[1] in order, i.e., reversing the string.
  • Given two strings a[1:n],b[1:n]a[1: n], b[1: n] of length nn, we say aa is lexicographically smaller than bb if and only if there exists 1in1 \leq i \leq n such that for all 1j<i1 \leq j < i, a[j]=b[j]a[j] = b[j], and a[i]<b[i]a[i] < b[i].

After understanding the definitions above, Xiao Y came up with the following problem:

Given a string s[1:n]s[1: n] of length nn. There are qq queries, and each query gives two parameters i,ri, r. You need to find how many ll satisfy the following conditions:

  • 1lr1 \leq l \leq r.
  • s[i:i+l1]s[i: i+l-1] is lexicographically smaller than R(s[i+l:i+2l1])R(s[i+l: i+2l-1]).

Xiao Y asks you for help to solve this problem.

Input Format

This problem contains multiple test cases.

The first line of input contains two integers c,tc, t, representing the test point ID and the number of test cases. c=0c=0 means this test point is the sample.

Then each test case is given as follows. For each test case:

The first line contains two positive integers n,qn, q, representing the string length and the number of queries.

The second line contains a string ss of length nn consisting only of lowercase letters.

The next qq lines each contain two positive integers i,ri, r, representing one query. It is guaranteed that i+2r1ni+2r-1 \leq n.

Output Format

For each query in each test case, output one line with an integer, representing the number of ll that satisfy the conditions.

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

4
0
3
2
0
2

见附件中的 string/string2.in。
见附件中的 string/string2.ans。
见附件中的 string/string3.in。
见附件中的 string/string3.ans。
见附件中的 string/string4.in。
见附件中的 string/string4.ans。

Hint

[Sample Explanation #1]

For the first query of the first test case:

  • When l=1l = 1, s[i:i+l1]=as[i: i + l - 1] = \texttt{a}, and R(s[i+l:i+2l1])=bR(s[i + l: i + 2l - 1]) = \texttt{b}.
  • When l=2l = 2, s[i:i+l1]=abs[i: i + l - 1] = \texttt{ab}, and R(s[i+l:i+2l1])=caR(s[i + l: i + 2l - 1]) = \texttt{ca}.
  • When l=3l = 3, s[i:i+l1]=abas[i: i + l - 1] = \texttt{aba}, and R(s[i+l:i+2l1])=bacR(s[i + l: i + 2l - 1]) = \texttt{bac}.
  • When l=4l = 4, s[i:i+l1]=abacs[i: i + l - 1] = \texttt{abac}, and R(s[i+l:i+2l1])=babaR(s[i + l: i + 2l - 1]) = \texttt{baba}.

In all four cases, s[i:i+l1]s[i: i + l - 1] is lexicographically smaller than R(s[i+l:i+2l1])R(s[i + l: i + 2l - 1]). Therefore, the answer is 44.

[Sample Explanation #2]

The Constraints of this sample satisfy test point 55.

[Sample Explanation #4]

The Constraints of this sample satisfy test points 242524 \sim 25.

[Constraints]

For all testdata, it is guaranteed that: 1t51 \le t \le 5, 1n1051 \le n \le 10 ^ 5, 1q1051 \le q \le 10 ^ 5, 1i+2r1n1 \le i + 2r - 1 \le n , and the string ss consists only of lowercase letters.

Test Point ID nn \le qq \le Special Property
11 55 A
22 1010
33 2020
44 5050
55 10210^2
66 10310^3 None
77 2,0002,000
88 3,0003,000
99 4,0004,000
1010 23,33323,333 A
1111 5×1045 \times 10 ^ 4
1212 75,00075,000
1313 9×1049 \times 10 ^ 4
1414 10510 ^ 5
1515 23,33323,333 B
1616 75,00075,000 $$75,000$
1717 9×1049 \times 10 ^ 4
1818 10510 ^ 5
1919 23,33323,333 None
2020 5×1045 \times 10 ^ 4
2121 75,00075,000
2222 9×1049 \times 10 ^ 4
2323 95,00095,000
242524 \sim 25 10510 ^ 5

Special property A: It is guaranteed that the string contains only characters a\texttt{a} and b\texttt{b}, and each character independently chooses between a\texttt{a} and b\texttt{b} with equal probability.

Special property B: It is guaranteed that adjacent characters in the string are all different.

On Luogu, in this problem, Subtask 1 contains hack data, and it is guaranteed that c=25c=25.

Translated by ChatGPT 5