A - ベルトコンベア / Belt Conveyor 解説 by admin
GPT 5.2 HighOverview
Each operation simply “moves all parts on a certain workbench to the one immediately to its right (or discards them if it’s the last workbench),” so we simulate the operations in order to determine the final state.
Key Observations
The two important observations are:
- Pressing the switch on workbench \(i\) only affects workbench \(i\) and (if \(i < N\)) workbench \(i+1\).
In other words, each operation is local (updating only a constant number of elements). - Parts don’t flow “one at a time” — rather, “all parts on that workbench move at once.”
Therefore, each operation only requires “extracting the count \(x\) of parts to move, adding it to the neighbor, and setting the original to 0.”
If you naively implement “moving parts one at a time,” the number of parts can be as large as \(2\times 10^{14}\), which would require an enormous number of updates per operation, resulting in TLE. However, since this problem uses bulk moves, each operation can be processed in \(O(1)\) regardless of the number of parts.
Example: With \(N=4,\ A=[3,0,2,1]\), pressing the switch on workbench 2 does nothing because \(A_2=0\).
Pressing workbench 1 moves \(3\) parts to workbench 2, resulting in \([0,3,2,1]\) (no need to move them one by one).
Algorithm
Maintain array \(A\) as “the current number of parts on each workbench” and process operations in input order.
Let \(b\) (0-indexed) be the workbench whose switch is pressed in each operation:
- If \(b = N-1\) (the last workbench):
- \(A[b] \leftarrow 0\) (discharged off the line)
- Otherwise:
- \(x \leftarrow A[b]\)
- \(A[b] \leftarrow 0\)
- \(A[b+1] \leftarrow A[b+1] + x\)
When \(A[b]=0\), we have \(x=0\) so nothing changes, which matches the problem specification (in the code, as a minor optimization, the addition is skipped when \(x\) is 0).
Complexity
- Time complexity: \(O(N+Q)\) (reading the initial array is \(O(N)\), and each operation is \(O(1)\))
- Space complexity: \(O(N)\) (array \(A\))
Implementation Notes
Since \(N,Q \le 2\times 10^5\), in Python it is safe to use
sys.stdin.buffer.read()for bulk input reading.To convert to 0-indexed, subtract \(1\) from the input \(B_j\).
Values can grow up to \(2\times 10^{14}\), but Python integers do not overflow.
Source Code
import sys
def main():
it = iter(map(int, sys.stdin.buffer.read().split()))
N = next(it)
Q = next(it)
A = [next(it) for _ in range(N)]
for _ in range(Q):
b = next(it) - 1
if b == N - 1:
A[b] = 0
else:
x = A[b]
if x:
A[b] = 0
A[b + 1] += x
sys.stdout.write(" ".join(map(str, A)))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: