#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. mysqli连接数据库常见函数

    mysqli_free_result() 返回最后一次查询中使用的自动生成 id,如果是多表插入,返回的是第一个被插入的id. mysqli_query($con,"INSERT INTO ...

  2. ASP.NET WebAPI 14 仿写Filter管道

    WebAPI中有设计了几种管道(Channel),大概如下:HttpMessageHandler,ActionFilter管道,ExceptionFilter管道.在三种管道中HttpMessageH ...

  3. windbg定位死锁

    操作系统对死锁的描述如下: 所谓死锁:是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去. 那么为什么会产生死锁呢? 1.因为系统资源不足. ...

  4. 【GOF23设计模式】中介者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_中介者模式.同事协作类.内部类实现 package com.test.mediator; /** * 同事类的接口 */ ...

  5. 为阿里云存储开发的PHP PEAR 包:Services_Aliyun_OSS

    阿里云开放存储服务 OSS:用于存储图片.apk等静态资源,使用阿里云带宽,不占用开发者服务器带宽. 阿里云官方PHP SDK: http://aliyun.com/product/oss/#help ...

  6. docker 使用

    https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-docker-application http ...

  7. Office2016体验

    Microsoft又迎来了更新的季节.对于我来说,win10就算了,不太稳定,特别是遇到一些专业的程序,因为很多行业软件开发.测试环境都是winxp或win7等:VS2015也安上了,但还没用,一直用 ...

  8. CSS重置样式表

    网页设计,让人最头疼的莫过于让页面兼容各大浏览器,准确些是兼容它们“默认”的CSS样式表.第一种方式 * {margin:0px; padding:0px;} 这行代码虽然简单,但却让网页解析太慢.于 ...

  9. 简明 Vim 练级攻略(转)

    原文:http://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会 ...

  10. IOS Quartz2D 通过UIColor生成图片

    普通生成 示例代码: //这里实现普通生成图片的方法 - (void)drawRect:(CGRect)rect { CGRect cxRect = CGRectMake(, , , ); UIGra ...