#P6385. 『MdOI R2』Little Goth
『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 of length , containing only lowercase letters.
For a string , we define as the length of the string, and as the string formed by concatenating, from left to right, the -th character to the -th character in .
Little M starts at position , and she wants to reach the peak at position , 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 exactly once before all operations. By casting the spell, Little B's position can be changed to any satisfying and . At the same time, they need to pay a cost of . It is guaranteed that such a exists.
After that, suppose Little M and Little B are at positions and , respectively. They may perform any number of times one of the following operations:
-
Let Little M climb, i.e. set or . If after this operation , then set .
-
Let Little B climb, i.e. set or . If after this operation , then set .
-
Use "Spiral Ascension". Specifically, choose two indices and such that , then set .
For some reason, after each operation, it must be guaranteed that . Performing any one of the above operations costs .
Hiking is tiring, so they want to know the minimum total cost needed to make Little M reach the peak, i.e. make . Also, because they like hiking very much, they have many queries to ask you.
Input Format
The first line contains two integers and , representing the length of the string and the number of queries.
The second line contains a string of length , consisting only of lowercase letters.
The next lines each contain three integers and , indicating Little M's position, the teleportation magic circle position, and the peak position.
Output Format
Output lines in total. The -th line contains an integer, representing for the -th query with given and , the minimum total cost needed to make Little M reach the peak, i.e. make Little M's position .
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 Explanation】
For the first query in the sample, when using the teleportation spell, we can only set , paying a cost of . After that, first use the third type of operation once, choosing and setting , then use the first type of operation once, setting , and thus make . The total cost is .
For the second query, we can choose , paying a cost of , then use the third type of operation once, choosing and setting , and then perform the first type of operation once, setting , to make . The total cost is .
【Constraints】
This problem uses bundled testdata.
For all testdata, it is guaranteed that , and contains only lowercase letters.
| Subtask ID | Special Property | Score | Time Limit | ||
|---|---|---|---|---|---|
| Subtask 1 | None | 1s | |||
| Subtask 2 | |||||
| Subtask 3 | contains only a |
3s | |||
| Subtask 4 | |||||
| Subtask 5 | None | 1s | |||
| Subtask 6 | All queries have the same | 3s | |||
| Subtask 7 | None | 2s | |||
| Subtask 8 | 3s | ||||
| Subtask 9 | |||||
Property is: for a given , the that satisfies the condition is unique.
Translated by ChatGPT 5