QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#10271. 字典序抽奖

الإحصائيات

给定一个长度为 $N$ 的字符串 $S$,其中包含小写英文字母。 对于 $1 \le L < R \le N$,过程 $\text{raffle}(L, R)$ 定义如下:

  • 步骤 1:令 $X$ 为子串 $S_L S_{L+1} \dots S_{R-1}$,$Y$ 为子串 $S_{L+1} S_{L+2} \dots S_R$。
  • 步骤 2:如果 $Y$ 在字典序上小于 $X$,则将 $L$ 加 1。否则,将 $R$ 减 1。
  • 步骤 3:如果 $L = R$,终止过程并返回 $L$ 作为过程的结果;否则,返回步骤 1。

给定 $Q$ 次询问,每次询问包含两个整数 $L$ 和 $R$ ($1 \le L < R \le N$)。对于每次询问,求出上述过程 $\text{raffle}(L, R)$ 最终的 $L$ 值。

输入格式

  • 第一行包含一个整数 $T$,表示测试用例的数量。
  • 每个测试用例包含多行输入:
    • 第一行包含 $N$ 和 $Q$,分别表示字符串的长度和询问次数。
    • 第二行包含字符串 $S$,长度为 $N$。
    • 接下来的 $Q$ 行,每行包含两个整数 $L$ 和 $R$,表示一次询问。

输出格式

对于每个测试用例的每次询问,输出一行,表示过程 $\text{raffle}(L, R)$ 结束后的最终 $L$ 值。

数据范围

  • $1 \le T \le 10^4$
  • $2 \le N \le 2 \cdot 10^5$
  • $1 \le Q \le 2 \cdot 10^5$
  • $|S| = N$
  • $S$ 仅包含小写英文字母
  • $1 \le L < R \le N$
  • 所有测试用例中 $N$ 的总和与 $Q$ 的总和均不超过 $2 \cdot 10^5$

样例

样例输入 1

4
6 3
kanpur
1 6
4 6
5 6
10 2
adccbabbab
1 10
2 9
5 3
accaa
1 5
2 4
3 5
5 1
jddda
3 4

样例输出 1

2
4
6
1
6
1
4
4
3

说明

测试用例 1:以下是第一个询问的解释:

  • 询问 1:
    • 初始时,$L = 1, R = 6$;$X = \text{kanpu}, Y = \text{anpur}$。由于 $Y$ 在字典序上更小,我们将 $L$ 增加到 2。由于 $L \neq R$,过程未终止。
    • 现在,$L = 2, R = 6$;$X = \text{anpu}, Y = \text{npur}$。由于 $Y$ 在字典序上不小于 $X$,我们将 $R$ 减小到 5。由于 $L \neq R$,过程未终止。
    • 现在,$L = 2, R = 5$;$X = \text{anp}, Y = \text{npu}$。由于 $Y$ 在字典序上不小于 $X$,我们将 $R$ 减小到 4。由于 $L \neq R$,过程未终止。
    • 现在,$L = 2, R = 4$;$X = \text{an}, Y = \text{np}$。由于 $Y$ 在字典序上不小于 $X$,我们将 $R$ 减小到 3。由于 $L \neq R$,过程未终止。
    • 现在,$L = 2, R = 3$;$X = \text{a}, Y = \text{n}$。由于 $Y$ 在字典序上不小于 $X$,我们将 $R$ 减小到 2。由于 $L = R$,过程终止。

因此,最终值为 $L = R = 2$。故答案为 2。

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.