#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 NN operations (1N8×1041 \leq N \leq 8 \times 10^4). Each operation is one of the following three types:

  1. a x: Add a cow with ID xx (1x1061 \leq x \leq 10^6).
  2. s: Sell the most recently added cow (it is guaranteed that there is at least one cow at this time).
  3. t x: Go back to the state before the xx-th operation (it is guaranteed that the xx-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 1-1.

Input Format

The first line contains an integer NN.

The next NN lines each describe one operation.

Output Format

On the ii-th line, output the ID of the newest cow Farmer John owns after the ii-th operation. In particular, if he has no cows, output 1-1.

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