POJ - 3481 splay板子
默写splay板子
很多细节问题。。。
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 1000005
int root;
int tot=;
int ch[maxn][],par[maxn];
int cnt[maxn],size[maxn];
int val[maxn],dat[maxn];
void pushup(int x)
{
size[x]=cnt[x]+size[ch[x][]]+size[ch[x][]];
}///size信息
bool which(int x)
{
return ch[par[x]][]==x;
}
void rotate(int x)
{
int y=par[x],z=par[y];///?
int w=which(x);
int t=ch[x][w^];
par[t]=y;
ch[y][w]=t;
//par[t]=y;
par[x]=z;
ch[z][which(y)]=x;
//par[x]=z;
par[y]=x;
ch[x][w^]=y;
//par[y]=x;
pushup(y);
pushup(x);
}
void splay(int x,int goal=)
{
if(x==)return;
int y,z;
while(par[x]!=goal)///为什么不是x
{
// cout<<"PAR"<<par[x]<<endl;
y=par[x],z=par[y];
//cout<<x<<y<<z<<"YZ"<<endl;
if(z!=goal) ///???
{
if(which(x)==which(y))
{
rotate(y);
}
else rotate(x);
}
rotate(x);
// x=par[x];
// y=par[y];
} if(!goal)root=x;
}
void find(int x)
{
int cur=root;
while(ch[cur][x>val[cur]]&&x!=val[cur])
{
cur=ch[cur][x>val[cur]];
}///为什么不维护父节点了?
splay(cur);
}
int pre(int x)
{
find(x);
int cur=ch[root][];
if(!cur)return ;///直接返回0
//cout<<root<<" pre "<<ch[root][0]<<endl;
while(ch[cur][])
{
cur=ch[cur][];
}
return cur; }
int succ(int x)
{
find(x);
int cur=ch[root][];
if(!cur)return ;
//cout<<root<<" pre "<<ch[root][1]<<endl;
while(ch[cur][])
{
cur=ch[cur][];
}
return cur; }
void dfs(int cur)
{
//cout<<cur<<"cur"<<" ch "<<ch[cur][1]<<endl;
if(ch[cur][])dfs(ch[cur][]);
cout<<val[cur]<<" "<<dat[cur]<<'\n';
if(ch[cur][])dfs(ch[cur][]);
}
void del(int x)
{
//cout<<root<<"END"<<endl;
int p=pre(x),s=succ(x);///后缀为0???前驱为0???
// cout<<p<<" PS "<<s<<endl; if(!s)
{
if(!p)
{
ch[root][]=ch[root][]=root=;///只有一个点
return;
} splay(p);///没有后缀
ch[root][]=;
return;
}///???
//cout<<x<<endl;
//cout<<p<<s<<endl;
splay(p);
splay(s,p);
int de=ch[s][];
if(cnt[de]>)
{
cnt[de]--;
splay(de);
}
else
ch[s][]=;
//par[ch[root][1]]=root;
//pushup(root); }
void ins(int x,int d)
{
int cur=root,p=;///维护父节点信息
while(cur&&(x!=val[cur]))
{
p=cur;
cur=ch[cur][x>val[cur]];///>号
}
if(cur)
{
cnt[cur]++;
}
else
{
cur=++tot;
//cout<<p<<"父亲"<<endl;
if(p)ch[p][x>val[p]]=cur;///判一下p是否为0
ch[cur][]=ch[cur][]=;
par[cur]=p;///注意维护下父节点
val[cur]=x;
dat[cur]=d;
size[cur]=;
cnt[cur]=;
// dfs(root); splay(cur);
}
// cout<<"DFS"<<endl;
//dfs(root); } int mi()
{
int cur=root;
if(!cur)return ;
while(ch[cur][])
{
cur=ch[cur][];
}
int ans=dat[cur];
del(val[cur]);
return ans;
}
int ma()
{
int cur=root;
if(!cur)return ;
while(ch[cur][])
{
cur=ch[cur][];
}
int ans=dat[cur];
//cout<<ans<<endl;
del(val[cur]);
//dfs(root);
return ans;
}
int main()
{
int n,d,p;
while(scanf("%d",&n)!=EOF)
{
if(n==)break;
else if(n==)
{
scanf("%d%d",&d,&p);
ins(p,d);
}
else if(n==)
{
cout<<ma()<<'\n'; }
else if(n==)
{
cout<<mi()<<'\n';
}
}
}
POJ - 3481 splay板子的更多相关文章
- POJ 3481 splay模板
最后撸一发splay. 之前用treap撸的,现在splay也找到感觉了,果然不同凡响,两者之间差别与精妙之处各有其精髓! 真心赞一个! POJ平衡树的题目还是比较少,只能挑之前做过的捏一捏.但是收获 ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- [bzoj] 1588 营业额统计 || Splay板子题
原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- Poj 3580-SuperMemo Splay
题目:http://poj.org/problem?id=3580 SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submis ...
- POJ 3481 & HDU 1908 Double Queue (map运用)
题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...
- 个人整理的数组splay板子,指针的写的太丑了就不放了。。
splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...
- bzoj3224 splay板子
开始学习新知识:splay——tree 是个板子题,学习splay可以看博客 https://blog.csdn.net/Clove_unique/article/details/50630280 # ...
- POJ 1754 Splay
单点更新,区间最值,用来练Splay刚好. 将位置作为排序的规则,利用Splay不会改变顺序的特点,求某一段区间[l,r]的最值时,将l-1伸展到根,将r+1伸展到l-1的右子树,这时r+1的左子树就 ...
随机推荐
- UI自动化之异常与截图处理
对操作不成功时,希望能够继续执行其他操作,或者是,希望操作不成功时,能够写日志记录 目录 1.常见异常 2.截图处理 1.常见异常 1.NoSuchElementException:没有找到元素 2. ...
- GUI_FlowLayout
void setBounds(x, y, width, height) 设置窗体坐标,窗体大小 import java.awt.Frame; public class IntegerDemo { pu ...
- CENTOS6.5 编译安装MySQL5.7.14
前言 mysql5.7.14 编译安装在自定义文件路径下 下载安装包 配置安装环境 编译安装 cmake \ -DCMAKE_INSTALL_PREFIX=/data/db5714 \ -DMYSQL ...
- web框架Django一
一.django安装 # pip 安装 pip3 install Django # 克隆下载最新版本 git clone https://github.com/django/django.git # ...
- HTML5-video(播放暂停视频;打开关闭声音;进度条)
<!DOCTYPE html> <html> <head> <title>HTML5-video(播放暂停视频:打开关闭声音:进度条)</titl ...
- EA逆向生成数据库E-R图(mysql数据库-->ER图)
[1]选择 工具-->ODBC-Data-Sources [2]ODBC数据源管理器 ,点击添加 [3]选择一个mysql驱动 ,点击MySQL ODBC 5.1 Driver(其它同理), ...
- Sublime text设置快捷键让编写的HTML文件在打指定浏览器预览
作者:浪人链接:https://www.zhihu.com/question/27219231/answer/43608776来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- spark sql 操作
DSL风格语法 1.查看DataFrame中的内容 scala> df1.show +---+--------+---+ | id| name|age| +---+--------+---+ | ...
- Django基础之简介(二)
三板斧 from django.shortcuts import render,HttpResponse, redirect HttpResponse # 返回字符串 urls: urlpatte ...
- Java开发桌面程序学习(10)——css样式表使用以及Button使用
css 样式表使用 javafx中的css样式,与html的有些不一样,javafx中的css,是以-fx-background-color这种样子的,具体可以参考文档JavaFx css官方文档 简 ...