poj 3481 Double Queue (Treap)
来源:互联网 发布:sql server有证书吗 编辑:程序博客网 时间:2024/06/06 00:40
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:
0The system needs to stop serving1 K PAdd client K to the waiting list with priority P2Serve the client with the highest priority and drop him or her from the waiting list3Serve the client with the lowest priority and drop him or her from the waiting listYour 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
21 20 141 30 321 10 993220
Sample Output
02030100
Source
题意:有三种操作,1是将一个数赋给p的优先级家入队列,2是以优先级从高到低排序弹出最高的,3是优先级从低到高排弹出最低的
思路:用Treap来维护这个队列。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m,size,root,ans;struct data{ int l,r,v,size,rnd,w,id;}tr[100005];void update(int k){ tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;}void rturn(int &k){ int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k; tr[t].size=tr[k].size;update(k);k=t;}void lturn(int &k){ int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k; tr[t].size=tr[k].size;update(k);k=t;}void insert(int &k,int x,int id){ if(k==0) { size++;k=size; tr[k].size=tr[k].w=1;tr[k].v=x;tr[k].rnd=rand(); tr[k].id=id; return; } tr[k].size++; if(tr[k].v==x) tr[k].w++; else if(x>tr[k].v) { insert(tr[k].r,x,id); if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k); } else { insert(tr[k].l,x,id); if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k); }}void del(int &k,int x){ if(k==0) return; if(tr[k].v==x) { if(tr[k].w>1) { tr[k].w--;tr[k].size--; return; } if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r; else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd) rturn(k),del(k,x); else lturn(k),del(k,x); } else if(x>tr[k].v) tr[k].size--,del(tr[k].r,x); else tr[k].size--,del(tr[k].l,x);}int query_id(int k,int x){ if(k==0) return 0; if(x<=tr[tr[k].l].size) return query_id(tr[k].l,x); else if(x>tr[tr[k].l].size+tr[k].w) return query_id(tr[k].r,x-tr[tr[k].l].size-tr[k].w); else return tr[k].id;}int query_v(int k,int x){ if(k==0) return 0; if(x<=tr[tr[k].l].size) return query_v(tr[k].l,x); else if(x>tr[tr[k].l].size+tr[k].w) return query_v(tr[k].r,x-tr[tr[k].l].size-tr[k].w); else return tr[k].v;}int main(){ int n,cnt=0; for(int i=0;i<100005;i++) { tr[i].l=0; tr[i].r=0; } while(~scanf("%d",&n)&&n) { if(n==1) { int id,v; scanf("%d%d",&id,&v); insert(root,v,id); cnt++; } else if(n==2) { if(cnt==0) printf("0\n"); else { printf("%d\n",query_id(root,cnt)); del(root,query_v(root,cnt)); cnt--; } } else if(n==3) { if(cnt==0) printf("0\n"); else { printf("%d\n",query_id(root,1)); del(root,query_v(root,1)); cnt--; } } } return 0;}
- POJ 3481 Double Queue(Treap)
- poj 3481 Double Queue(treap)
- POJ 3481 Double Queue Treap
- POJ 3481 Double Queue Treap
- poj 3481 Double Queue (Treap)
- POJ 3481 Double Queue SBT || treap
- poj 3481 Double Queue
- Double Queue POJ 3481
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double Queue
- poj 3481 Double Queue
- poj 3481 Double Queue
- POJ - 3481 Double Queue
- POJ 3481 Double Queue
- POJ 3481 Double queue
- C语言实现一维数组实现输出杨辉三角
- 计算机视觉方面的数据集收藏(CV Datasets on the web)
- 168. Excel Sheet Column Title
- execve函数源码走读
- Linux系统下安装rz/sz命令及使用说明
- poj 3481 Double Queue (Treap)
- 欢迎使用CSDN-markdown编辑器
- Java的学习之路--HashMap原理
- 文章标题
- Redis初解之原理
- 兼容性获取样式+缓慢停止的运动
- MYSQL优化的一些性能与技巧
- Python:Matplotlib 画图
- 144. Binary Tree Preorder Traversal