#P6385. 『MdOI R2』Little Goth

    ID: 6903 远端评测题 1000~3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串动态规划 DP贪心后缀自动机 SAMO2优化广度优先搜索 BFS动态树 LCT分块后缀树洛谷月赛

『MdOI R2』Little Goth

Background

Little M and Little B are a pair of good friends, and they really like hiking.

Problem Description

A mountain can be abstracted as a string SS of length nn, containing only lowercase letters.

For a string SS, we define S|S| as the length of the string, and SLRS_{L\ldots R} as the string formed by concatenating, from left to right, the LL-th character to the RR-th character in SS.

Little M starts at position ii, and she wants to reach the peak at position kk, while Little B will help her. To do this, they need to perform a sequence of operations.

They must use the teleportation magic circle at position pp exactly once before all operations. By casting the spell, Little B's position can be changed to any jj satisfying jij \geq i and Sij=Spp+(ji)S_{i \ldots j} = S_{p \ldots p + (j-i)}. At the same time, they need to pay a cost of njn-j. It is guaranteed that such a jj exists.

After that, suppose Little M and Little B are at positions ii and jj, respectively. They may perform any number of times one of the following operations:

  • Let Little M climb, i.e. set i=i+1i=i+1 or i=i1i=i-1. If after this operation i>ji>j, then set j=ij=i.

  • Let Little B climb, i.e. set j=j+1j=j+1 or j=j1j=j-1. If after this operation i>ji>j, then set i=ji=j.

  • Use "Spiral Ascension". Specifically, choose two indices ll and rr such that Slr=SijS_{l \ldots r} = S_{i \ldots j}, then set i=l,j=ri=l,j=r.

For some reason, after each operation, it must be guaranteed that 1i,jn1 \leq i , j \leq n. Performing any one of the above operations costs 11.

Hiking is tiring, so they want to know the minimum total cost needed to make Little M reach the peak, i.e. make i=ki=k. Also, because they like hiking very much, they have many queries to ask you.

Input Format

The first line contains two integers nn and qq, representing the length of the string SS and the number of queries.

The second line contains a string SS of length nn, consisting only of lowercase letters.

The next qq lines each contain three integers i,pi,p and kk, indicating Little M's position, the teleportation magic circle position, and the peak position.

Output Format

Output qq lines in total. The ii-th line contains an integer, representing for the ii-th query with given i,pi,p and kk, the minimum total cost needed to make Little M reach the peak, i.e. make Little M's position i=ki=k.

8 2
dacdaaaa
5 8 1
1 4 5
5
8

Hint

【Help and Hints】

To make it easier for contestants to test their code, this problem additionally provides an extra sample for contestants to use.

Sample Input Sample Output


【Sample Explanation】

For the first query in the sample, when using the teleportation spell, we can only set j=5j=5, paying a cost of 85=38-5=3. After that, first use the third type of operation once, choosing l=2,r=2l=2,r=2 and setting i=l,j=ri=l,j=r, then use the first type of operation once, setting i1i-1, and thus make i=ki=k. The total cost is 55.

For the second query, we can choose j=2j=2, paying a cost of 82=68-2=6, then use the third type of operation once, choosing l=4,r=5l=4,r=5 and setting i=l,j=ri=l,j=r, and then perform the first type of operation once, setting i+1i+1, to make i=ki=k. The total cost is 88.


【Constraints】

This problem uses bundled testdata.

For all testdata, it is guaranteed that 1n,q3×1041 \leq n,q \leq 3\times 10^4, and SS contains only lowercase letters.

Subtask ID nn\leq qq \leq Special Property Score Time Limit
Subtask 1 1515 None 33 1s
Subtask 2 8080 1414
Subtask 3 2×1042 \times 10^4 SS contains only a 88 3s
Subtask 4 S1S_1 77
Subtask 5 400400 None 99 1s
Subtask 6 2×1042\times 10^4 2×1042 \times 10^4 All queries have the same kk 1010 3s
Subtask 7 10310^3 None 2s
Subtask 8 1.5×1041.5 \times 10^4 1111 3s
Subtask 9 3×1043 \times 10^4 2828

Property S1S_1 is: for a given pp, the jj that satisfies the condition is unique.

Translated by ChatGPT 5