C - 川を行くボート / A Boat Going Down the River 解説 by admin
gemini-3.5-flash-highOverview
This problem asks us to quickly find the coordinate of Takahashi at a given time \(X_j\), as he moves according to the changes in the river’s current and his boat rowing speed.
Analysis
Takahashi’s velocity is constant during each time interval. Specifically, let \(V_i\) be the velocity during the time interval from \(T_i\) to \(T_{i+1}\) (where we define \(T_0 = 0\)). The velocity is determined as follows:
- From time \(0\) to before \(T_1\) (\(i = 0\)): Since there is no river current, the velocity is \(V_0 = 0\).
- From time \(T_i\) to before \(T_{i+1}\) (\(1 \le i < N\)), and after time \(T_N\) (\(i = N\)):
Since he rows the boat in the direction of the river current \(W_i\):
- When \(W_i > 0\) (eastward), the velocity is \(W_i + V\).
- When \(W_i < 0\) (westward), the velocity is \(W_i - V\).
If we simulate the coordinate at time \(X_j\) naively for each query, it will take up to \(O(N)\) time per query. Since the number of queries \(Q\) is up to \(10^5\) and the number of changes \(N\) is up to \(10^5\), the total time complexity would be \(O(NQ)\), which will exceed the time limit (resulting in TLE).
Therefore, we consider precomputing Takahashi’s coordinates at each change timing \(T_i\). If a given time \(X\) satisfies \(T_i \le X < T_{i+1}\), the coordinate at time \(X\) can be found as: “Coordinate at time \(T_i\)” \(+\) “Distance traveled from time \(T_i\) to \(X\)”
Specifically: - Let \(P_i\) be the coordinate at time \(T_i\). - Let \(V_i\) be the velocity between time \(T_i\) and \(T_{i+1}\). - Setting \(P_0 = 0\), we can precompute all \(P_i\) sequentially as \(P_i = P_{i-1} + V_{i-1} \times (T_i - T_{i-1})\).
When a query \(X\) is given, if we can quickly find the maximum \(i\) such that \(T_i \le X\), the coordinate can be computed instantly as \(P_i + V_i \times (X - T_i)\). Since \(T\) is sorted in ascending order, we can quickly find the index \(i\) that satisfies the condition using Binary Search.
Algorithm
Precomputation of Velocities and Coordinates:
- Prepare a list of times \(T = [0, T_1, T_2, \ldots, T_N]\).
- Calculate a list of velocities for each interval \(V_{list} = [0, V_1, V_2, \ldots, V_N]\).
- Calculate a list of coordinates at each change point \(P = [0, P_1, P_2, \ldots, P_N]\) using prefix sums as follows: $\(P_i = P_{i-1} + V_{list}[i-1] \times (T[i] - T[i-1])\)$
Answering Queries: For each query \(X\):
- Use binary search to find the maximum index \(i\) such that \(T[i] \le X\).
- Compute and output the coordinate at time \(X\) using the following formula: $\(\text{ans} = P_i + V_{list}[i] \times (X - T[i])\)$
Complexity
Time Complexity: \(O(N + Q \log N)\)
- In the precomputation, calculating the velocity and coordinate at each change point takes \(O(N)\) time.
- For each query, performing binary search takes \(O(\log N)\) time. Since there are \(Q\) queries, the query processing as a whole takes \(O(Q \log N)\) time.
- Therefore, the overall time complexity is \(O(N + Q \log N)\), which is well within the execution time limit.
Space Complexity: \(O(N + Q)\)
- The size of the arrays to store times, velocities, and coordinates is \(O(N)\).
- Storing the queries and results requires \(O(Q)\) memory.
Implementation Details
Utilizing Binary Search Libraries: By using
bisect_rightfrom Python’sbisectmodule, you can perform binary search concisely and quickly. Usingbisect_right(T_list, x) - 1allows you to retrieve the index of the largest element inT_listthat is less than or equal toxin \(O(\log N)\) time.Fast I/O: Since the number of inputs and outputs is large, using
sys.stdin.readandsys.stdout.writecan resolve I/O bottlenecks.Source Code
import sys
from bisect import bisect_right
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
V = int(input_data[1])
Q = int(input_data[2])
T_list = [0] * (N + 1)
V_list = [0] * (N + 1)
raw_events = input_data[3:3+2*N]
for i in range(N):
t = int(raw_events[2*i])
w = int(raw_events[2*i+1])
T_list[i+1] = t
if w > 0:
V_list[i+1] = w + V
else:
V_list[i+1] = w - V
X_pos = [0] * (N + 1)
for i in range(1, N + 1):
X_pos[i] = X_pos[i-1] + V_list[i-1] * (T_list[i] - T_list[i-1])
queries = input_data[3+2*N:]
out = []
for x_str in queries:
x = int(x_str)
pos_idx = bisect_right(T_list, x) - 1
ans = X_pos[pos_idx] + V_list[pos_idx] * (x - T_list[pos_idx])
out.append(str(ans))
sys.stdout.write('\n'.join(out) + '\n')
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3.5-flash-high.