#x1081. Yet Another LCP Problem
Yet Another LCP Problem
Yet Another LCP Problem
题面翻译
记表示i这个后缀和j这个后缀的最长公共前缀长度
给定一个字符串,每次询问的时候给出两个正整数集合A和B,求
的值
题目描述
Let be the length of the longest common prefix of strings and . Also let be the substring of from index to index (inclusive). For example, if "abcde", then "abc", "bcde".
You are given a string of length and queries. Each query is a pair of integer sets and . Calculate $ \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.
输入格式
The first line contains two integers and ( ) — the length of string and the number of queries, respectively.
The second line contains a string consisting of lowercase Latin letters ( ).
Next lines contains descriptions of queries — three lines per query. The first line of each query contains two integers and ( ) — sizes of sets and respectively.
The second line of each query contains integers ( ) — set .
The third line of each query contains integers ( ) — set .
It is guaranteed that $ \sum\limits_{i = 1}^{i = q}{k_i} \le 2 \cdot 10^5 $ and $ \sum\limits_{i = 1}^{i = q}{l_i} \le 2 \cdot 10^5 $ .
输出格式
Print integers — answers for the queries in the same order queries are given in the input.
样例 #1
样例输入 #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:
- In the first query and are considered. The answer for the query is $ \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 $ .
- In the second query , , and are considered. The answer for the query is $ \text{LCP}(\text{abacaba}, \text{a}) + \text{LCP}(\text{bacaba}, \text{a}) + \text{LCP}(\text{acaba}, \text{a}) = 1 + 0 + 1 = 2 $ .
- In the third query are compared with all suffixes. The answer is the sum of non-zero values: $ \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 $ .
- In the fourth query and are considered. The answer for the query is $ \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 $ .