AmazingCounters.com

POJ3481 Double Queue

传送门:http://poj.org/problem?id=3481

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
     

Description

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
1 K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

Sample Output

0
20
30
10
0

Source

 
最简单的平衡树题,写的是treap。
codes:
 1 #include<set>
 2 #include<ctime>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 const int N = 100100;
11 #define For(i,n) for(int i=1;i<=n;i++)
12 #define Rep(i,l,r) for(int i=l;i<=r;i++)
13 
14 struct treap{
15     int s[2],pri,v,key;
16     void init(int x,int y,int z){
17         v = x; key = y; pri = z;
18     }
19 }T[N];
20 
21 int op,rt,root,tot,v,key;
22 
23 void Rot(int &y,int f){
24     int x = T[y].s[!f];
25     T[y].s[!f] = T[x].s[f];
26     T[x].s[f] = y;
27     y = x;
28 }
29 
30 void Insert(int &i,int v,int key){
31     if(!i){
32         T[i=++tot].init(v,key,rand());
33         return;
34     }        
35     int f = T[i].key > key;
36     Insert(T[i].s[!f],v,key);
37     if(T[T[i].s[!f]].pri > T[i].pri) Rot(i,f);
38 }
39 
40 void Delete(int &i,int key){
41     if(T[i].key == key){
42         if(!T[i].s[0]||!T[i].s[1]){
43             if(!T[i].s[0]) i = T[i].s[1];
44             else           i = T[i].s[0];
45         }
46         else{
47             int f = T[T[i].s[0]].pri > T[T[i].s[1]].pri;
48             Rot(i,f);
49             Delete(T[i].s[f],key);
50         }
51     }else Delete(T[i].s[key>T[i].key],key);
52 }
53 
54 int read(){
55     char ch = getchar(); int num = 0 , q = 1;
56     while(ch>'9'||ch<'0'){
57         if(ch=='-') q = -1;
58         ch = getchar();
59     }
60     while(ch>='0'&&ch<='9'){
61         num = num * 10 + ch - '0';
62         ch = getchar();
63     }
64     return num * q;
65 }
66 
67 int Get(int i,int f){
68     int rt = i;
69     while(T[rt].s[f]) rt = T[rt].s[f];
70     return rt;
71 }
72 
73 int main(){
74     srand(time(NULL));
75     while(op = read(),op){
76         if(op==1){
77             v = read(); key = read();
78             Insert(root,v,key);
79         }else{
80             if(op==2) rt = Get(root,1);
81             else      rt = Get(root,0);
82             printf("%d\n",T[rt].v);
83             Delete(root,T[rt].key);
84         }
85     }
86     return 0;
87 }

 

posted @ 2014-07-31 23:54  ZJDx1998  阅读(186)  评论(0编辑  收藏  举报