#P9623. [ICPC2020 Nanjing R] Baby's First Suffix Array Problem

[ICPC2020 Nanjing R] Baby's First Suffix Array Problem

题目描述

A suffix array for string ss of length nn is a permutation sasa of integers from 11 to nn such that s[sa1..n],s[sa2..n],,s[san..n]s[sa_1.. n], s[sa_2..n], \dots, s[sa_n..n] is the list of non-empty suffixes of ss sorted in lexicographical order. The rank table for suffixex of ss is a permutation rankrank of integers from 11 to nn such that ranksai=irank_{sa_i} = i.

Kotori has a string s=s1s2sns=s_1s_2\dots s_n. She would like to ask mm queries. And in the ii-th query, a substring x=s[li..ri]x=s[l_i..r_i] of ss is given, Kotori would like to know the rank of suffix s[ki..ri]s[k_i..r_i] of xx.

Note s[l..r]s[l..r] means the substring of ss which starts from the ll-th position and ends at the rr-th position, both inclusive.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m5×1041 \le n, m \le 5 \times 10^4) -- the length of the string and the number of queries.

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

Each of the next mm lines contains three integers lil_i, rir_i and kik_i (1lirin,likiri1 \le l_i \le r_i \le n, l_i \le k_i \le r_i) denoting a query.

It is guaranteed that neither the sum of nn or the sum of mm of all test cases will exceed 5×1045 \times 10^4.

输出格式

For each query output one line containing one integer denoting the answer.

题目大意

有一个长度为 nn 的字符串 s=[s1,s2,..,sn]s=[s_1,s_2,..,s_n],有 mm 个询问,每个询问给定 l,r,kl,r,k,现将 ss 的子串 [sl,..,sr][s_l,..,s_r]rl+1r-l+1 个后缀字符串按字典序从小到大排序,问 [sk,..,sr][s_k,..,s_r] 在其中的排名。

TT 组数据。

数据保证 1n,m,n,m5×1041 \leq n,m,\sum n,\sum m \leq 5 \times 10^4;对于每组询问,保证 1lkrn1 \leq l \leq k \leq r \leq n

2
10 4
baaabbabba
2 8 3
1 1 1
2 3 2
2 5 4
20 3
cccbccbadaacbbbcccab
14 17 16
3 20 17
17 20 18

2
1
2
3
4
15
3