Treap实现山寨set
treap插入、删除、查询时间复杂度均为O(logn)
treap树中每个节点有两种权值:键值和该节点优先值
如果只看优先值,这棵树又是一个堆
treap有两种平衡方法:左旋&右旋

insert 插入
remove 删除
_find 查找
kth 返回root为根的树中第k大的元素
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std; struct Node
{
Node* ch[];
int r,v,s; //r:随机优先级,v:值,s:以本节点为根的子树的结点总数
bool operator < (const Node& rhs)
{
return (r<rhs.r);
}
int cmp(int x)
{
if (x==v) return -;
return x<v?:;
}
void maintain()
{
s=;
if (ch[]!=NULL) s+=ch[]->s;
if (ch[]!=NULL) s+=ch[]->s;
}
Node (int v):v(v) //结构体的构造函数
{
ch[]=ch[]=NULL;
r=rand();
s=;
}
}; void rotate(Node* &o,int d)
{
Node* k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
o->maintain();
k->maintain();
o=k;
} void insert(Node* &o,int x)
{
if (o==NULL)
o=new Node(x);
else
{
//int d=o->cmp(x); //不用cmp
int d=(x < o->v ? : );
insert(o->ch[d],x);
if (o->ch[d]->r > o->r)
rotate(o,d^);
}
o->maintain();
} void remove(Node* &o,int x)
{
int d=o->cmp(x);
if (d==-)
{
Node *u=o;
if ((o->ch[]!=NULL) && (o->ch[]!=NULL))
{
int d2=(o->ch[]->r > o->ch[]->r ? : );
rotate(o,d2);
remove(o->ch[d2],x);
}
else
{
if (o->ch[]==NULL)
o=o->ch[];
else
o=o->ch[];
delete u;
}
}
else
remove(o->ch[d],x);
if (o!=NULL)
o->maintain();
} int kth(Node* o,int k)
{
if ((o==NULL)||(k<=)||(k > o->s))
return ;
int s=(o->ch[]==NULL ? : o->ch[]->s);
if (k==s+)
return o->v;
else if (k<=s)
return kth(o->ch[],k);
else
return kth(o->ch[],k-s-); } int _find(Node* o,int x)
{
while (o!=NULL)
{
int d=o->cmp(x);
if (d==-)
return ;
else
o=o->ch[d];
}
return ;
} int main()
{
//freopen("in.txt","r",stdin); int n,m,opr,x;
srand(time());
cin>>n;
Node* root=new Node();
for (int i=;i<=n;i++)
{
cin>>opr>>x;
if (opr==)
{
insert(root,x);
}
else if (opr==)
{
if (!_find(root,x))
cout<<"Not Found,Operation Failed"<<endl;
else
remove(root,x);
}
}
cout<<"-----------------"<<endl;
cin>>m;
for (int i=;i<=m;i++)
{
cin>>x;
if (_find(root,x))
cout<<"Found"<<endl;
else
cout<<"Not Found"<<endl;
}
cout<<"-----------------"<<endl;
cin>>m;
for (int i=;i<=m;i++)
{
cin>>x;
int ans=kth(root,x);
cout<<x<<"th: "<<ans<<endl;
}
}
PS:发现一个画图论图形的神器:GraphViz
Treap实现山寨set的更多相关文章
- fhq treap最终模板
新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...
- 山寨Unity3D?搜狐畅游的免费开源游戏引擎Genesis-3D
在CSDN上看到了<搜狐畅游发布3D游戏引擎Genesis-3D 基于MIT协议开源>(http://www.csdn.net/article/2013-11-21/2817585-cha ...
- 让我们山寨一张Windows Azure Global的壁纸
用过国际版Azure的同学都见过一个显示了机器中主要信息的壁纸,而这个壁纸是通过Sysinternals的一款叫做bginfo来实现的,这款软件的好处是对于批量管理主(虚拟)机的管理员和使用方都很实用 ...
- [翻译+山寨]Hangfire Highlighter Tutorial
前言 Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的后台任务,而无需自行定制开发和管理基于Windows ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- 非旋treap模板
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
- 从零开始山寨Caffe·陆:IO系统(一)
你说你学过操作系统这门课?写个无Bug的生产者和消费者模型试试! ——你真的学好了操作系统这门课嘛? 在第壹章,展示过这样图: 其中,左半部分构成了新版Caffe最恼人.最庞大的IO系统. 也是历来最 ...
- NanUI for Winform 使用示例【第一集】——山寨个代码编辑器
NanUI for Winform从昨天写博客发布到现在获得了和多朋友的关注,首先感谢大家的关注和支持!请看昨天本人的博文<NanUI for Winform发布,让Winform界面设计拥有无 ...
随机推荐
- JMeter学习(十四)JMeter监控Tomcat性能
使用jmeter的tomcat监视器功能,可以通过向tomcat的status页面发送get请求,得到资源使用信息,然后转换为只直观的图像方式,这样的话,就可以监视到服务器的资源使用情况,不过需要注意 ...
- java 15- 5 List集合
需求 1:List集合存储字符串并遍历.(步骤跟Collection集合一样,只是最初创建集合对象中的集合类改变了,Collection变成List) List集合的特点: 有序(存储和取出的元素一致 ...
- jQuery的无new实例化
我只能说想法很好,设计的巧妙.看代码: var jQuery = function( selector, context ) { //执行了init函数并返回jQuery实例 return new j ...
- Bolts-Android
对Android客户端编程来说,有个明确的规则是不能在ui线程里面做耗时的操作.这样就要求网络请求.文件读写等等操作都要异步操作.而异步操作完成后,往往需要再更新ui界面.最直接的想法是回调,只要保证 ...
- crontab任务取消发送邮件
1. 方式一,每一个计划任务后加上 >/dev/null 2>&1 */5 * * * * sh /web/adm/Shell/checkin_user_count_everyda ...
- unity触发器和碰撞器
Unity中检测碰撞的方法有两种,一种是触发器一种是碰撞器,现在我来解释一下两种的区别. 触发器:有三种方法,分别是OnTriggerEnter,OnTriggerStay,OnTriggerExit ...
- int 与Integer的用法与区别
1.int是基本类型,直接存取数值,Integer是对象,用一个引用指向这个对象. 2.java中的数据类型分为基本数据类型和复杂数据类型,int是前者,Integer是后者(也就是一个类). 3.初 ...
- C#使用IrisSkin2.dll美化WinForm程序界面
一.添加控件IrisSkin2.dll. 方法: 1.右键“工具箱”.“添加选项卡”,取名“皮肤”. 2.右键“皮肤”,“选择项”弹出对话框 3.点击“浏 ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- Android开发探秘之二:导入存在的项目及其注意事项
网上看到有jsoup写的例子,就下载下来进行了研究,但是发现不会导入,于是就百度一下,发现了方法:也就是依次点击“File”->“Import”->“General”->“Exist ...