#P14668. [ICPC 2025 Seoul R] Badge Relay

[ICPC 2025 Seoul R] Badge Relay

题目描述

A research facility operates two buildings, Left Lab and Right Lab, connected by a single secure corridor. At the beginning, all selected employees are standing in the Left Lab.

  • The corridor can hold at most two people at a time.
  • There is exactly one security badge, and every traversal of the corridor (whether by one person or two together) must be accompanied by the badge.
  • If two people travel together, they must walk side by side, and the traversal time is equal to the slower person's time.
  • If there are still employees remaining in the Left Lab after a traversal to the Right Lab, then someone in Right Lab must bring the badge back to the Left Lab before the next traversal from the Left Lab to the Right Lab can begin.

To see how the strategy affects the total time, consider four employees whose individual traversal times {1,2,5,10}\{1, 2, 5, 10\}. One possible strategy is as follows. If (1,2)(1, 2) traverse first (taking 2 minutes), then (1)(1) returns with the badge (1 minute), then (1,5)(1, 5) traverse (5 minutes), (1)(1) returns (1 minute), and finally (1,10)(1, 10) traverse (10 minutes), the total time is 1919 minutes. However, there is another (better) strategy; if (1,2)(1, 2) traverse first (2 minutes), (1)(1) returns (1 minute), then (5,10)(5, 10) traverse together (10 minutes), (2)(2) returns (2 minutes), and finally (1,2)(1, 2) traverse again (2 minutes), the total time becomes 1717 minutes, which yields a smaller total traversal time than the first. For convenience, the two crossing sequences are summarized in Tables 1 and 2 below.

:::align{center} Table 1. Sequence A (Total 19 min) :::

Step Action Left Lab (after) Right Lab (after) Duration (min) Elapsed (min)
0 - {1,2,5,10}\{1, 2, 5, 10\} \emptyset -
1 Cross (1,2)(1, 2) {5,10}\{5, 10\} {1,2}\{1, 2\} 22
2 Return (1)(1) {1,5,10}\{1, 5, 10\} {2}\{2\} 11 33
3 Cross (1,5)(1, 5) {10}\{10\} {1,2,5}\{1, 2, 5\} 55 88
4 Return (1)(1) {1,10}\{1, 10\} {2,5}\{2, 5\} 11 99
5 Cross (1,10)(1, 10) \emptyset {1,2,5,10}\{1, 2, 5, 10\} 1010 1919

:::align{center} Table 2. Sequence B (Total 17 min) :::

Step Action Left Lab (after) Right Lab (after) Duration (min) Elapsed (min)
0 - {1,2,5,10}\{1, 2, 5, 10\} \emptyset -
1 Cross (1,2)(1, 2) {5,10}\{5, 10\} {1,2}\{1, 2\} 22
2 Return (1)(1) {1,5,10}\{1, 5, 10\} {2}\{2\} 11 33
3 Cross (5,10)(5, 10) {1}\{1\} {2,5,10}\{2, 5, 10\} 1010 1313
4 Return (2)(2) {1,2}\{1, 2\} {5,10}\{5, 10\} 22 1515
5 Cross (1,2)(1, 2) \emptyset {1,2,5,10}\{1, 2, 5, 10\} 1717

You are given nn employees. Employee ii needs TiT_i minutes to traverse the corridor. You are also given qq queries. Each query is specified by:

  • an index range [x,y][x, y] (inclusive),
  • a time range [a,b][a, b] (inclusive), and
  • a selection cap KK (the maximum number of employees you may select).

For each query, consider all employees whose indices lie in [x,y][x, y] and whose traversal times lie in [a,b][a, b]. Among these employees, select the KK employees who have the smallest individual traversal times. If fewer than KK such employees exist, select all of them. These selected employees all start in the Left Lab with the single badge. Under the rules described above, you should compute the minimum total traversal time required for all selected employees to reach the Right Lab. If no employee is selected, the answer should be zero.

Given nn employees with traversal times T1,,TnT_1, \cdots, T_n and qq queries of the form (x,y,a,b,K)(x, y, a, b, K), write a program that processes the queries and outputs, for each query, the minimum total traversal time for the selected employees to move from the Left Lab to the Right Lab.

输入格式

Your program is to read from standard input. The input starts with a line containing two integers nn and qq (1n,q100,0001 \le n, q \le 100,000), where nn is the number of employees and qq is the number of queries. Employees are numbered from 1 to nn. The second line contains nn integers T1,,TnT_1, \cdots, T_n (1Ti1091 \le T_i \le 10^9), where TiT_i is the traversal time of employee ii (1in1 \le i \le n). Each of the following qq lines contains five integers x,y,a,b,Kx, y, a, b, K described above, where 1xyn1 \le x \le y \le n; 1ab1091 \le a \le b \le 10^9; 1Kn1 \le K \le n.

输出格式

Your program is to write to standard output. For each query, print a single line containing the minimum total traversal time for the selected employees to move from the Left Lab to the Right Lab. If no employee is selected, output 0.

3 3
1 2 3
1 3 1 3 3
1 3 1 3 2
1 3 4 5 1
6
2
0
4 4
5 1 10 2
1 4 1 10 4
1 4 2 10 2
1 4 2 10 4
1 3 1 13 3
17
5
17
16

提示

翻译由 DeepSeek V3 完成