POJ 3481Double Queue Splay
#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的更多相关文章
- Poj 2887-Big String Splay
题目:http://poj.org/problem?id=2887 Big String Time Limit: 1000MS Memory Limit: 131072K Total ...
- POJ 3934 Queue(DP)
Queue Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the di ...
- codeforces 38G - Queue splay伸展树
题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...
- POJ-3481 Double Queue (splay)
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped w ...
- 周赛 POJ 3934 Queue
Description Linda is a teacher in ACM kindergarten. She is in charge of n kids. Because the dinning ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- 三大平衡树(Treap + Splay + SBT)总结+模板
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- {POJ}{动态规划}{题目列表}
动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...
随机推荐
- nginx跨域处理
http://www.nginx.cn/nginx-download nginx.conf配置 if ($request_method = ‘OPTIONS’) { add_heade ...
- bootstrap glyphicon图标无法显示
如果不注意bootstrap引入css和fonts的规范,则可能会导致bootstrap 在显示glyphicon图标时无法正常显示,显示为方框. 此时可搜索bootstrap.css中的.glyph ...
- ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制
1.前言 a.微软对ASP.NET的开发从WebForm到MVC的转变,已经正式过去5,6个年头,现在WebForm和MVC也都越来越完善,小小算来我也已经工作了将近三年,从大学的时候学习ASP.NE ...
- CRM 2013 系统设置新功能二:Entity images 图像字段
CRM2013系统默认的许多实体都已经有了图像的选择,如 右上角可以设置当前用户的头像. 客户.联系人等都可以,下面将介绍一下此项新功能: 一.实体中新建图像字段,其中系统自动命名entityinag ...
- iOS仿京东分类菜单之UICollectionView内容
在上<iOS仿京东分类菜单实例实现>已经实现了大部分主体的功能,本文是针对右边集合列表进行修改扩展,使它达到分组的效果,本文涉及到的主要是UICollectionView的知识内容,左边列 ...
- page resizing
<script type="text/javascript"> $(window).load(function () { var root; root = $(&quo ...
- 揭开智能配置上网(微信Airkiss)的神秘面纱
本文介绍微信利用Airkiss技术对wifi设备进行智能配置上网的场景,并分析其实现的原理.这里再次说明,Airkiss只是用于配置上网,其跟微信硬件平台的通信流程和接入协议规范完全没有关系.一个wi ...
- UIlable 属性详用
我的好朋友给我制定了一个新的学习方法,从新的看每个控件,去了解他的每个属性,方法来让自己对oc的认识更加充实 今天重新认识一下UILable 的属性lable的阴影设置: 阴影的偏移量是以lable中 ...
- 深入理解Android的startservice和bindservice
一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比 ...
- swift网络编程入门应用:天气预报
学习来自<小波说雨燕 第二季 网络编程(入门篇)> 工具:xcode6.4 首先在Main.storyborad中添加并设置好三个label做简单的界面显示: import UIKit / ...