403200: GYM101064 G The Declaration of Independence

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. The Declaration of Independencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

In 1776, a Committee of Five was chosen to draft the Declaration of Independence of the USA, among them John Adams, Thomas Jefferson and Benjamin Franklin. From June 11 to July 5 they worked tirelessly to write such important document.

It wasn't written correctly in the first attempt, and many many changes had to be made. Since in that time there weren't such great and time-saving utilities like git and version control, they had a very simple but time consuming way to keep old versions of the document.

Suppose Thomas Jefferson wanted to add a line to a version i of the document. Instead of changing the actual document in version i, he would use a letter copying press, a machine just invented by James Watt just around that time, to copy the version i to a new sheet of paper, and then would modify this new sheet of paper. This paper would then be stored so it could be copied later. As each modification creates a new version of the document, the kth modification will create the version k of the document. You can assume the version 0 of the document is an empty piece of paper.

As everything was written in ink and the committee doesn't like to contradict itself, each modification could only add some lines to the end of the document, or erase some lines from the beginning of the document.

Your task is to simulate the making of the Declaration of Independence. Each sentence is represented as an integer number. You need to process the following queries:

  • E v x — Copy the document with version v, then add sentence x to the end of the copied document
  • D v — Copy the document with version v, then remove the first sentence of the copied document

Queries are numbered from one. The document with version 0 is empty.

Input

In the first line an integer Q, the number of queries. Each of the next Q lines has a query, in the format given in the statement.

Limits

  • 1 ≤ Q ≤ 105
  • In the ith query it is guaranteed that 0 ≤ v < i
  • For each query of type E, x will fit in a 32-bit signed integer
  • For each query of type D, it is guaranteed the document will not be empty during the removal of the first sentence
Output

For each query of type D, print the sentence removed.

ExampleInput
8
E 0 10
E 0 -10
D 2
D 1
E 2 5
D 5
D 6
D 2
Output
-10
10
-10
5
-10

Source/Category

加入题单

算法标签: