Double Queue

默写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板子的更多相关文章

  1. POJ 3481 splay模板

    最后撸一发splay. 之前用treap撸的,现在splay也找到感觉了,果然不同凡响,两者之间差别与精妙之处各有其精髓! 真心赞一个! POJ平衡树的题目还是比较少,只能挑之前做过的捏一捏.但是收获 ...

  2. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  3. [bzoj] 1588 营业额统计 || Splay板子题

    原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...

  4. POJ 3481 Double Queue(Treap模板题)

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Des ...

  5. Poj 3580-SuperMemo Splay

    题目:http://poj.org/problem?id=3580   SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  6. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...

  7. 个人整理的数组splay板子,指针的写的太丑了就不放了。。

    splay的板子.. 由于被LCT榨干了..所以昨天去学了数组版的splay,现在整理一下板子.. 以BZOJ3224和3223为例题..暂时只有这些,序列的话等有时间把维修序列给弄上来!! BZOJ ...

  8. bzoj3224 splay板子

    开始学习新知识:splay——tree 是个板子题,学习splay可以看博客 https://blog.csdn.net/Clove_unique/article/details/50630280 # ...

  9. POJ 1754 Splay

    单点更新,区间最值,用来练Splay刚好. 将位置作为排序的规则,利用Splay不会改变顺序的特点,求某一段区间[l,r]的最值时,将l-1伸展到根,将r+1伸展到l-1的右子树,这时r+1的左子树就 ...

随机推荐

  1. 类File

    * File类常用的构造方法: * (1)File(String s);//由s确定File对象的文件名 * (2)File(String directory,String s);//由directo ...

  2. 没看这篇干货,别和我说你会IDEA Debug

    所谓工欲善其事必先利其器,现在idea已经成为java开发者眼中最热门最好用的IDE了.下面这篇文章将总结下idea调试的一些高级技巧. 多线程调试 直接上例子说明,比如下面这段代码 debug模式下 ...

  3. Java ——修饰符 包 Bean

    本节重点思维导图 Bean 是一个类,类中所有的属性都是私有化的,所有的属性都有相应的getter/setter方法 对于boolean类型的成员变量来说,它的getter方法是:isXxxx() 详 ...

  4. Learn Python the hard way, ex41 来自Percal 25 号星星的哥顿人

    我承认,我偷懒了,少打了大量代码(剧情),英文太差,下次可以编个中文的试试 #!/urs/bin/python #coding:utf-8 from sys import exit from rand ...

  5. 如何创建Windows虚拟机

    Windows虚拟机搭建 第1步:运行"Vmware WorkStation",看到主页面,创建新的虚拟机 第2步:新建虚拟机向导——典型(推荐) 第3步:选择光盘映像文件 第4步 ...

  6. iBatis框架之配置文件之注意点之总结

    1.配置文件sqlMap.xml中需要注意的点 比如: <?xml version="1.0" encoding="UTF-8" ?> <!D ...

  7. python中json的基本使用

    一.json的概念 json是一种通用的数据类型 一般情况下接口返回的数据类型都是json 长得像字典,形式也是k-v{ } 其实json是字符串 字符串不能用key.value来取值,所以要先转换为 ...

  8. Redis--小小总结

    1.基本定义 memcached是纯粹的key-value内存数据库,也可能不应该叫数据库,应该叫另类缓存技术: Redis是一个基于内存的高性能key-value数据库:将数据全部加载到内存中,并定 ...

  9. vue 使用jssdk分享

    背景 在vue中使用jssdk微信分享 weixin-js-sdk mint-ui需要安装npm install weixin-js-sdk mint-ui --save mixins/wechat. ...

  10. 使用absolute实现的后台布局,包括小图标定位,菜单弹出等完整版

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...