Editor
Time limit3sMemory limit512 MB
Given up to 500000 edits and leveled undos, print the editor state after each operation.
- Level
Hard8 of 10
- Topics
- Stack, Segment tree, Simulation
- Solved
- No attempts yet
Problem
Byteasar is building a text editor. It has two kinds of operations: an editing operation that changes the text, and an undo operation that cancels an earlier operation. The undo of this editor works on several levels.
An editing operation is an operation of level 0. An undo operation of level (for ) cancels the most recent operation of level at most that is not already undone. So an undo of level 1 reaches only editing operations, and an undo of level 2 reaches editing operations and undo operations of level 1, but no undo operation of a higher level.
More formally, every operation already performed is either active or undone. Right after an operation is performed, is active. If is an undo operation of level , take the most recent active operation of level at most , call it , and make undone. If is itself an undo operation, the operation that had undone becomes active again. The rule keeps applying: whenever the state of an undo operation changes, the state of the operation that had undone changes too. The chain of state changes ends when it reaches an editing operation.
The current contents of the editor are described by a single integer , called the editor state, which is 0 at the beginning. Each editing operation names the editor state it produces. The current editor state is the value of the most recent active editing operation, and it is 0 when no editing operation is active.
The table below shows a run of operations and the editor state after each one. Es is an editing operation that changes the state to , and Ui is an undo operation of level .
Byteasar first performs three editing operations, so the state goes from 0 to 1, then to 2, then to 5. The two undo operations of level 1 cancel E5 and E2, which brings the state back to 1. The undo of level 3 cancels the last U1, so E2 becomes active again and the state is 2 once more. Then U2 cancels E4, the next U1 cancels the restored E2, the last U1 cancels E1, and the final operation is an editing operation that sets the state to 1.
Report the editor state after every operation.
Input
The first line contains the number of operations () performed by Byteasar.
Each of the next lines contains one integer (, ) describing an operation. If , the operation is an editing operation that changes the editor state to . If , the operation is an undo operation of level . Every undo operation in the input has an active operation of smaller level to cancel.
Output
Print lines. Line contains the editor state after the first operations of the input.