C - 川を行くボート / A Boat Going Down the River 解説 by admin
claude4.8opus-highOverview
This problem asks us to track how the position of a boat changes over time as the river current changes at specific times, and to answer multiple queries asking for the coordinate at a given time.
Analysis
First, let’s analyze Takahashi’s velocity relative to the ground during each time interval.
- From time \(0\) to before \(T_1\): There is no current, so the velocity is \(0\) (he stays in place).
- From time \(T_i\) to before \(T_{i+1}\): Since he rows at speed \(V\) in the same direction as the current of velocity \(W_i\):
- When \(W_i > 0\) (eastward), the velocity relative to the ground is \(W_i + V\).
- When \(W_i < 0\) (westward), the velocity relative to the ground is \(W_i - V\).
Thus, since the velocity is constant during each time interval, the coordinate changes as a linear function (a straight line) with respect to time within each interval.
Drawback of the Naive Approach
For each query, if we calculate the coordinate by summing up the movements interval by interval from time \(0\) to \(X_j\), it will take up to \(O(N)\) per query, resulting in \(O(NQ)\) in total. Since \(N, Q\) are up to \(2 \times 10^5\), this will exceed the time limit.
Key Idea
Calculating from scratch for each query is inefficient. Instead, we can precompute (accumulate) the coordinates at each transition time \(T_i\). Since we know the velocity during each time interval, we can compute them sequentially as:
\[\mathrm{pos}[i+1] = \mathrm{pos}[i] + v_i \times (T_{i+1} - T_i)\]
to find the coordinate \(\mathrm{pos}[i]\) at time \(T_i\) in order (where \(v_i\) is the velocity during that interval).
When given a query time \(X_j\), we find the interval to which \(X_j\) belongs (the largest index \(idx\) such that \(T_{idx} \le X_j\)). The answer will then be the coordinate at the start of that interval, \(\mathrm{pos}[idx]\), plus the movement during the remaining time \(X_j - T_{idx}\) at velocity \(v_{idx}\):
\[\mathrm{ans} = \mathrm{pos}[idx] + v_{idx} \times (X_j - T_{idx})\]
Finding the correct interval can be done in \(O(\log N)\) using binary search, as \(T\) is sorted in ascending order.
Algorithm
- For each \(i\), calculate the velocity relative to the ground during that interval: \(v_i = W_i + (W_i > 0\ ?\ V : -V)\).
- Precompute the coordinates \(\mathrm{pos}[i]\) (coordinate at time \(T_i\)) cumulatively, starting with \(\mathrm{pos}[0] = 0\).
- For each query \(X_j\):
- If \(X_j < T_1\), there is no current yet, so the coordinate is \(0\).
- Otherwise, find the largest \(idx\) satisfying \(T_{idx} \le X_j\) using binary search, and output \(\mathrm{pos}[idx] + v_{idx} \times (X_j - T_{idx})\).
Example: Suppose \(V=2\), \(T=(3, 7)\), and \(W=(5, -4)\). - Interval \([3,7)\): The velocity is \(5+2=7\). \(\mathrm{pos}[0]=0\). - Interval \([7,\infty)\): The velocity is \(-4-2=-6\). \(\mathrm{pos}[1] = 0 + 7 \times (7-3) = 28\).
For a query \(X=10\), we get \(idx=1\), and the coordinate is \(28 + (-6) \times (10-7) = 28 - 18 = 10\).
Complexity
- Time Complexity: \(O(N + Q \log N)\)
- \(O(N)\) for precomputation, and \(O(\log N)\) for the binary search in each query.
- Space Complexity: \(O(N)\)
- To store the arrays for times, velocities, and cumulative coordinates.
Implementation Details
Beware of integer overflow: The maximum velocity is \(|W_i| + V \approx 2 \times 10^9\), and the time interval can also be up to \(10^9\). Thus, their product requires a 64-bit integer. In C++, use
long long(the problem guarantees that the answer fits within a signed 64-bit integer).How to use binary search: Since
upper_bound(T, X)points to the first element strictly greater than \(X\), subtracting \(1\) from its iterator/index gives the index of “the largest \(T_i\) less than or equal to \(X\)”.Do not forget to handle the case \(X_j < T_1\) (when the current has not started yet) by returning \(0\).
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
long long V;
int Q;
scanf("%d %lld %d", &N, &V, &Q);
vector<long long> T(N), W(N);
for(int i=0;i<N;i++){
scanf("%lld %lld", &T[i], &W[i]);
}
// prefix positions at time T[i]
// pos at time T[0] = 0 (since before T[0] no flow)
// velocity in [T[i], T[i+1]) is W[i]+ (W[i]>0? V : -V)
// pos[i] = position at time T[i]
vector<long long> pos(N);
pos[0]=0;
for(int i=0;i<N-1;i++){
long long v = W[i] + (W[i]>0 ? V : -V);
pos[i+1] = pos[i] + v*(T[i+1]-T[i]);
}
// for query time X
for(int j=0;j<Q;j++){
long long X;
scanf("%lld", &X);
if(X < T[0]){
printf("0\n");
continue;
}
// find largest i with T[i] <= X
int idx = (int)(upper_bound(T.begin(), T.end(), X) - T.begin()) - 1;
long long v = W[idx] + (W[idx]>0 ? V : -V);
long long ans = pos[idx] + v*(X - T[idx]);
printf("%lld\n", ans);
}
return 0;
}
This editorial was generated by claude4.8opus-high.