C - 川を行くボート / A Boat Going Down the River 解説 by admin
gpt-5.5-highOverview
Since the river’s flow speed is constant in each interval, Takahashi’s speed is also constant in each interval.
We precompute the coordinates at each change time, and then use binary search to find which interval each query time belongs to.
Analysis
From time \(T_i\) until the next change time, the river’s flow speed is constant.
Letting \(W_i\) be the river’s flow speed, Takahashi’s speed relative to the ground is as follows:
- When \(W_i > 0\), it is \(W_i + V\) eastward.
- When \(W_i < 0\), it is \(|W_i| + V\) westward, which means the velocity is \(W_i - V\).
Therefore, the speed in each interval is constant.
For example, letting \(u_i\) be the velocity in the interval \([T_i, T_{i+1})\),
\[ u_i = \begin{cases} W_i + V & (W_i > 0) \\ W_i - V & (W_i < 0) \end{cases} \]
From time \(0\) to \(T_1\), there is no river flow, so Takahashi does not move.
Thus, the coordinate at time \(T_1\) is \(0\).
If we naively iterate through the intervals from the beginning for each query, it takes \(O(N)\) time per query.
Since the sum of \(N\) and \(Q\) is up to \(2 \times 10^5\) at maximum, the worst-case time complexity would be \(O(NQ)\), which is too slow to pass within the time limit.
To optimize this, we can precompute the coordinates at each change time \(T_i\). Then, by using binary search to find which interval the query time \(X_j\) belongs to, we can process each query in \(O(\log N)\) time.
Algorithm
First, we calculate the velocity in each interval.
\[ \text{vel}[i] = \begin{cases} W_i + V & (W_i > 0) \\ W_i - V & (W_i < 0) \end{cases} \]
This represents Takahashi’s velocity in the interval starting at time \(T_i\).
Next, we prepare an array \(\text{pos}\).
Let \(\text{pos}[i]\) be the coordinate at time \(T_i\).
Since he does not move until time \(T_1\),
\[ \text{pos}[0] = 0 \]
After that, we add the displacement from the previous change time to the current change time.
\[ \text{pos}[i] = \text{pos}[i-1] + (T_i - T_{i-1}) \times \text{vel}[i-1] \]
With this, the coordinates at all change times are determined.
For each query time \(X\), we answer as follows:
- Find the maximum \(i\) such that \(T_i \leq X\) using binary search.
- If no such \(i\) exists (i.e., \(X < T_1\)), the answer is \(0\).
- Otherwise, since he moves from time \(T_i\) to \(X\) with velocity \(\text{vel}[i]\), the answer is:
\[ \text{answer} = \text{pos}[i] + (X - T_i) \times \text{vel}[i] \]
In Python, we can find the index of the maximum \(T_i\) that is less than or equal to \(x\) using bisect_right(T, x) - 1.
Complexity
- Time Complexity: \(O(N + Q \log N)\)
- Space Complexity: \(O(N)\)
Implementation Points
If the time \(X\) is exactly equal to \(T_i\), the new velocity \(W_i\) is applied starting from that exact moment. Therefore, we use
bisect_rightfor the binary search to get the maximum index where \(T_i \leq X\).If \(X < T_1\), there is no river flow and Takahashi does not move, so the answer is always \(0\).
Since coordinates and displacement values can become very large, you must use 64-bit integer types in languages other than Python.
Source Code
import sys
from bisect import bisect_right
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
N = next(it)
V = next(it)
Q = next(it)
T = [0] * N
vel = [0] * N
for i in range(N):
t = next(it)
w = next(it)
T[i] = t
vel[i] = w + V if w > 0 else w - V
pos = [0] * N
for i in range(1, N):
pos[i] = pos[i - 1] + (T[i] - T[i - 1]) * vel[i - 1]
ans = []
for _ in range(Q):
x = next(it)
idx = bisect_right(T, x) - 1
if idx < 0:
ans.append("0")
else:
ans.append(str(pos[idx] + (x - T[idx]) * vel[idx]))
sys.stdout.write("\n".join(ans))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.