Balls
Time limit1sMemory limit256 MB
Maintain a set of unit-diameter balls on a line with a wall; support insertions at free spots and repeatedly roll the leftmost ball, propagating collisions until it stops, then print all final positions.
- Level
Medium7 of 10
- Topics
- Simulation, Hash map, Union-find, Implementation
- Solved
- No attempts yet
Problem
There are balls on the number line, each of diameter , numbered from through . Ball 's leftmost point is located at position . Additionally, there is an immovable wall located at position . You have to process queries of one of the following forms:
- "1 ": Insert a new ball with its leftmost point at . If this spot is already occupied, do nothing.
- "2": Roll the leftmost ball to the right. When a rolling ball (possibly after moving distance zero) collides with a stationary ball, it stops, and the stationary ball begins rolling in the same direction. Specifically, a rolling ball stops at the position less than the position of the object it collided with. A ball stops when it reaches the wall.
Calculate the final positions of the balls.
Input
The first line contains three integers , , and : the initial number of balls, the number of queries, and the position of the wall (, ).
The second line contains integers (). It is guaranteed the positions are distinct.
The next lines describe the queries and may have one of the following forms:
- "1 " ()
- "2"
Output
Print out the final positions of the balls in increasing order on a single line, separated by spaces.