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

题意:有三种操作

1:加入一个数,给出它的权值,把它放入队列中,

2:弹出权值最大的数

3:弹出权值最小的数

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 100010
using namespace std; struct splaytree
{
int size,root;
int son[maxn][],fa[maxn],key[maxn],pos[maxn];
void init()
{
size=root=;
son[][]=son[][]=;
}
void newnode(int &rt,int father,int val,int id)
{
rt=++size;
son[rt][]=son[rt][]=;
fa[rt]=father;
key[rt]=val;
pos[rt]=id;
}
void rotate(int x,int kd)
{
int y=fa[x];
son[y][!kd]=son[x][kd];
fa[son[x][kd]]=y;
fa[x]=fa[y];
if(fa[y])
{
son[fa[y]][son[fa[y]][]==y]=x;
}
son[x][kd]=y;
fa[y]=x;
}
void splay(int x,int to)
{
while(fa[x]!=to)
{
if(son[fa[x]][]==x)
{
rotate(x,);
}
else
{
rotate(x,);
}
}
if(!to)
{
root=x;
}
}
void insert(int val,int id)
{
int x;
for(x=root; son[x][key[x]<val]; x=son[x][key[x]<val]);
newnode(son[x][key[x]<val],x,val,id);
splay(son[x][key[x]<val],);
}
void Delete(int x)
{
if(x==root)
{
if(!son[x][]&&!son[x][])
{
init();
}
else
{
int t=son[x][]?:;
fa[son[x][t]]=;
root=son[x][t];
}
}
else
{
int y,t;
y=fa[x];
t=(son[y][]==x);
son[y][t]=son[x][!t];
fa[son[x][!t]]=y;
splay(y,);
}
}
void lowest()
{
int x=root;
if(x)
{
for(; son[x][]; x=son[x][]);
printf("%d\n",pos[x]);
Delete(x);
}
else
{
puts("");
}
}
void highest()
{
int x=root;
if(x)
{
for(; son[x][]; x=son[x][]);
printf("%d\n",pos[x]);
Delete(x);
}
else
{
puts("");
}
}
} tree; int main()
{
int op,val,id;
tree.init();
while(scanf("%d",&op),op)
{
if(op==)
{
scanf("%d%d",&id,&val);
tree.insert(val,id);
}
else
{
if(op==)
{
tree.highest();
}
else
{
tree.lowest();
}
}
}
return ;
}

 

POJ-3481 Double Queue (splay)的更多相关文章

  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 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

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

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

  8. POJ - 3481 splay板子

    Double Queue 默写splay板子 很多细节问题... #include<cstdio> #include<iostream> using namespace std ...

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

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

随机推荐

  1. css中伪类和伪元素的区别

    转载:http://www.cnblogs.com/ihardcoder/p/5294927.html CSS3伪类和伪元素的特性和区别   前端er们大都或多或少地接触过CSS伪类和伪元素,比如最常 ...

  2. 使用妹子UI开发的体验分享

    前阵子看到一个类似bootstrap的前端UI框架,好奇心驱使下,去琢磨了一些,最终决定网站改版用这个UI试试效果: 首页+头部: 投稿页: 现成拷贝过来的评论列表: 总结: 上手难度: (熟悉boo ...

  3. FPGA前世今生(一)

    关于FPGA,我想做硬件的或多或少都听过.从上世纪80年代算来,FPGA已走过了30多个年头.我们以FPGA两大生产厂商,两大巨头之一的INTEL(altera)公司的FPGA为例,为大家逐步介绍FP ...

  4. 微信官方jssdk Demo

    1.html部分 <!DOCTYPE html> <!-- saved from url=(0028){sh:$selfUrl} --> <html><hea ...

  5. StringBuilder与StringBuffer的区别

    相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类.St ...

  6. 委托小结及Func用法

    首先,委托是一种类型,由关键字delegate声明.确切的说,委托是一种可用于封装命名或者匿名方法的引用类型.  它类似于 C++ 中的函数指针,而且是类型安全和可靠的.       委托类型的声明与 ...

  7. Java微信公众平台开发(八)--多媒体消息回复

    转自:http://www.cuiyongzhi.com/post/46.html 之前我们在做消息回复的时候我们对回复的消息简单做了分类,前面也有讲述如何回复[普通消息类型消息],这里将讲述多媒体消 ...

  8. webService调用模式比较

  9. LINQ GroupBy 查询数据赋给select

    roles.GroupBy(a => new { a.SubjectID,a.SubjectName}).Select(p => new SelectListItem() { Value ...

  10. 201671010140. 2016-2017-2 《Java程序设计》java学习第五周

    java学习第五周心得体会        本周,是Java学习第五周,随着时间推移,随着课本内容的推进,我们接触到的程序也开始变得越来越复杂,不再是二三章那些用来练手的小程序了,这一点,在我们的例题运 ...