#P4559. [JSOI2018] 列队

    ID: 3498 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018线段树各省省选递归江苏主席树

[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 nn students, and the rest position of the ii-th student is aia_i. For each command, the instructor specifies an interval [l,r][l, r] and a meeting point KK. All students whose indices are in [l,r][l, r] must rush to the meeting point to line up. During the lineup, each student must choose an integer coordinate in [K,K+rl][K, K + r - l] to stand on, and no two students may choose the same coordinate. If a student runs from coordinate xx to coordinate yy, the stamina cost is yx\vert y - x \vert.

During one day of training, the instructor issues mm commands (l,r,K)(l, r, K). For each command, you need to compute the minimum possible total stamina cost over all valid lineup assignments.

Additional clarifications:

  1. 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.
  2. During gathering, there may be students whose indices are not in [l,r][l, r] but are located within [K,K+rl][K, K + r - l]. 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 n,mn, m.

The second line contains nn integers aia_i representing the rest positions of the students. All rest positions are pairwise distinct.

Each of the next mm lines contains three integers l,r,Kl, r, K 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

Explanation for Sample 1\,\boldsymbol{\text{Explanation for Sample 1}}

In the first command, the five students run to [2,5,4,6,3][2,5,4,6,3] respectively; the total cost is 21+55+47+66+32=5|2-1|+|5-5|+|4-7|+|6-6|+|3-2|=5.

In the second command, the five students run to [4,5,7,6,3][4,5,7,6,3] respectively; the total cost is 41+55+77+66+32=4|4-1|+|5-5|+|7-7|+|6-6|+|3-2|=4.

In the third command, the three students run to [11,10,9][11,10,9] respectively; the total cost is 111+105+97=17|11-1|+|10-5|+|9-7|=17.

In the fourth command, the three students run to [4,2,3][4,2,3] respectively; the total cost is 45+27+36=9|4-5|+|2-7|+|3-6|=9.

In the fifth command, the three students run to [7,6,5][7,6,5] respectively; the total cost is 77+66+52=3|7-7|+|6-6|+|5-2|=3.

Constraints\,\boldsymbol{\text{Constraints}}

For 10%10\% of the testdata, n,m10n, m \leq 10.

For 40%40\% of the testdata, n,m103n, m \leq 10^3.

For 70%70\% of the testdata, n,m105n, m \leq 10^5.

For 100%100\% of the testdata, n,m5×105n, m \leq 5 \times 10^5, 1ai,K1061 \leq a_i, K \leq 10^6.

For 100%100\% of the testdata, the students’ rest positions are pairwise distinct.

Translated by ChatGPT 5