B - 数直線を進む / Walking on the Number Line Editorial by admin Editorial for printing
swe-2-highExplanation
We simulate the moves one by one. Initialize a variable pos to \(0\); for each move, add \(X_i\) if \(D_i\) is R and subtract \(X_i\) if it is L, then print the current pos.
The coordinate right after each move is just a running (prefix) sum, so repeating “add/subtract then print” gives exactly what is asked. With \(N \leq 100\) an int suffices, but the program uses long long for safety.
Correctness
After the \(i\)-th move the position is \(\sum_{j=1}^{i} d_j X_j\) where \(d_j = +1\) for R and \(-1\) for L. The loop processes each move exactly once and keeps pos equal to this prefix sum, so the \(i\)-th printed line matches the required value.
Complexity
Time complexity is \(O(N)\); besides the output, only the current position and one input record are stored, so extra space is \(O(1)\).
posted:
last update: