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. Java 构造器或构造方法

    构造方法的定义 构造方法也叫构造器或者构造函数 构造方法与类名相同,没有返回值,连void都不能写 构造方法可以重载(重载:方法名称相同,参数列表不同) 如果一个类中没有构造方法,那么编译器会为类加上 ...

  2. Jave 文件介绍

    Java程序的基本组成单元是类,有class声明,类体中包括属性和方法. 一个Java文件中可以有多个class声明,但由public修饰的类只能有一个,并且类名作为该文件的名称. 每一个应用程序都必 ...

  3. flask 之 mongodb

    查看mongod 是否启动,启动了会显示进程ID和程序名 pgrep mongod -l 查找mongod的位置whereis mongod 或locate mongod 启动mongodmongod ...

  4. delphi 2010安装unidac

    UniDAC是一个功能强大的非可视化跨数据库的数据访问组件,可用于Delphi,Delphi for .NET,C++Builder,and Lazarus (Free Pascal).它提供了对流行 ...

  5. CCPC2018-湖南全国邀请赛 G String Transformation

    G.String Transformation 题目描述 Bobo has a string S = s1 s2...sn consists of letter a , b and c . He ca ...

  6. Unity3D 4.3在Windows下打包iOS资源

    想当年,想弄iOS的版本必须弄台mac机器,虽然一开始要弄iOS的时候觉得在mac上开发感觉挺高富帅的,但是做多了之后就觉得在mac上开发各种不方便.现在好了,Unity3D 4.3开始支持在Wind ...

  7. Py修行路 python基础 (二十一)logging日志模块 json序列化 正则表达式(re)

    一.日志模块 两种配置方式:1.config函数 2.logger #1.config函数 不能输出到屏幕 #2.logger对象 (获取别人的信息,需要两个数据流:文件流和屏幕流需要将数据从两个数据 ...

  8. 关于windows的jdk

    扯淡工作从web开发转向build automation,不知不觉已经一个半月了,各种学习熟悉build相关的流程和知识,比如ant, maven, jenkins 等CI技能.因为这一个半月学的东西 ...

  9. Texture Filter

    [Texture Filter] 我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture上的采样中心texel),大小也不一 ...

  10. CSV Format

    [CSV Format] The comma separated values format (CSV) has been used for exchanging and converting dat ...