HDU 1908
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 was identified by a positive an integer K and, upon arriving to the bank for some s Ervices, 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 breaks the tradition by sometimes calling the serving desk with the lowest priority instead of th At with the highest priority. Thus, the system would receive the following types of request:
0 |
The system needs to stop serving |
1 KP |
ADD client K to the waiting list with priority P |
2 |
Serve the client with the highest priority and drop him or she from the waiting list |
3 |
Serve the client with the lowest priority and drop him or she 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; The last line contains the Stop-request (code 0). You may assume this when there are 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 identifierK was always less than 106, and a priority P was less than 107. The client may arrive to being served multiple times, and each time could obtain a different priority.
Output
For each request with code 2 or 3, the program have 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 and then the program prints Zero (0) to the output.
Sample Input
21 20 141 30 321 10 993220
Sample Output
02030100
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespacestd;structdata{intL,r,v,vo; intRnd;} tr[1100000];intSize,root,ans2;///the default initial value for defining global integer variables is 0;voidRturn (int&k) { intt=TR[K].L; TR[K].L=TR[T].R; TR[T].R=K; K=t;}voidLturn (int&k) { intt=TR[K].R; TR[K].R=TR[T].L; TR[T].L=K; K=t;}voidInsertint&k,intXintXO) { if(k==0) {size++;///record the number of structures already in use;k=size; TR[K].V=x; Tr[k].vo=XO; Tr[k].rnd=rand (); return; } if(x>tr[k].v) {Insert (TR[K].R,X,XO); if(tr[tr[k].r].rnd<tr[k].rnd) Lturn (k); } Else{Insert (TR[K].L,X,XO); if(tr[tr[k].l].rnd<tr[k].rnd) Rturn (k); }}voidSearch_min (int&K)///The minimum value must be on the leftmost node;{ if(tr[k].l==0) {Ans2=Tr[k].vo; K=TR[K].R; return ; } search_min (TR[K].L);}voidSearch_max (int&K)///The maximum value must be on the right-most node;{ if(tr[k].r==0) {Ans2=Tr[k].vo; K=TR[K].L; return ; } search_max (TR[K].R);}intMain () {intx,k,p; while(SCANF ("%d", &x)!=eof&&x) {ans2=0; if(x==1) {scanf ("%d%d",&k,&p); Insert (ROOT,P,K); } Else if(x==2) { if(Root) Search_max (root); printf ("%d\n", ANS2); } Else if(x==3) { if(Root) search_min (root); printf ("%d\n", ANS2); } } return 0;}
Treap Tree---Double Queue