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 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
随机推荐
- Java面试题:如何对HashMap按键值排序
Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...
- Ajax异步调用http接口后刷新页面
使用Ajax的目的就是提高页面响应速度,无需同步调用,无需整个页面刷新.这里直接在html中使用js来实现: 先获取XMLHttpRequest对象 var xmlHttp; //创建一个xmlHtt ...
- sql for 存储过程格式
sql DELIMITER && CREATE PROCEDURE test2() BEGIN DECLARE i INT; ; DO ')); ; END WHILE; END; & ...
- 73个word使用终极技巧
1.问:Word里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉.文件——页面设置——版式——页眉和页脚——首页不同 2.问:请问Word中怎样让每一章用 ...
- java图形用户界面BorderLayout布局。冲突
总结:在使用边界布局发现,把所有的按钮组件都放入了panel.但是在中部的按钮组件找不到了.发现自己重复用了组件 1.this.add(bt4,BorderLayout.North); 2.panel ...
- WebSocket :Nginx+WebSocket内部路由策略推送服务器的实现(附可生产环境应用代码)
1.项目背景 前几天写了一篇WebSocket推送的博客:WebSocket :用WebSocket实现推送你必须考虑的几个问题 支持的连接数大概几千个,具体数量依赖于tomcat能并发的线程数,但很 ...
- codeforce 980B - Marlin(构造)
Marlin time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Java-Runoob-高级教程:Java 9 新特性
ylbtech-Java-Runoob-高级教程:Java 9 新特性 1.返回顶部 1. Java 9 新特性 Java 9 发布于 2017 年 9 月 22 日,带来了很多新特性,其中最主要的变 ...
- NoSuchBeanDefinitionException: No bean named 'shiroFilter' is defined
以前运行正常的项目,过了一段时间再运行时出问题,打开链接无反应,无法访问Tomcat,空白页面. 经检查发现,在Tomcat log中有报错: NoSuchBeanDefinitionExceptio ...
- android滚条简单说明
先看一下我自己写的布局,电脑屏幕太小,只截取到了一个radiobutton. 先画一个horizontalScrollView,因为我要做水平滚动,然后我需要水平布局,就添加了一个LinearLayo ...