splay poj3481
三种操作
1 k p 插入一个点
2 输出p最大对应的k 删除这个点
3 输出p最小对应的k 删除这个点
splay 维护一下 一不小心就会超时
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h> using namespace std;
#define inf 1000000000
#define MAXN 1000100 int pa[MAXN],ch[MAXN][],ki[MAXN],pi[MAXN];
int root,cnt; void Rotate(int x,int kind)
{
int y=pa[x];
int z=pa[y];
ch[y][!kind]=ch[x][kind];
pa[ch[x][kind]]=y;
if(pa[y])ch[pa[y]][ch[pa[y]][]==y]=x;
pa[x]=pa[y];
ch[x][kind]=y;
pa[y]=x;
}
void splay(int x,int goal)
{
while(pa[x]!=goal)
{
if(pa[pa[x]]==goal)
{
Rotate(x,ch[pa[x]][]==x);
}
else
{
int y=pa[x];
int kind=ch[pa[y]][]==y;
if(ch[y][kind]==x)
{
Rotate(x,!kind);
Rotate(x,kind);
}
else
{
Rotate(y,kind);
Rotate(x,kind);
}
}
}
if(goal==)root=x;
} void newnode(int &x,int k,int p,int father)
{
x=++cnt;
pa[x]=father;
ch[x][]=ch[x][]=;
pi[x]=p;
ki[x]=k;
}
void Insert(int k,int p)
{
int rt=root;
int r=root;
while(rt!=)
{
r=rt;
if(pi[rt]<p)rt=ch[rt][];
else rt=ch[rt][];
}
newnode(ch[r][pi[r]<p],k,p,r);
splay(ch[r][pi[r]<p],root);
ch[][]=ch[][]=;
} int f_max(int x)
{
while(ch[x][])
x=ch[x][];
if(x==root)
root=ch[x][];
ch[pa[x]][]=ch[x][];
pa[ch[x][]]=pa[x];
return ki[x];
}
int f_min(int x)
{
while(ch[x][])
x=ch[x][];
if(x==root)
root=ch[x][];
ch[pa[x]][]=ch[x][];
pa[ch[x][]]=pa[x];
return ki[x];
}
int main()
{
root=cnt=;
int n; while(scanf("%d",&n)!=EOF&&n)
{
if(n==)
{
int k,p;
scanf("%d%d",&k,&p);
Insert(k,p);
}
else if(n==)
printf("%d\n",f_max(root));
else
printf("%d\n",f_min(root));
} return ;
}
splay poj3481的更多相关文章
- 【POJ3481】【splay】Double Queue
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- POJ-3481 Double Queue (splay)
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped w ...
- poj3481(splay tree 入门题)
平衡树都能做. // // main.cpp // splay // // Created by 陈加寿 on 16/3/25. // Copyright © 2016年 chenhuan001. A ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】
平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- 三大平衡树(Treap + Splay + SBT)总结+模板
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
随机推荐
- 线程同步以及yield()、wait()、Notify()、Notifyall()
一.线程同步 1.线程同步的目的是为了保护多个线程访问一个资源时对资源的破坏. 2.线程同步方法是通过锁来实现,每个对象都有切仅有一个锁,这个锁与一个特定的对象关联,线程一旦获取了对象锁,其他访问该对 ...
- TestLink学习二:Windows搭建TestLink环境
环境准备: 搭建php5.4.39+apache2.2+mysq5.5.28l环境 (可参考http://www.cnblogs.com/yangxia-test/p/4414161.html) (注 ...
- rstudio安装devtool出现“无法解析服务器的名称或地址”问题的解决
问题:直接使用CRAN的源出现如下错误 Warning in install.packages : InternetOpenUrl failed: '无法解析服务器的名称或地址'Warning in ...
- 包含文件函数include与require的区别
include或include_once一般用于动态包含,所谓动态包含就是根据不同条件包含不同文件 require或require_once一般用于静态包含,比如包含一个html文件的头部或者尾部 如 ...
- Castle IOC容器与Spring.NET配置之比较
我本人对于Spring.NET并不了解,本文只是通过一个简单的例子来比较一下两者配置之间的区别.在Castle IOC容器中,提出了自动装配(Auto-Wiring)的概念,即由容器自动管理组件之间的 ...
- 微信小程序 开发 微信开发者工具 快捷键
微信小程序已经跑起来了.快捷键设置找了好久没找到,完全凭感觉.图贴出来.大家看看. 我现在用的是0.10.101100的版本,后续版本更新快捷键也应该不会有什么变化. 现在貌似不能修改.如果有同学找到 ...
- Python.Django视频教程(全13集)
Python.Django视频教程(全13集)教程目录: 下载地址:http://www.fu83.cn/thread-205-1-1.html
- linux上课
1. service --status-all 2. service sshd restart 3. service --status-all | grep ssh 4. chkconfig --l ...
- 编程中的offsetof
linux和windows平台都已经定义了offsetof函数,用于取struct类型中某个变量的偏移量 在stddef.h头文件中,该宏的完整说明如下: #ifdef __cplusplus #if ...
- Middleware的艺术
定义 Middleware直译叫中间件,目前在百度上很难找到一个简单明了的含义解释,.Net下以前也比较难以看到它的身影,但在Microsoft.Owin里,多个地方都看到MiddleWare,我近来 ...