#P9604. [IOI 2023] 超车
[IOI 2023] 超车
Background
This is an interactive problem. You only need to implement the functions required in the code.
Your code does not need to include any additional header files, and you do not need to implement the main function.
This problem only supports submissions in C++. However, please do not use C++14 (GCC 9).
Problem Description
There is a one-way, single-lane road from Budapest Airport to the Forrás Hotel, and the road has length kilometers.
During IOI 2023, there are buses driving on this road. The buses are numbered from to . Bus () is planned to depart from the airport at second , and it needs seconds to travel one kilometer. Bus is a spare bus; it needs seconds to travel one kilometer. Its departure time from the airport is not determined yet.
On this road, overtaking is normally not allowed, but it is allowed at some places called dispatch stations. There are dispatch stations (), numbered from to , located at distinct positions on the road. Dispatch station () is located at kilometers from the airport along the road. The dispatch stations are listed in increasing order of distance from the airport, i.e., for every , we have . The first dispatch station is at the airport and the last one is at the hotel. That is, and .
Each bus drives at its maximum speed unless it encounters a slower bus in front of it. In that case, the faster bus behind is blocked by the slower bus and is forced to drive at the slower bus's speed. This continues until both buses reach the next dispatch station, where the faster bus overtakes the slower one.
Formally, for each pair and such that and , the time (in seconds) when bus arrives at dispatch station is defined as follows: for each , we have . Also, . For each such that :
- Define the expected arrival time (in seconds) of bus at dispatch station as the time when bus reaches dispatch station if it drives at full speed after arriving at dispatch station . That is,
- for each , ;
- and .
- The arrival time of bus at dispatch station is the maximum of the expected arrival time of bus at dispatch station , and the expected arrival times at dispatch station of all other buses that arrived at dispatch station earlier than bus . Formally, is the maximum of and all such that and .
The IOI committee wants to schedule the spare bus (bus ). Your task is to answer queries from the committee, each of the following form: given the departure time (in seconds) of the spare bus from the airport, when will it arrive at the hotel?
Input Format
The sample grader reads input in the following format:
- Line :
- Line :
- Line :
- Line :
- Line (): the value for query
Output Format
The sample grader prints your answers in the following format:
- Line (): the return value of
arrival_timefor query
6 4 10 4 2
20 10 40 0
5 20 20 30
0 1 3 6
0
50
60
130
Hint
[Implementation details]
Your task is to implement the following functions:
void init(int L, int N, int64[] T, int[] W, int X, int M, int[] S)
- : the length of the road
- : the number of regular (non-spare) buses
- : an array of length , describing the planned departure times of the regular buses from the airport.
- : an array of length , describing the maximum speeds of the regular buses.
- : the time (in seconds) the spare bus needs to travel one kilometer
- : the number of dispatch stations
- : an array of length , describing the distances from the airport to the dispatch stations.
- For each test case, this function is called exactly once, before any call to
arrival_time.
int64 arrival_time(int64 Y)
- : the planned departure time of the spare bus (bus ) from the airport
- This function should return the time when the spare bus arrives at the hotel.
- This function is called exactly times.
[Example]
Consider the following call sequence:
init(6, 4, [20, 10, 40, 0], [5, 20, 20, 30], 10, 4, [0, 1, 3, 6])
Ignoring bus (since its departure time has not been determined yet), the table below lists the expected and actual arrival times of the buses at each dispatch station:
The time when a bus arrives at dispatch station is exactly its planned departure time from the airport. That is, for , .
The expected and actual arrival times at dispatch station are computed as follows:
- Expected arrival times at dispatch station :
- Bus : $e_{0,1} = t_{0,0} + W[0] \cdot (S[1]-S[0]) = 20 + 5 \cdot 1 = 25$.
- Bus : $e_{1,1} = t_{1,0} + W[1] \cdot (S[1]-S[0]) = 10 + 20 \cdot 1 = 30$.
- Bus : $e_{2,1} = t_{2,0} + W[2] \cdot (S[1]-S[0]) = 40 + 20 \cdot 1 = 60$.
- Bus : $e_{3,1} = t_{3,0} + W[3] \cdot (S[1]-S[0]) = 0 + 30 \cdot 1 = 30$.
- Arrival times at dispatch station :
- Buses and arrived at dispatch station earlier than bus , so .
- Bus arrived at dispatch station earlier than bus , so .
- Buses , , and arrived at dispatch station earlier than bus , so $t_{2,1} = \max([e_{0,1},e_{1,1},e_{2,1},e_{3,1}]) = 60$.
- There is no bus that arrived at dispatch station earlier than bus , so .
arrival_time(0)
Bus needs seconds to travel one kilometer, and it is now planned to depart from the airport at second . In this case, the table below lists the arrival times of each bus. The only changes in the regular buses' expected and actual arrival times are underlined.
From this, we can see that bus arrives at the hotel at second . Therefore, the function should return .
arrival_time(50)
Bus is now planned to depart from the airport at second . In this case, compared to the initial table, the arrival times of the regular buses do not change. The table below lists the arrival times.
Bus and the slower bus arrive at dispatch station at the same time, and then bus overtakes bus . Next, while bus is traveling between dispatch stations and , it is blocked by bus , which makes it arrive at dispatch station at second instead of second . After passing dispatch station , bus is blocked by bus until they reach the hotel. Bus arrives at the hotel at second . Therefore, the function should return .
Plot the time of each bus from departure at the airport to different distances as a polyline chart. In the figure, the -axis represents the distance from the airport (in kilometers), and the -axis represents time (in seconds). The vertical dashed lines mark the locations of the dispatch stations. The solid lines in different colors (labeled with bus numbers) represent the four regular buses. The black dotted line represents the spare bus.
arrival_time(0) |
arrival_time(50) |
|---|---|
![]() |
![]() |
[Constraints]
- (for each such that )
- (for each such that )
[Subtasks]
- (9 points) .
- (10 points) .
- (20 points) .
- (26 points) .
- (35 points) No additional constraints.
Translated by ChatGPT 5

