atcoder#ABC273E. [ABC273E] Notebook

[ABC273E] Notebook

Score : 500500 points

Problem Statement

We have an integer sequence AA and a notebook. The notebook has 10910^9 pages.

You are given QQ queries. Each query is of one of the following four kinds:

ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.

Initially, AA is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process QQ queries successively in the given order and print the last term of AA after processing each query.

The use of fast input and output methods is recommended because of potentially large input and output.

Constraints

  • 1Q5×1051 \leq Q \leq 5 \times 10^5
  • 1x,y,z1091 \leq x, y, z \leq 10^9
  • QQ, xx, yy, and zz are integers.
  • Each of the given queries is of one of the four kinds in the Problem Statement.

Input

The input is given from Standard Input in the following format:

QQ

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Output

For each i=1,2,,Qi = 1, 2, \ldots, Q, let XiX_i be the last element of AA after processing up to the ii-th query, or let Xi:=1X_i := -1 if AA is empty, and print them in the following format:

X1X_1 X2X_2 \ldots XQX_Q

11
ADD 3
SAVE 1
ADD 4
SAVE 2
LOAD 1
DELETE
DELETE
LOAD 2
SAVE 1
LOAD 3
LOAD 1
3 3 4 4 3 -1 -1 4 4 -1 4

Initially, AA is an empty sequence, so A=()A = (), and an empty sequence is recorded on each page of the notebook.

  • By the 11-st query, 33 is appended to the tail of AA, resulting in A=(3)A = (3).
  • By the 22-nd query, the sequence recorded on the 11-st page of the notebook becomes (3)(3). It remains that A=(3)A = (3).
  • By the 33-rd query, 44 is appended to the tail of AA, resulting in A=(3,4)A = (3, 4).
  • By the 44-th query, the sequence recorded on the 22-nd page of the notebook becomes (3,4)(3, 4). It remains that A=(3,4)A = (3, 4).
  • By the 55-th query, AA is replaced by (3)(3), which is recorded on the 11-st page of the notebook, resulting in A=(3)A = (3).
  • By the 66-th query, the last term of AA is removed, resulting in A=()A = ().
  • By the 77-th query, nothing happens because AA is already empty. It remains that A=()A = ().
  • By the 88-th query, AA is replaced by (3,4)(3,4), which is recorded on the 22-nd page of the notebook, resulting in A=(3,4)A = (3, 4).
  • By the 99-th query, the sequence recorded on the 11-st page of the notebook becomes (3,4)(3, 4). It remains that A=(3,4)A = (3, 4).
  • By the 1010-th query, AA is replaced by ()(), which is recorded on the 33-rd page of the notebook, resulting in A=()A = ().
  • By the 1111-th query, AA is replaced by (3,4)(3, 4), which is recorded on the 11-st page of the notebook, resulting in A=(3,4)A = (3, 4).
21
ADD 4
ADD 3
DELETE
ADD 10
LOAD 7
SAVE 5
SAVE 5
ADD 4
ADD 4
ADD 5
SAVE 5
ADD 2
DELETE
ADD 1
SAVE 5
ADD 7
ADD 8
DELETE
ADD 4
DELETE
LOAD 5
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1