CF2209E.A Trivial String Problem

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • There exist kk strings p1,p2,…,pkp_1,p_2,\ldots,p_k, which are prefixes of tt, such that t=p1+p2+…+pkt=p_1+p_2+\ldots+p_k. Here ++ denotes the string concatenation.

You are given a string ss of length nn, consisting of only lowercase English letters. Let s[x..y]s[x..y] represent the substring∗^{\text{∗}} of ss from the xx-th to the yy-th character (inclusive). You need to answer qq queries. The ii-th query provides two integers lil_i and rir_i, and you need to find

\\sum\_{j=l\_i}^{r\_i} f(s\[l\_i..j\]).

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

定义 f(t)f(t) 为字符串 tt 可被划分成的最多部分数,使得每一部分均为 tt 的非空前缀。换言之,f(t)f(t) 是满足以下条件的最大正整数 kk:

  • 存在 kk 个字符串 p1,p2,…,pkp_1,p_2,\ldots,p_k,它们均为 tt 的前缀,且满足 t=p1+p2+…+pkt=p_1+p_2+\ldots+p_k。其中 ++ 表示字符串连接运算。

给定一个长度为 nn 的字符串 ss,其仅由小写英文字母组成。记 s[x..y]s[x..y] 表示 ss 中从第 xx 个字符到第 yy 个字符(含端点)的子串∗^{\text{∗}}。你需要回答 qq 个查询。第 ii 个查询给出两个整数 lil_i 和 rir_i,你需要计算

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

∗^{\text{∗}} 若字符串 aa 可通过从字符串 bb 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 aa 是 bb 的子串。

输入格式

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

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

The second line contains the string ss of length nn, consisting of only lowercase English letters.

The ii-th of the next qq lines contains two integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n), representing the ii-th query.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1031 \le t \le 10^3)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n≤1061 \le n \le 10^6,1≤q≤1001 \le q \le 100)。

第二行包含一个长度为 nn 的字符串 ss,仅由小写英文字母组成。

接下来的 qq 行中,第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n),表示第 ii 个查询。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

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

对于每个查询,输出一个整数,表示该表达式的值。

输入输出样例

  • 输入#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

    输出#1

    1
    15
    6
    6
    3
    14
    4
    7
    12
    9
    5
    13
    14
    7

说明/提示

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

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

In the third test case, f(a)+f(ab)+f(abc)+f(abcd)+f(abcde)+f(abcdef)=1+1+1+1+1+1=6f(\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(c)+f(cd)+f(cde)=1+1+1=3f(\texttt{c})+f(\texttt{cd})+f(\texttt{cde})=1+1+1=3.

在第一个测试用例中,f(a)=1f(\texttt{a})=1。

在第二个测试用例中,f(a)+f(aa)+f(aaa)+f(aaaa)+f(aaaaa)=1+2+3+4+5=15f(\texttt{a})+f(\texttt{aa})+f(\texttt{aaa})+f(\texttt{aaaa})+f(\texttt{aaaaa})=1+2+3+4+5=15,且 f(a)+f(aa)+f(aaa)=1+2+3=6f(\texttt{a})+f(\texttt{aa})+f(\texttt{aaa})=1+2+3=6。

在第三个测试用例中,f(a)+f(ab)+f(abc)+f(abcd)+f(abcde)+f(abcdef)=1+1+1+1+1+1=6f(\texttt{a})+f(\texttt{ab})+f(\texttt{abc})+f(\texttt{abcd})+f(\texttt{abcde})+f(\texttt{abcdef})=1+1+1+1+1+1=6,且 f(c)+f(cd)+f(cde)=1+1+1=3f(\texttt{c})+f(\texttt{cd})+f(\texttt{cde})=1+1+1=3。

输入解题思路,AI测评打分。不知道怎么写?

首页