#P10176. 「OICon-02」Native Faith

    ID: 11421 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串后缀自动机 SAMO2优化分块AC 自动机根号分治

「OICon-02」Native Faith

Problem Description

In this problem, string indices start from 11.

Define the result of adding two strings as the new string formed by concatenating the two strings end to end.

Let $f(a,b,c)=\sum\limits_{i=1}^{|a|}\sum\limits_{j=i}^{|a|}\sum\limits_{k=1}^{|b|}\sum\limits_{l=k}^{|b|}[a_{i,i+1,\cdots,j}+b_{k,k+1,\cdots,l} = c]$ (a,b,ca,b,c are all strings).

That is, how many ways are there to choose a non-empty substring from each of aa and bb such that the sum (concatenation) of the two substrings equals cc.

Given nn strings s1,s2,s3,,sns_1,s_2,s_3,\cdots,s_n.

There are qq queries. Each query gives three positive integers l,r,kl,r,k. Compute $\sum\limits_{i=l}^r\sum\limits_{j=l}^rf(s_i,s_j,s_k)$.

Input Format

The first line contains two integers n,qn,q.

The next nn lines each contain a non-empty string consisting only of lowercase letters, representing s1,s2,,sns_1,s_2,\cdots,s_n.

The next qq lines each contain three positive integers l,r,kl,r,k, representing one query.

Output Format

For each query, output one integer per line representing the answer.

3 3
a
aa
aaa
1 2 3
2 3 3
1 3 3
6
30
36
10 10
aabb
aba
abbba
abaccaab
abbba
ababababab
aaaaa
bbbbbb
aaba
abbba
1 10 10
1 4 5
3 6 4
2 8 1
1 5 4
2 10 7
2 9 2
4 5 5
5 5 6
8 9 10
241
31
51
105
40
136
460
17
0
0
5 5
a
ba
aba
ababa
abab
1 3 3
1 2 3
2 3 3
4 4 5
3 4 4
12
2
9
11
28

Hint

Sample Explanation

For sample 11, some values of the function ff are given.

  • f(s1,s1,s3)=0f(s_1,s_1,s_3)=0, f(s1,s2,s3)=1f(s_1,s_2,s_3)=1, f(s1,s3,s3)=2f(s_1,s_3,s_3)=2, f(s2,s1,s3)=1f(s_2,s_1,s_3)=1, f(s2,s2,s3)=4f(s_2,s_2,s_3)=4, f(s2,s3,s3)=7f(s_2,s_3,s_3)=7, f(s3,s1,s3)=2f(s_3,s_1,s_3)=2, f(s3,s2,s3)=7f(s_3,s_2,s_3)=7, f(s3,s3,s3)=12f(s_3,s_3,s_3)=12.

Constraints

This problem uses bundled testdata.

Let m=sim=\sum|s_i|.

::cute-table | Subtask\text{Subtask} | Special Property | Score\text{Score} | | :-----------: | :-----------: | :-----------: | | 11 | n,m,q3×103n,m,q\le 3\times 10^3 | 1717 | | 22 | The kk in each query are all different | 2323 | | 33 | n,m,q3×104n,m,q\le 3\times 10^4 | 2727 | | 44 | The strings only contain the lowercase letter a\texttt{a} | 1919 | | 55 | None | 1414 |

For 100%100\% of the data: 1n,m,q1051\le n,m,q\le 10^5, 1lrn1\le l \le r\le n, 1kn1\le k\le n, and the strings contain only lowercase letters.

Translated by ChatGPT 5