#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 LL kilometers.

During IOI 2023, there are N+1N+1 buses driving on this road. The buses are numbered from 00 to NN. Bus ii (0i<N0 \le i \lt N) is planned to depart from the airport at second T[i]T[i], and it needs W[i]W[i] seconds to travel one kilometer. Bus NN is a spare bus; it needs XX seconds to travel one kilometer. Its departure time YY 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 MM dispatch stations (M>1M \gt 1), numbered from 00 to M1M - 1, located at distinct positions on the road. Dispatch station jj (0j<M0 \le j \lt M) is located at S[j]S[j] kilometers from the airport along the road. The dispatch stations are listed in increasing order of distance from the airport, i.e., for every 0jM20 \le j \le M - 2, we have S[j]<S[j+1]S[j] \lt S[j+1]. The first dispatch station is at the airport and the last one is at the hotel. That is, S[0]=0S[0] = 0 and S[M1]=LS[M-1] = L.

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 ii and jj such that 0iN0 \le i \le N and 0j<M0 \le j \lt M, the time ti,jt_{i,j} (in seconds) when bus ii arrives at dispatch station jj is defined as follows: for each 0i<N0 \le i \lt N, we have ti,0=T[i]t_{i,0} = T[i]. Also, tN,0=Yt_{N,0} = Y. For each jj such that 0<j<M0 \lt j \lt M:

  • Define the expected arrival time ei,je_{i,j} (in seconds) of bus ii at dispatch station jj as the time when bus ii reaches dispatch station jj if it drives at full speed after arriving at dispatch station j1j-1. That is,
    • for each 0i<N0 \le i \lt N, ei,j=ti,j1+W[i](S[j]S[j1])e_{i,j} = t_{i,j-1} + W[i] \cdot (S[j]-S[j-1]);
    • and eN,j=tN,j1+X(S[j]S[j1])e_{N,j} = t_{N,j-1} + X \cdot (S[j]-S[j-1]).
  • The arrival time of bus ii at dispatch station jj is the maximum of the expected arrival time of bus ii at dispatch station jj, and the expected arrival times at dispatch station jj of all other buses that arrived at dispatch station j1j-1 earlier than bus ii. Formally, ti,jt_{i,j} is the maximum of ei,je_{i,j} and all ek,je_{k,j} such that 0kN0 \le k \le N and tk,j1<ti,j1t_{k,j-1} \lt t_{i,j-1}.

The IOI committee wants to schedule the spare bus (bus NN). Your task is to answer QQ queries from the committee, each of the following form: given the departure time YY (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 11: L  N  X  M  QL \; N \; X \; M \; Q
  • Line 22: T[0]  T[1]    T[N1]T[0] \; T[1] \; \ldots \; T[N-1]
  • Line 33: W[0]  W[1]    W[N1]W[0] \; W[1] \; \ldots \; W[N-1]
  • Line 44: S[0]  S[1]    S[M1]S[0] \; S[1] \; \ldots \; S[M-1]
  • Line 5+k5 + k (0k<Q0 \le k \lt Q): the value YY for query kk

Output Format

The sample grader prints your answers in the following format:

  • Line 1+k1 + k (0k<Q0 \le k \lt Q): the return value of arrival_time for query kk
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)
  • LL: the length of the road
  • NN: the number of regular (non-spare) buses
  • TT: an array of length NN, describing the planned departure times of the regular buses from the airport.
  • WW: an array of length NN, describing the maximum speeds of the regular buses.
  • XX: the time (in seconds) the spare bus needs to travel one kilometer
  • MM: the number of dispatch stations
  • SS: an array of length MM, 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)
  • YY: the planned departure time of the spare bus (bus NN) from the airport
  • This function should return the time when the spare bus arrives at the hotel.
  • This function is called exactly QQ 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 44 (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:

ii ti,0t_{i,0} ei,1e_{i,1} ti,1t_{i,1} ei,2e_{i,2} ti,2t_{i,2} ei,3e_{i,3} ti,3t_{i,3}
00 2020 2525 3030 4040 5555
11 1010 3030 7070 130130
22 4040 6060 100100 160160 180180
33 00 3030 9090 180180

The time when a bus arrives at dispatch station 00 is exactly its planned departure time from the airport. That is, for 0i30 \le i \le 3, ti,0=T[i]t_{i,0} = T[i].

The expected and actual arrival times at dispatch station 11 are computed as follows:

  • Expected arrival times at dispatch station 11:
    • Bus 00: $e_{0,1} = t_{0,0} + W[0] \cdot (S[1]-S[0]) = 20 + 5 \cdot 1 = 25$.
    • Bus 11: $e_{1,1} = t_{1,0} + W[1] \cdot (S[1]-S[0]) = 10 + 20 \cdot 1 = 30$.
    • Bus 22: $e_{2,1} = t_{2,0} + W[2] \cdot (S[1]-S[0]) = 40 + 20 \cdot 1 = 60$.
    • Bus 33: $e_{3,1} = t_{3,0} + W[3] \cdot (S[1]-S[0]) = 0 + 30 \cdot 1 = 30$.
  • Arrival times at dispatch station 11:
    • Buses 11 and 33 arrived at dispatch station 00 earlier than bus 00, so t0,1=max([e0,1,e1,1,e3,1])=30t_{0,1} = \max([e_{0,1},e_{1,1},e_{3,1}]) = 30.
    • Bus 33 arrived at dispatch station 00 earlier than bus 11, so t1,1=max([e1,1,e3,1])=30t_{1,1} = \max([e_{1,1},e_{3,1}]) = 30.
    • Buses 00, 11, and 33 arrived at dispatch station 00 earlier than bus 22, 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 00 earlier than bus 33, so t3,1=max([e3,1])=30t_{3,1} = \max([e_{3,1}]) = 30.
arrival_time(0)

Bus 44 needs 1010 seconds to travel one kilometer, and it is now planned to depart from the airport at second 00. 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.

ii ti,0t_{i,0} ei,1e_{i,1} ti,1t_{i,1} ei,2e_{i,2} ti,2t_{i,2} ei,3e_{i,3} ti,3t_{i,3}
00 2020 2525 3030 4040 5555 60\underline{60}
11 1010 3030 7070 130130
22 4040 6060 100100 160160 180180
33 00 3030 9090 180180
44 1010 3030 6060

From this, we can see that bus 44 arrives at the hotel at second 6060. Therefore, the function should return 6060.

arrival_time(50)

Bus 44 is now planned to depart from the airport at second 5050. 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.

ii ti,0t_{i,0} ei,1e_{i,1} ti,1t_{i,1} ei,2e_{i,2} ti,2t_{i,2} ei,3e_{i,3} ti,3t_{i,3}
00 2020 2525 3030 4040 5555
11 1010 3030 7070 130130
22 4040 6060 100100 160160 180180
33 00 3030 9090 9090 180180
44 5050 6060 8080 120120 130130

Bus 44 and the slower bus 22 arrive at dispatch station 11 at the same time, and then bus 44 overtakes bus 22. Next, while bus 44 is traveling between dispatch stations 11 and 22, it is blocked by bus 33, which makes it arrive at dispatch station 22 at second 9090 instead of second 8080. After passing dispatch station 22, bus 44 is blocked by bus 11 until they reach the hotel. Bus 44 arrives at the hotel at second 130130. Therefore, the function should return 130130.

Plot the time of each bus from departure at the airport to different distances as a polyline chart. In the figure, the xx-axis represents the distance from the airport (in kilometers), and the yy-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]

  • 1L1091 \le L \le 10^9
  • 1N10001 \le N \le 1\,000
  • 0T[i]10180 \le T[i] \le 10^{18}(for each ii such that 0i<N0 \le i \lt N
  • 1W[i]1091 \le W[i] \le 10^9(for each ii such that 0i<N0 \le i \lt N
  • 1X1091 \le X \le 10^9
  • 2M10002 \le M \le 1\,000
  • 0=S[0]<S[1]<<S[M1]=L0 = S[0] \lt S[1] \lt \cdots \lt S[M-1] = L
  • 1Q1061 \le Q \le 10^6
  • 0Y10180 \le Y \le 10^{18}

[Subtasks]

  1. (9 points) N=1,Q1000N = 1, Q \le 1\,000.
  2. (10 points) M=2,Q1000M = 2, Q \le 1\,000.
  3. (20 points) N,M,Q100N, M, Q \le 100.
  4. (26 points) Q5000Q \le 5\,000.
  5. (35 points) No additional constraints.

Translated by ChatGPT 5