Xiao Ming has a string $$$S$$$ of length $$$N$$$, where each character belongs to the set $$$\Sigma=\{a,b,c,d,e\}$$$.
The hash value of a string $$$T$$$ of length $$$M$$$ is defined as $$$\sum_{i=1}^{M}(T_i-\text{'a'}+1) \times 1331^{M-i} \pmod{2^{32}}$$$. Here, the subtraction is performed by converting the characters to their corresponding ASCII codes.
Now, Xiao Ming has $$$Q$$$ queries. Each query consists of three positive integers $$$l,r,k$$$. You need to consider all distinct subsequences within the interval $$$[l,r]$$$ that start with the character $$$S_l$$$, end with the character $$$S_r$$$, and have a length of at least 2. These subsequences should be sorted lexicographically from smallest to largest, and you should output the hash value of the $$$k$$$-th subsequence in this order. Specifically, if the number of distinct subsequences in the interval $$$[l,r]$$$ is less than $$$k$$$, output '-1'.
A subsequence within the interval $$$[l,r]$$$ is obtained by deleting some characters from that interval. Two subsequences with lengths $$$a$$$ and $$$b$$$, respectively, are considered distinct if and only if $$$a \ne b$$$, or there exists an integer $$$i$$$ such that $$$1 \le i \le a$$$ and the $$$i$$$-th characters of the two subsequences differ.
The first line of input contains two positive integers $$$N,Q$$$, representing the length of the string and the number of queries.
The second line contains a string $$$S$$$ of length $$$N$$$.
The next $$$Q$$$ lines each contain three positive integers $$$l,r,k$$$.
$$$1 \le N \le 10^5$$$, $$$1 \le Q \le 10^5$$$, $$$1 \le l \le r \le N$$$, $$$1 \le k \le 10^9$$$, $$$\forall 1 \le i \le N$$$, $$$S_i \in \{a,b,c,d,e\}$$$.
For each query, output a number on a separate line, representing the answer.
2 3aa1 2 11 2 21 1 1
1332 -1 -1
| Название |
|---|


