F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

YJJ¡¯s Stack

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 43    Accepted Submission(s): 16


Problem Description
As it¡¯s known to all, there¡¯s a cute girl named YJJ living in Sand River. One day when he was learning stack, he suddenly thought that it would be more interesting to add timestamp to each stack command, and wanted to delete the element(even it¡¯s not the top one) as he wanted.

So he invented a stack called "YJJ's stack", which supports the following four command:

¡ñ push v t ¨C push $v$ into YJJ¡¯s stack, whose timestamp is $t$ ;
¡ñ delete v t ¨C delete the uppermost element from the stack which is equal to $v$ , whose timestamp is $t$ ;
¡ñ pop t ¨C pop the top element from YJJ¡¯s stack, whose timestamp is $t$ ;
¡ñ query t ¨C execute a query operation which is described below, whose timestamp is $t$ ;

There is an empty set of commands initially. When an command except query t arrives, we put the command into the set. When an command query t arrives, we process as follows:

1. Clear YJJ's stack .
2. Execute the command currently in the set whose timestamp is less than t .
3. The executing order of command is ordered by their timestamp in ascending order.
4. Print the top element in the stack.

Records of command are in arriving order, you are required to execute according to above rules. Another three constraints are made to simplify the problem :

1. All the pop and delete command are valid. In another word, if you do the process correctly, pop command will not be excuted on an empty YJJ¡¯s stack and delete v command will not be excuted on an YJJ¡¯s stack without v .
2. All timestamps are different.
3. The number of pop command is no more than $5$.
 

Input
The first line of the input contains an integer $T$ $(1¡ÜT¡Ü10)$,which is the number of test cases.

In each case, the first line of the input contains an integer $N$ $(1¡ÜN¡Ü1.2\times 10^5)$. The following $N$ lines each contain an command in one of the following four formats: push v t , delete v t , pop t , query t , where $1¡Üv¡Ü5, 0¡Üt¡Ü10^9$, and $v, t$ are integers. The command are given in the order in which they arrive.
 

Output
For each query command, output the answer in a line. If the stack is empty, output $-1$ instead.
 

Sample Input
2 13 push 2 10 push 1 20 query 21 delete 2 30 query 31 push 3 40 query 53 pop 60 query 62 push 2 70 query 41 push 1 50 query 91 9 push 2 10 push 1 20 query 11 query 21 delete 2 30 query 31 query 12 pop 40 query 41
 

Sample Output
1 1 3 1 3 2 2 1 1 2 -1
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-25 02:29:33, Gzip enabled