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 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
随机推荐
- Maven项目合并
当多个项目之间有关联.依赖jar包有重复时,可以考虑进行合并.举例,我一开始有一个test-pilling项目,pom文件如下: <project xmlns="http://mave ...
- 使用Inno SetUp脚本打包Winform程序
在开发桌面程序时,往往需要用到打包工具将程序打包为exe可执行文件. 之前在项目中用了下 InstallShield Limited Edition for Visual Studio 2015,它 ...
- C++ 中的 new/delete 和 new[]/delete[]
在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...
- .com .cn .org .edu等域名的意义
在开发的时候遇到了.org的域名,后来就去查了一下,原来这种域名是非盈利组织或者协会的标志 比如: https://getcomposer.org/ https://packagist.org/ ht ...
- java代码逆序输出数字
总结:请告诉我更好的方法~~~总觉得不好. package com.badu; import java.util.Scanner; //逆序输出数字: // class fa { public sta ...
- Window中的内存地址(小知识)
现在的编辑器大部分工作都是内存管理托管型,所以很少直接对Window的内存地址直接管理了. Window中的内存地址主要是以16进制数字体现的,当操作系统为32位时,那么每个内存地址为2的32次方,也 ...
- Mybites和hibernate的优缺点和区别2
Hibernate与MyBatis都可以是通过SessionFactoryBuider由XML配置文件生成SessionFactory,然后由SessionFactory 生成Session,最后由S ...
- CSS——优先级
转自:http://www.planabc.net/2008/05/06/css_specificity/ CSS2.1 中规定了关于 CSS 规则 Specificity(特异性)的计算方式,用一个 ...
- 页面布局整理(基于scss)
页面开发步骤: 1.全局reset.设置基础背景色.设置基础字体样式 2.全局布局页面结构,meta 标签引入 3.按钮等相同的样式,用scss提前写好一份公用,渐变等 border-radius b ...
- Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间
遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...