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
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 每个顾客有个编号和优先级,银行每次可以添加顾客的要求进队列,且保证队列中当前任意顾客的编号和优先级都不同.银行可以执行先服务最大优先级的顾客或者先服务最小优先级的顾客操作.对于每个服务,输出顾客的编号.
按照优先级插入treap,左儿子优先级大于父亲,右儿子小于
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include <algorithm>
using namespace std;
const int maxn=1e5+;
struct Node
{
Node *ch[];
int r;
int v;
int id;
Node(int v,int id):v(v),id(id){ch[]=ch[]=NULL;r=((rand()<<)|rand()); }
inline int cmp(int x) const {
if (x==v) return -;
else return x>v?:;
}
};
void rotate(Node* &o,int d) {
Node* k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
o=k;
}
void insert(Node* &o,int x,int id) {
if(o==NULL) {
o=new Node(x,id);
}
else {
int d=x<o->v?:;
insert(o->ch[d],x,id);
if(o->ch[d]->r > o->r)
rotate(o,d^);
}
}
void remove(Node* &o,int x) {
int d=o->cmp(x);
if(d==-) {
Node* u=o;
if(o->ch[]!=NULL&&o->ch[]!=NULL) {
int d2=(o->ch[]->r>o->ch[]->r?:);
rotate(o,d2);
remove(o->ch[d2],x);
}
else {
if(o->ch[]==NULL) o=o->ch[];
else o=o->ch[];
delete u;
}
}
else {
remove(o->ch[d],x);
}
}
int find_max(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_max(o->ch[]);
}
int find_min(Node *o)
{
if (o->ch[]==NULL){
printf("%d\n",o->id);
return o->v;
}
return find_min(o->ch[]);
} int main()
{
//freopen("de.txt","r",stdin);
Node *root=NULL;
int op;
while (~scanf("%d",&op)){
if (op==) break;
if (op==){
int id,v;
scanf("%d%d",&id,&v);
//printf("ins %d %d \n",id,v);
insert(root,v,id);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_max(root);
remove(root,v);
}
else if (op==){
if (root==NULL) {
printf("0\n");
continue;
}
int v = find_min(root);
remove(root,v);
}
}
return ;
}
 

POJ 3481 Double Queue (treap模板)的更多相关文章

  1. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  2. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  3. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  4. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

  5. POJ 3481 Double Queue

    平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...

  6. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  7. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  8. BZOJ 1588: Treap 模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12171  Solved: 4352 Description ...

  9. hdu 1908 Double Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1908 Double Queue Description The new founded Balkan ...

随机推荐

  1. loj#137 最小瓶颈路 加强版

    分析 我们知道答案一定再最小生成树上 于是我们按边权从小到大建立kruskal重构树 然后每次查询lca的值即可 由于询问较多采用st表维护lca 代码 格式化代码 #include<bits/ ...

  2. 线段树2(P3373)

    传送 感谢洛谷题解让我理清了这一撮标记 这里多了一个乘法操作,乘法的优先级高于加法.我们来思考一下有关标记的问题. 首先由两种操作,可以想到要有两个标记,一个标记乘法(mul[k]),一个标记加法(a ...

  3. 20150722---点击按钮使指定的控件可见部分平移(JS)

    前段代码: <div id="out" style=" width:400px;overflow:hidden;"> <div id=&quo ...

  4. 【ABAP系列】SAP ABAP模块-取整操作中CEIL和FLOOR用法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-取整操作中 ...

  5. Android深度探索-卷1第十章心得体会

    本章介绍了传统的printk 函数调试技术和其他的调试技术,如gdb gdbserver  kgdb 对于复杂的Linux 驱动及HAL 等程序库,需要使用各种方法对其进行调试,如,设置断点.逐步跟踪 ...

  6. 23.协程的使用场景,高I/O密集型程序

    import time def one_hundred_millionA(): start_time = time.time() i = 0 for _ in range(100000000): i ...

  7. 微信支付公众号支付redirect_uri域名与后台配置不一致,错误码10003

    最近弄微信支付,微信支付公众号支付redirect_uri域名与后台配置不一致,错误码10003,最容易出错两个地方 1,appid 对应不到 2,开发者网页授权 填写域名 文章来自http://ww ...

  8. 怎么区分PV、IV、UV以及网站统计名词解释(pv、曝光、点击)

    PV(Page View)访问量,即页面访问量,每打开一次页面PV计数+1,刷新页面也是. IV(Internet Protocol)访问量指独立IP访问数,计算是以一个独立的IP在一个计算时段内访问 ...

  9. k8s常用笔记

    安装docker // 安装docker $ yum install -y docker-ce // 开机启动 && 启动服务 $ systemctl enable docker &a ...

  10. 4.ireport基本使用

    转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html 第一部分,下载与安装 Ireport官网:http:// jasperfor ...