PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000151. Duff đang tức giận (Duff is Mad)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Duff đang tức giận (Duff is Mad)

Duff is Mad

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Duff has nn friends. Friend ii has a nonempty lowercase-English name sis_i. Names need not be unique.

For each of qq queries (l,r,k)(l,r,k), compute the sum of occurrences of the names sl,…,srs_l,\ldots,s_r as contiguous substrings of the text sks_k:

∑i=lroccur⁡(si,sk).\sum_{i=l}^{r}\operatorname{occur}(s_i,s_k).

Here occur⁡(u,v)\operatorname{occur}(u,v) is the number of starting positions at which uu exactly matches a contiguous substring of vv. Overlapping occurrences count separately; equal names at different indices also contribute separately.

All queries are independent; strings do not change.

Input

  • First line: integers n,qn,q.
  • Next nn lines: the nonempty strings sis_i, consisting of lowercase letters a–z.
  • Next qq lines: l,r,kl,r,k, specifying the inclusive range of name indices and the target text index.

Output

Print qq lines. Line tt gives the sum of the occurrence counts for query tt.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤301\le q\le 30; 1≤∑i=1n∣si∣≤1501\le\sum_{i=1}^{n}|s_i|\le 150; all sis_i nonempty and lowercase a–z; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000; 1≤∑i=1n∣si∣≤30001\le\sum_{i=1}^{n}|s_i|\le 3000; all sis_i nonempty and lowercase a–z; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤∑i=1n∣si∣≤1000001\le\sum_{i=1}^{n}|s_i|\le 100000; all sis_i nonempty and lowercase a–z; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.

Examples

Example 1

Input

5 5
a
ab
abab
ababab
b
1 5 4
3 5 4
1 5 2
1 5 3
1 4 1

Output

12
6
3
7
1

Explanation

The text ababab contains a three times, ab three times, abab twice, ababab once and b three times: total 1212. Restricting to indices 3..53..5 yields 2+1+3=62+1+3=6. The text ab contains a, ab, b once each: 33. The text abab yields counts 2,2,1,0,22,2,1,0,2, total 77. The last query asks for names indexed 1..41..4 in text a, giving 11.