#P4559. [JSOI2018] 列队
[JSOI2018] 列队
Description
As a university student, Jiutiao Kelian (pinyin) took part in the last military training of her life last year.
An important task in the training is practicing lineups. To train the students, the instructor assigns each student a rest position. Before each training session starts, all students rest at their own rest positions. When the instructor gives the gathering command, the selected students must gather at the specified location.
To simplify the problem, we model the rest positions and meeting positions as a number line. There are students, and the rest position of the -th student is . For each command, the instructor specifies an interval and a meeting point . All students whose indices are in must rush to the meeting point to line up. During the lineup, each student must choose an integer coordinate in to stand on, and no two students may choose the same coordinate. If a student runs from coordinate to coordinate , the stamina cost is .
During one day of training, the instructor issues commands . For each command, you need to compute the minimum possible total stamina cost over all valid lineup assignments.
Additional clarifications:
- Any two commands are independent. That is, after one gathering command ends, all students return to their rest positions before the instructor issues the next command.
- During gathering, there may be students whose indices are not in but are located within . They will move away on their own, and their movement distance is not counted in the total stamina cost.
Input Format
The first line contains two integers .
The second line contains integers representing the rest positions of the students. All rest positions are pairwise distinct.
Each of the next lines contains three integers describing a command.
Output Format
For each command, output one line with a single integer, the minimum total stamina cost.
5 5
1 5 7 6 2
1 5 2
1 5 3
1 3 9
2 4 2
3 5 5
5
4
17
9
3
Hint
In the first command, the five students run to respectively; the total cost is .
In the second command, the five students run to respectively; the total cost is .
In the third command, the three students run to respectively; the total cost is .
In the fourth command, the three students run to respectively; the total cost is .
In the fifth command, the three students run to respectively; the total cost is .
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , .
For of the testdata, the students’ rest positions are pairwise distinct.
Translated by ChatGPT 5
京公网安备 11011102002149号