poj 3481 treap树堆模板
2017-04-18 13:46
323 查看
每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cstdlib> #include <cmath> #include <utility> #include <vector> #include <queue> #include <map> #include <set> #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; struct Tree{ int key, num, pri, son[2]; void set(int _x, int _y, int _z){ key=_x; num=_y; pri=_z; son[0]=son[1]=0; } }T[MAXN]; void rotate(int &x, int p) //1 右旋 0 左旋 { int y=T[x].son[!p]; T[x].son[!p]=T[y].son[p]; T[y].son[p]=x; x=y; } void ins(int &x, int key, int num) { if(x == 0) T[x=cnt++].set(key, num, rand()); else { int p=key < T[x].key; ins(T[x].son[!p], key, num); if(T[x].pri < T[T[x].son[!p]].pri) rotate(x, p); } } void del(int &x, int 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(x, p); del(T[x].son[p], key); } else { if(!T[x].son[0]) x=T[x].son[1]; else x=T[x].son[0]; } } else { int p=T[x].key > key; del(T[x].son[!p], key); } } int get(int x, int p) { while(T[x].son[p]) x=T[x].son[p]; return x; } int main () { srand(time(NULL)); int p, key, num, x; while(scanf("%d", &p) && p) { switch (p) { case 1: scanf("%d%d", &num, &key); ins(rt, key, num); break; case 2: x=get(rt, 1); if(x) { printf f0f5 ("%d\n",T[x].num); del(rt, T[x].key); } else printf("0\n"); break; case 3: x=get(rt, 0); if(x) { printf("%d\n",T[x].num); del(rt, T[x].key); } else printf("0\n"); } } return 0; }
相关文章推荐
- POJ 1442 平衡树Treap模板
- POJ 1442 Treap模板
- POJ_P1442 Black Box(Treap模板题+动态第k小)
- POJ-3481 Double Queue,Treap树和set花式水过!
- POJ 1442 Treap模板
- poj 3481 Treap
- poj 3481 treap 练习
- Black Box+POJ+treap树模板
- POJ 3481 Double Queue Treap
- POJ 3481 Double Queue(Treap)
- POJ 3481 Double Queue(treap)
- AVL树(模板题)—— POJ 3481 Double Queue
- Double Queue - POJ 3481 - 树堆Treap的数组实现
- POJ 3481 treap
- poj 1442 Black Boxes(treap)求第k大数的treap模板
- poj 3481 Double Queue(treap)
- POJ-3481 Double Queue (平衡树 入门题 splay模板题)
- Treap模板——POJ1442——Black Box
- POJ 1442 Black Box【treap模板】
- POJ 3481 Double Queue SBT || treap