Under construction

Still being built. Things may change

Overtaking

Time limit2sMemory limit1024 MB

Summary
Buses on a one-way road can overtake only at sorting stations. For each query departure time Y of a reserve bus, find when it reaches the hotel.
Level

Hard9 of 10

Topics
Segment tree, Binary search, Simulation, Sorting
Solved
No attempts yet

Statement

A one-lane, one-way road runs from Budapest Airport to Hotel Forrás. The road is LL kilometres long.

During the IOI 2023 event, N+1N+1 transfer buses travel on this road. The buses are numbered from 00 to NN. Bus ii (0≤i<N0 \le i < N) is scheduled to leave the airport at the T[i]T[i]-th second of the event, and it can travel 1 kilometre in W[i]W[i] seconds. Bus NN is a reserve bus that can travel 1 kilometre in XX seconds. The time YY when the reserve bus leaves the airport has not been decided yet.

Overtaking is generally not allowed on the road. Instead, buses may overtake each other at sorting stations. There are MM sorting stations (M>1M > 1), numbered from 00 to M−1M-1, at different positions on the road. Sorting station jj is S[j]S[j] kilometres from the airport. The stations are in increasing order of distance, so S[j]<S[j+1]S[j] < S[j+1] for each 0≤j≤M−20 \le j \le M-2. The first station is the airport and the last station is the hotel, so S[0]=0S[0] = 0 and S[M−1]=LS[M-1] = L.

A bus travels at its maximum speed unless it catches up to a slower bus ahead of it. In that case, the two buses bunch together and travel at the speed of the slower bus until they reach the next sorting station. There, the faster bus overtakes the slower one.

Formally, let ti,jt_{i,j} be the time in seconds when bus ii arrives at sorting station jj, for each 0≤i≤N0 \le i \le N and 0≤j<M0 \le j < M. Let ti,0=T[i]t_{i,0} = T[i] for 0≤i<N0 \le i < N, and let tN,0=Yt_{N,0} = Y. For each 0<j<M0 < j < M, define the expected time of arrival ei,je_{i,j} as the time bus ii would reach station jj if it travelled at full speed from station j−1j-1:

ei,j=ti,j−1+W[i]⋅(S[j]−S[j−1]) for 0≤i<Ne_{i,j} = t_{i,j-1} + W[i] \cdot (S[j]-S[j-1]) \text{ for } 0 \le i < N

eN,j=tN,j−1+X⋅(S[j]−S[j−1])e_{N,j} = t_{N,j-1} + X \cdot (S[j]-S[j-1])

Bus ii arrives at station jj at the maximum of ei,je_{i,j} and ek,je_{k,j} for every bus kk (0≤k≤N0 \le k \le N) with tk,j−1<ti,j−1t_{k,j-1} < t_{i,j-1}.

You must answer QQ queries. Each query gives the departure time YY of the reserve bus. Find the time at which the reserve bus arrives at the hotel.

Constraints

1≤L≤1091 \le L \le 10^9, 1≤N≤10001 \le N \le 1000, 0≤T[i]≤10180 \le T[i] \le 10^{18} for each ii with 0≤i<N0 \le i < N, 1≤W[i]≤1091 \le W[i] \le 10^9 for each ii with 0≤i<N0 \le i < N, 1≤X≤1091 \le X \le 10^9, 2≤M≤10002 \le M \le 1000, 0=S[0]<S[1]<⋯<S[M−1]=L0 = S[0] < S[1] < \cdots < S[M-1] = L, 1≤Q≤1061 \le Q \le 10^6, 0≤Y≤10180 \le Y \le 10^{18}.

Examples1

  1. Example 1

    Input
    1 1 1 2 1
    0
    1
    0 1
    0
    
    Expected output
    1