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. 1204C Anna, Svyatoslav and Maps

    题目大意 给你一个有向图和一个路径 让你在给定路径中选出尽量少的点使得新路径的最短路长度和原路径相等 给定路径相邻两点间距离为1 分析 先floyd求出两点间最短路 之后每次对于点i找到所有跟它的最短 ...

  2. 牛客提高D2t1 ACGT

    分析 用map维护一下每种字符串当前有几个即可 代码 #include<iostream> #include<cstdio> #include<cstring> # ...

  3. JS-Promise(使异步操作同步执行)

    单个异步操作同步 <div id="box"></div> <script> var box = document.querySelector( ...

  4. spring4.1.8扩展实战之八:Import注解

    spring4.1.8扩展实战之八:Import注解 2018年09月10日 12:53:57 博陵精骑 阅读数:441更多 所属专栏: spring4源码分析与实战    版权声明:欢迎转载,请注明 ...

  5. jmeter之集合点的使用

    通过jmeter并不能1秒立即达到某一并发,这时候,可以通过集合点来实现,达到某一并发时,然后再一起执行某一动作,仅作用于第一次动作的时候 目录 1.集合点元件 2.简单的概念介绍 1.集合点元件 集 ...

  6. 通过VLC的ActiveX进行二次开发,实现一个多媒体播放器 2011-04-10 00:57:23

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25498312&id=218294  通过VLC的ActiveX进行二 ...

  7. 配置最漂亮的PyCharm界面,Python程序员必备!

    高逼格超美的IDE界面,是每个程序员的梦想! 随着人工智能/机器学习的兴起,Python作为一门“漂亮的语言”,再次获得广大程序员的关注.而JetBrains出品的PyCharm无疑是最好用的Pyth ...

  8. 爬虫(十一)—— 请求库(三)pypeteer请求库

    曾经使用模拟浏览器操作(selenium + webdriver)来写爬虫,但是稍微有点反爬的网站都会对selenium和webdriver进行识别,网站只需要在前端js添加一下判断脚本,很容易就可以 ...

  9. PHP学习:set_time_limit,max_execution_time,sleep

    set_time_limit 设置脚本最大允许执行时间,可以在php脚本中使用, 参数为秒,如果为0,表示无时间限制: set_time_limit(seconds); max_execution_t ...

  10. pandas分组聚合案例

    美国2012年总统候选人政治献金数据分析 导入包 import numpy as np import pandas as pd from pandas import Series,DataFrame ...