POJ-3481 Double Queue (splay)
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)的更多相关文章
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- POJ 3481 Double Queue(STL)
题意 模拟银行的排队系统 有三种操作 1-加入优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统 能够用stl中的map 由于map本身 ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
- POJ 3481 Double Queue
平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- POJ - 3481 splay板子
Double Queue 默写splay板子 很多细节问题... #include<cstdio> #include<iostream> using namespace std ...
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
随机推荐
- JMeter使用经历
JMeter是Apache大树下的又一个果实,是一个压力测试工具,因为使用方便又开源免费,也被用来做功能测试.项目里也是拿JMeter来做功能性的接口自动化测试.这里大概说明下怎么用. 首先还是先下载 ...
- 用 Python 实现文件查找
用 Python 实现文件查找(BIF实现及队列实现) (1)利用内置函数实现文件查找 1.功能:返回用户输入的文件的绝对路径 2.设计思路: (1)用户输入在哪个盘进行查找 (2)遍历此盘文件,若为 ...
- 华为公司内部培训资料_介绍RTSP的消息、信令等
https://wenku.baidu.com/view/b10415dabd64783e08122b9c.html
- 开启vmotion,实现虚拟机可以在线迁移的选项
先决条件: 1.vcenter5.5 2.vmotion服务开启 3.分布式交换机已经部署完毕 4.虚拟机在线迁移必须在web管理下,在vclient不可以
- Unity Shader入门精要读书笔记(三)Shader必须的数学基础
Xyz三维坐标系如下:左手坐标系 但是摄像机观察空间则是采用右手系: 右手法则判断叉乘的结果的方向: 正交矩阵(单位互相垂直的基矢量构成正交矩阵)具有逆与转置一致性: 列矩阵运算CBAv和行矩阵的运算 ...
- Oracle sqlldr数据加载
1 sqlldr 传统路径:sqlldr会利用sql插入为我们加载数据 直接路径加载:sqlldr不适用sql,直接格式化数据块,绕开undo,避开redo,最快的方法就是并行直接路径加载 sqlld ...
- AD9如何设置原点位置
Edit --> Origin --> Set
- 使用JAVA爬取网页图片
经过之前的HttpURLConnection还有各种流的结束,已经可以开始理解怎么下载网页上的一张图片了. 对各种流不理解的话,可以翻翻前面的随笔,讲得都比较详细.在此就不细讲了. 主要流程: 1.H ...
- MFC 打开外部EXE文件的三种方法
目前知道三种方式:WinExec,ShellExecute ,CreateProcess,别人已经总结的很好了<vc中调用其他应用程序的方法(函数) winexec,shellexecute , ...
- Python 的mock模拟测试介绍
如何不靠耐心测试 可能我们正在写一个社交软件并且想测试一下"发布到Facebook的功能",但是我们不希望每次运行测试集的时候都发布到Facebook上. Python的unitt ...