#include<stdio.h>
#include<string.h>
const int N=1e6+;
int t[N][],data[N],id[N],fa[N],size,root;
void Rotate(int x,int w){//0:左旋 1:右旋
int y=fa[x];
t[y][!w]=t[x][w];
if(t[x][w]) fa[t[x][w]]=y;
fa[x]=fa[y];
if(fa[y]) t[fa[y]][t[fa[y]][]==y]=x;
t[x][w]=y;
fa[y]=x;
}
void Splay(int x,int y){
while(fa[x]!=y){
if(t[fa[x]][]==x)
Rotate(x,);
else
Rotate(x,);
}
if(y==) root=x;
}
void newnode(int &rt,int father,int v,int Id){
rt=++size;
t[rt][]=t[rt][]=;
fa[rt]=father;
data[rt]=v;
id[rt]=Id;
}
void Insert(int v,int Id){
int x=root;
for(;t[x][v>data[x]];x=t[x][v>data[x]]);
newnode(t[x][v>data[x]],x,v,Id);
Splay(t[x][v>data[x]],);
}
void init(){
size=root=;//root初始化为0,插入第一个点后root会被更新
t[][]=t[][]=;
}
void Delete(int x){
if(x==root){
if(!t[x][]&&!t[x][])
init();
else{
int p=t[x][]?:;
fa[t[x][p]]=;
root=t[x][p];
}
}else{//因为删除的点要么没有前驱,要么没有后继,所以可以直接删除,这里也是不超时的主要原因
int y=fa[x];
int p=(t[y][]==x);
t[y][p]=t[x][!p];
fa[t[x][!p]]=y;
Splay(y,);
}
}
void low(){
int x=root;
if(x){
for(;t[x][];x=t[x][]);
printf("%d\n",id[x]);
Delete(x);
}
else{
puts("");
}
}
void high(){
int x=root;
if(x){
for(;t[x][];x=t[x][]);
printf("%d\n",id[x]);
Delete(x);
}
else{
puts("");
}
}
int main(){
int op;
init();
while(scanf("%d",&op)!=EOF&&op){
if(op==){
int Id,p;
scanf("%d%d",&Id,&p);
Insert(p,Id);
}
else if(op==) high();
else low();
}
return ;
}

http://www.cnblogs.com/DrunBee/archive/2012/08/12/2634194.html

POJ 3481Double Queue Splay的更多相关文章

  1. Poj 2887-Big String Splay

    题目:http://poj.org/problem?id=2887       Big String Time Limit: 1000MS   Memory Limit: 131072K Total ...

  2. POJ 3934 Queue(DP)

    Queue Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the di ...

  3. codeforces 38G - Queue splay伸展树

    题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...

  4. POJ-3481 Double Queue (splay)

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

  5. 周赛 POJ 3934 Queue

    Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...

  6. 三大平衡树(Treap + Splay + SBT)总结+模板[转]

    Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...

  7. 三大平衡树(Treap + Splay + SBT)总结+模板[转]

    Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...

  8. 三大平衡树(Treap + Splay + SBT)总结+模板

    Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...

  9. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

随机推荐

  1. Python正则表达式模块(re模块)

    Python是我接触到的第一门编程语言,虽然它足够简单,但是对于当时刚刚接触编程语言的我来说还是有些难度的,于是只是了解了一些Python的基本语法,稍微深入一点的地方都没怎么了解.不过,到现在为止, ...

  2. PHP遍历目录四种方法

    学习SPL的时候,遇到了DirectoryIterator这个目录类,谢了一下遍历目录的方法.于是总结一下遍历目录的四种写法 如下: <?php /* * 方法一:利用SPL的目录类,这个很简单 ...

  3. IT外包行业与职业发展

          在IT行业,总是有一些IT外包公司的存在,凡是存在的都是合理的.当你做为IT从业人员应该尽量避免去外包公司工作 .特别是你从事软件开发工作.     先来说说缘由,一些外包公司本来是从事软 ...

  4. mysql performance_schema/information_schema授权问题

    mysql> grant all on performance_schema.* to 'testuser'@'%';ERROR 1044 (42000): Access denied for ...

  5. SharePoint 2013 新功能探索 之 列表等级设置

    一.列表等级及赞功能 ,在SharePoint 2010 中,对列表的等级设定,需要一定时间才能看到,现在可以实时同步,评分人数也能显示出来 等级分为两类 赞和星级评定   

  6. Vue自定义过滤器

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...

  7. Javascript对象赋值操作

    首先,我们还是举个例子来说明对象赋值操作的问题吧: ps: 本文默认约定log = console.log function A(){} A.prototype.x = 10; var a1 = ne ...

  8. Bit-Coin收入的一分钱

    好吧,这是我在Slush's pool上对Bit-coin收入的第一分钱. 回想起来,2013年平安夜开始到今天,将近3个月没日没夜窝在这个矿里挖矿 从最开始的集成显卡挖,买了显卡挖,加了显卡挖,使用 ...

  9. 响应式SharePoint模版页

    一张好的皮肤显然的会给你的项目加分不少.特别是大部分的项目,UI甚至可以决定成败. SharePoint在这方面一直都做得不好,曾经我有好多项目都是坐在美工旁边来一起修改样式.痛苦的经历. 不久以前, ...

  10. STL--容器适配器(queue、priority_queue、stack)

    适配器(Adaptor)是提供接口映射的模板类.适配器基于其他类来实现新的功能,成员函数可以被添加.隐藏,也可合并以得到新的功能. STL提供了三个容器适配器:queue.priority_queue ...