CF1073G.Yet Another LCP Problem

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Let LCP(s,t)\text{LCP}(s, t) be the length of the longest common prefix of strings ss and tt. Also let s[x…y]s[x \dots y] be the substring of ss from index xx to index yy (inclusive). For example, if $s = $ "abcde", then s[1…3]=s[1 \dots 3] = "abc", s[2…5]=s[2 \dots 5] = "bcde".

You are given a string ss of length nn and qq queries. Each query is a pair of integer sets a1,a2,…,aka_1, a_2, \dots, a_k and b1,b2,…,blb_1, b_2, \dots, b_l. Calculate ∑i=1i=k∑j=1j=lLCP(s[ai…n],s[bj…n])\sum\limits_{i = 1}^{i = k} \sum\limits_{j = 1}^{j = l}{\text{LCP}(s[a_i \dots n], s[b_j \dots n])} for each query.

设 LCP(s,t)\text{LCP}(s, t) 表示字符串 ss 与 tt 的最长公共前缀的长度。同时,令 s[x…y]s[x \dots y] 表示字符串 ss 中从下标 xx 到下标 yy(含端点)的子串。例如,若 $s = $ "abcde",则 s[1…3]=s[1 \dots 3] = "abc",s[2…5]=s[2 \dots 5] = "bcde"。

给定一个长度为 nn 的字符串 ss 以及 qq 个查询。每个查询由两个整数集合组成:a1,a2,…,aka_1, a_2, \dots, a_k 和 b1,b2,…,blb_1, b_2, \dots, b_l。对每个查询,计算 ∑i=1i=k∑j=1j=lLCP(s[ai…n],s[bj…n])\sum\limits_{i = 1}^{i = k} \sum\limits_{j = 1}^{j = l}{\text{LCP}(s[a_i \dots n], s[b_j \dots n])}。

输入格式

The first line contains two integers nn and qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5) — the length of string ss and the number of queries, respectively.

The second line contains a string ss consisting of lowercase Latin letters (∣s∣=n|s| = n).

Next 3q3q lines contains descriptions of queries — three lines per query. The first line of each query contains two integers kik_i and lil_i (1≤ki,li≤n1 \le k_i, l_i \le n) — sizes of sets aa and bb respectively.

The second line of each query contains kik_i integers a1,a2,…akia_1, a_2, \dots a_{k_i} (1≤a1<a2<⋯<aki≤n1 \le a_1 \lt a_2 \lt \dots \lt a_{k_i} \le n) — set aa.

The third line of each query contains lil_i integers b1,b2,…blib_1, b_2, \dots b_{l_i} (1≤b1<b2<⋯<bli≤n1 \le b_1 \lt b_2 \lt \dots \lt b_{l_i} \le n) — set bb.

It is guaranteed that ∑i=1i=qki≤2⋅105\sum\limits_{i = 1}^{i = q}{k_i} \le 2 \cdot 10^5 and ∑i=1i=qli≤2⋅105\sum\limits_{i = 1}^{i = q}{l_i} \le 2 \cdot 10^5.

第一行包含两个整数 nn 和 qq(1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5)—— 分别为字符串 ss 的长度和查询次数。

第二行包含一个由小写拉丁字母组成的字符串 ss(∣s∣=n|s| = n)。

接下来的 3q3q 行描述了所有查询,每个查询占三行。每个查询的第一行包含两个整数 kik_i 和 lil_i(1≤ki,li≤n1 \le k_i, l_i \le n)—— 分别为集合 aa 和 bb 的大小。

每个查询的第二行包含 kik_i 个整数 a1,a2,…,akia_1, a_2, \dots, a_{k_i}(1≤a1<a2<⋯<aki≤n1 \le a_1 \lt a_2 \lt \dots \lt a_{k_i} \le n)—— 集合 aa。

每个查询的第三行包含 lil_i 个整数 b1,b2,…,blib_1, b_2, \dots, b_{l_i}(1≤b1<b2<⋯<bli≤n1 \le b_1 \lt b_2 \lt \dots \lt b_{l_i} \le n)—— 集合 bb。

保证 ∑i=1i=qki≤2⋅105\sum\limits_{i = 1}^{i = q}{k_i} \le 2 \cdot 10^5 且 ∑i=1i=qli≤2⋅105\sum\limits_{i = 1}^{i = q}{l_i} \le 2 \cdot 10^5。

输出格式

Print qq integers — answers for the queries in the same order queries are given in the input.

输出 qq 个整数——按输入中查询出现的相同顺序,依次输出各查询的答案。

输入输出样例

  • 输入#1

    7 4
    abacaba
    2 2
    1 2
    1 2
    3 1
    1 2 3
    7
    1 7
    1
    1 2 3 4 5 6 7
    2 2
    1 5
    1 5

    输出#1

    13
    2
    12
    16

说明/提示

Description of queries:

  1. In the first query s[1…7]=abacabas[1 \dots 7] = \text{abacaba} and s[2…7]=bacabas[2 \dots 7] = \text{bacaba} are considered. The answer for the query is LCP(abacaba,abacaba)+LCP(abacaba,bacaba)+LCP(bacaba,abacaba)+LCP(bacaba,bacaba)=7+0+0+6=13\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{bacaba}) + \text{LCP}(\text{bacaba}, \text{abacaba}) + \text{LCP}(\text{bacaba}, \text{bacaba}) = 7 + 0 + 0 + 6 = 13.
  2. In the second query s[1…7]=abacabas[1 \dots 7] = \text{abacaba}, s[2…7]=bacabas[2 \dots 7] = \text{bacaba}, s[3…7]=acabas[3 \dots 7] = \text{acaba} and s[7…7]=as[7 \dots 7] = \text{a} are considered. The answer for the query is LCP(abacaba,a)+LCP(bacaba,a)+LCP(acaba,a)=1+0+1=2\text{LCP}(\text{abacaba}, \text{a}) + \text{LCP}(\text{bacaba}, \text{a}) + \text{LCP}(\text{acaba}, \text{a}) = 1 + 0 + 1 = 2.
  3. In the third query s[1…7]=abacabas[1 \dots 7] = \text{abacaba} are compared with all suffixes. The answer is the sum of non-zero values: LCP(abacaba,abacaba)+LCP(abacaba,acaba)+LCP(abacaba,aba)+LCP(abacaba,a)=7+1+3+1=12\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{acaba}) + \text{LCP}(\text{abacaba}, \text{aba}) + \text{LCP}(\text{abacaba}, \text{a}) = 7 + 1 + 3 + 1 = 12.
  4. In the fourth query s[1…7]=abacabas[1 \dots 7] = \text{abacaba} and s[5…7]=abas[5 \dots 7] = \text{aba} are considered. The answer for the query is LCP(abacaba,abacaba)+LCP(abacaba,aba)+LCP(aba,abacaba)+LCP(aba,aba)=7+3+3+3=16\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{aba}) + \text{LCP}(\text{aba}, \text{abacaba}) + \text{LCP}(\text{aba}, \text{aba}) = 7 + 3 + 3 + 3 = 16.

查询描述:

  1. 在第一个查询中,考虑子串 s[1…7]=abacabas[1 \dots 7] = \text{abacaba} 和 s[2…7]=bacabas[2 \dots 7] = \text{bacaba}。该查询的答案为 LCP(abacaba,abacaba)+LCP(abacaba,bacaba)+LCP(bacaba,abacaba)+LCP(bacaba,bacaba)=7+0+0+6=13\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{bacaba}) + \text{LCP}(\text{bacaba}, \text{abacaba}) + \text{LCP}(\text{bacaba}, \text{bacaba}) = 7 + 0 + 0 + 6 = 13。
  2. 在第二个查询中,考虑子串 s[1…7]=abacabas[1 \dots 7] = \text{abacaba}、s[2…7]=bacabas[2 \dots 7] = \text{bacaba}、s[3…7]=acabas[3 \dots 7] = \text{acaba} 和 s[7…7]=as[7 \dots 7] = \text{a}。该查询的答案为 LCP(abacaba,a)+LCP(bacaba,a)+LCP(acaba,a)=1+0+1=2\text{LCP}(\text{abacaba}, \text{a}) + \text{LCP}(\text{bacaba}, \text{a}) + \text{LCP}(\text{acaba}, \text{a}) = 1 + 0 + 1 = 2。
  3. 在第三个查询中,将 s[1…7]=abacabas[1 \dots 7] = \text{abacaba} 与所有后缀进行比较。答案为所有非零值之和:LCP(abacaba,abacaba)+LCP(abacaba,acaba)+LCP(abacaba,aba)+LCP(abacaba,a)=7+1+3+1=12\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{acaba}) + \text{LCP}(\text{abacaba}, \text{aba}) + \text{LCP}(\text{abacaba}, \text{a}) = 7 + 1 + 3 + 1 = 12。
  4. 在第四个查询中,考虑子串 s[1…7]=abacabas[1 \dots 7] = \text{abacaba} 和 s[5…7]=abas[5 \dots 7] = \text{aba}。该查询的答案为 LCP(abacaba,abacaba)+LCP(abacaba,aba)+LCP(aba,abacaba)+LCP(aba,aba)=7+3+3+3=16\text{LCP}(\text{abacaba}, \text{abacaba}) + \text{LCP}(\text{abacaba}, \text{aba}) + \text{LCP}(\text{aba}, \text{abacaba}) + \text{LCP}(\text{aba}, \text{aba}) = 7 + 3 + 3 + 3 = 16。

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

首页