#P6182. [USACO10OPEN] Time Travel S
[USACO10OPEN] Time Travel S
Problem Description
Farmer John bought a time machine, which allows him to manage his herd of cows conveniently.
He now has operations (). Each operation is one of the following three types:
a x: Add a cow with ID ().s: Sell the most recently added cow (it is guaranteed that there is at least one cow at this time).t x: Go back to the state before the -th operation (it is guaranteed that the -th operation exists).
After Farmer John performs each operation, output the ID of the newest cow he currently owns. In particular, if he has no cows, output .
Input Format
The first line contains an integer .
The next lines each describe one operation.
Output Format
On the -th line, output the ID of the newest cow Farmer John owns after the -th operation. In particular, if he has no cows, output .
12
a 5
a 3
a 7
s
t 2
a 2
t 4
a 4
s
t 7
s
s
5
3
7
3
5
2
7
4
7
2
5
-1
Hint
Below is the explanation for the sample, where the owned cows are already listed in the order they were added.
| Operation No. | Operation | Owned Cows | Output |
|---|---|---|---|
| 1 | a 5 |
5 | |
| 2 | a 3 |
5,3 | 3 |
| 3 | a 7 |
5,3,7 | 7 |
| 4 | s |
5,3 | 3 |
| 5 | t 2 |
5 | |
| 6 | a 2 |
5,2 | 2 |
| 7 | t 4 |
5,3,7 | 7 |
| 8 | a 4 |
5,3,7,4 | 4 |
| 9 | s |
5,3,7 | 7 |
| 10 | t 7 |
5,2 | 2 |
| 11 | s |
5 | |
| 12 | / | -1 | |
Translated by ChatGPT 5