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的左子树就 ...
随机推荐
- VGA时序及其原理(转载)
显示器扫描方式分为逐行扫描和隔行扫描:逐行扫描是扫描从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信 ...
- 【ABAP系列】SAP ABAP诠释BDC的OK CODE含义
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP诠释BDC的OK ...
- 【ABAP系列】SAP LSMW(摘自官网)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP LSMW(摘自官网) 前 ...
- OuterXml和InnerXml(2)
官方例子:https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.outerxml.aspx using System; using S ...
- 洛谷 P1462 通往奥格瑞玛的道路(二分答案,堆优化dijkstra)
传送门 解题思路 首先看题目问题,求经过的所有城市中最多的一次收取的费用的最小值是多少.一看“最大值最小”就想到了二分答案. 在读一遍题目,就是二分收取的费用,然后对于每一个二分的费用,跑一边最短路, ...
- JS补充笔记
<script> 函数: 普通函数: function func(){ } 匿名函数: setInterval("func()",5000); setInterval( ...
- Windows 中下载 Android Q 源码
1. 安装软件 1.1. 安装 git A.git官网下载:https://git-scm.com/downloads/ 安装git到如下路径 C:/Program Files/Git B.图 ...
- nginx中break和last的区别
一.环境准备 资源文件创建 mkdir -p /opt/tmp/wqy/test/aaa mkdir -p /opt/tmp/wqy/test/bbb echo "aaa" > ...
- 06Web服务
1.web开发入门 1.1 引入 软件结构分类: CS结构:客户端和服务器端 特点: 1)必须安装特点的客户端程序 2)服务器端升级,客户端同步升级 BS结构:浏览器和服务器端 特点: 1)不需要安装 ...
- [POI2006]ORK-Ploughing(贪心,枚举)
[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He can begi ...