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个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...
随机推荐
- php正规则表达式学习笔记(几个常用函数的区别)
preg_mache()函数和 preg_mache_all()函数的区别: preg_mache()只会匹配规则中的字符一次, preg_mache_all()会匹配符合条件的所有字符! 例子对比: ...
- Android开发中的问题及相应解决(持续更新)
最近博客写的少了,以后还得经常更新才行. ------------------------------------------------------------ 1.特定业务需求下try cath ...
- vmware linux top si高以及网卡队列、软负载相关优化
今日,测试公司自行开发的一rpc中间件,期间发现top si的比例很高,且几乎只有一个cpu是繁忙的,其他均基本为0. 经查,si主要是系统软中断,最后确定是网卡导致的系统中断.于是,往上搜了下资料, ...
- spring扫描classpath下特定package,并加载具有特定注解的接口
spring扫描classpath下特定package,并加载具有特定注解的接口. 在框架平台的开发中,通常有很多的情况通过spring配置方式来实现某些功能会使得框架平台难以使用和扩展,我们通常的做 ...
- JSON-RPC轻量级远程调用协议介绍及使用
这个项目能够帮助开发人员利用Java编程语言轻松实现JSON-RPC远程调用.jsonrpc4j使用Jackson类库实现Java对象与JSON对象之间的相互转换.jsonrpc4j包含一个JSON- ...
- 怎样高效地去判断Array中是否包含某个值?
问题 怎样去判断Array(无序)中是否包含某个值呢? 这是一个在Java中经常被问到的问题.它也是Stack Overflow上投票前几的一个问题.下面将展示投票前几的几个回答,这些回答使用不同的方 ...
- [Azure] Notification Hubs注册模式
[Azure] Notification Hubs注册模式 关于Azure Notification Hubs的注册模式,可以参考下列连结的文件内容. Notification Hubs Featur ...
- 通过原生js添加div和css
function createStyle(){ return"*{padding:0;margin:0;border:0}.loading{width:640px;height:1024px ...
- 关于DOM操作的性能优化
最著名的有关用js操作dom的观点是:js和dom是独立的小岛,用桥实现两者的联系,但桥很窄,要过路费,所以我们要尽最大可能减少过桥的次数.下面代码演示了用js操作dom的innerHTML,且一下修 ...
- WCF服务部署到IIS7.5
下面介绍如何把WCF服务部署到IIS: 为WCF服务创建.svc文件 我们知道,每一个ASP.NET Web服务都具有一个.asmx文本文件,客户端通过访问.asmx文件实现对相应Web服务的调用.与 ...