#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. 中国各城市PM2.5数据间的相关分析

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  2. C#在图片上添加文字代码

    创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...

  3. CSS Hack(转)

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  4. Android破解之Lic文件加密程序(首例)

    我不会写Android,这是我第一个破解Android的例子,耗时接近一天,希望大神不要见笑! 本程序为商业软件,不便发布APK程序. 不要给我发消息,我不得回,有问题,直接回帖就可以了. 准备工作 ...

  5. Atitit. Xss 漏洞的原理and应用xss木马

    Atitit. Xss 漏洞的原理and应用xss木马 1. XSS漏洞1 2. XSS的用途2 2.1. 盗取cookie2 2.2. 刷新流量 刷分3 2.3. DOS 窃取隐私”.“假冒身份”. ...

  6. 穷举法破解 zebrone1.1

    系统 : Windows xp 程序 : zebrone1.1 程序下载地址 :http://pan.baidu.com/s/1boqVcU7 要求 : 编写注册机 使用工具 :OD 可在看雪论坛中查 ...

  7. php 安全过滤函数代码

    php 安全过滤函数代码,防止用户恶意输入内容. //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_r ...

  8. openstack问题汇总

    No tenant network is available for allocation.    No tenant network is available for allocation. 这个问 ...

  9. PHP读写XML文件的四种方法

    PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介 ...

  10. 用memoization优化递归算法[JS/PHP实现]

    递归函数,通过把一个大而复杂问题简化为许多但规模较小的问题,以同一个相似模式来计算,降低了解题的难度:通过调用自身函数,极大地减少了函数代码量的优点而为开发者喜爱.但因其不断调用自身函数开辟新栈,且大 ...