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
9月份那会工大的大神过来讲课,像是被催眠似的昏睡了大半节课,拖延症晚期患者终于下决心要开始重新学习平衡树。不看不知道,一看吓一跳。原来treap树、splay树都是平衡树==我还在假期学习计划里面分开列了==丢人丢大发了orz 而treap树就是堆树,利用大根堆或者小根堆的性质以及随机给出的优先级建的树,实在是忘得太狠了==由于本人实在不喜欢一堆指针的模板,于是乎在网上找了这个:点击打开链接 而且是三大平衡树都有,另一个是SBT 。
特别好理解的建树操作!相比于插头dp,主席树这些来说的。而且,treap树也是左右下标储存的下一组下标序号,序号套序号,序号套序号~~~
/*************** poj3481 2016.1.26 2052K 235MS C++ 2518B ***************/ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cstdlib> #include <cmath> #include <utility> #include <ctime> #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define INF 0x3f3f3f3f #define MAXN 100005 using namespace std; int cnt=1,rt=0; //节点编号从1开始 struct Tree { int key, size, pri, son[2],num; //保证父亲的pri大于儿子的pri void set(int x, int y, int z,int a) { key=x; pri=y; size=z; num=a; son[0]=son[1]=0; } }T[MAXN]; void rotate(int p, int &x) { int y=T[x].son[!p]; T[x].size=T[x].size-T[y].size+T[T[y].son[p]].size; T[x].son[!p]=T[y].son[p]; T[y].size=T[y].size-T[T[y].son[p]].size+T[x].size; T[y].son[p]=x; x=y; } void ins(int key, int &x,int a) { if(x == 0) T[x = cnt++].set(key, rand(), 1,a); else { T[x].size++; int p=key < T[x].key; ins(key, T[x].son[!p],a);///左儿子小,右儿子大 if(T[x].pri < T[T[x].son[!p]].pri)///父节点的优先级小于儿子的 则旋转 rotate(p, x); } } void del(int key, int &x) //删除值为key的节点 { if(T[x].key == key) { if(T[x].son[0] && T[x].son[1]) { int p=T[T[x].son[0]].pri > T[T[x].son[1]].pri; rotate(p, x); del(key, T[x].son[p]); } else { if(!T[x].son[0]) x=T[x].son[1]; else x=T[x].son[0]; } } else { T[x].size--; int p=T[x].key > key; del(key, T[x].son[!p]); } } int find(int p, int x) //找出第p小的节点的编号 是指下标 { if(p == T[T[x].son[0]].size+1) return x; if(p > T[T[x].son[0]].size+1) find(p-T[T[x].son[0]].size-1, T[x].son[1]); else find(p, T[x].son[0]); } int main() { //freopen("cin.txt","r",stdin); int que,k,p,tot=0; while(~scanf("%d",&que)) { if(que==0) break; if(que==1) { scanf("%d%d",&k,&p); ins(p,rt,k); tot++; } if(que==2)//del max { if(tot==0) { printf("0\n"); continue; } int pos=find(tot,rt); printf("%d\n",T[pos].num); // pos=1; del(T[pos].key,rt); tot--; } if(que==3)//del min { if(tot==0) { printf("0\n"); continue; } int pos=find(1,rt); printf("%d\n",T[pos].num); // pos=1; del(T[pos].key,rt); tot--; } } return 0; }