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界面设计拥有无 ...
随机推荐
- Oracle 排序中使用nulls first 或者nulls last 语法
-原理 Nulls first和nulls last是Oracle Order by支持的语法 如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc ...
- sublime text2 常用快捷键
1. ctrl+方向键 按单词移动 2. ctrl+shift + 方向键 按单词选取 3. ctrl + F3 查找选定的或光标所在单词 4. F3 查找特定的单词(一般查找的流程是先ctrl+ ...
- Adivisor
1.Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 2.区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcu ...
- Rdlc报表出现空白页解决方法(转)
在使用RDLC报表时,碰到这种情况:当只有一页数据时,报表确显示两页,第二页除了报表头之外数据为空.然后,当有多页数据时,最后一页为空. 这个问题很奇怪,网上有很多解决方案,以下的方法可以解决此问题. ...
- 关于js中onclick字符串传参问题
规则: 外变是“”,里面就是‘’外边是‘’,里边就是“” 示例: var a="111"; var html="<a onclick='selecthoods( ...
- 【转】【UML】使用Visual Studio 2010 Team System中的架构师工具(设计与建模)
Lab 1: 应用程序建模 实验目标 这个实验的目的是展示如何在Visual Studio 2010旗舰版中进行应用程序建模.团队中的架构师会通过建模确定应用程序是否满足客户的需求. 你可以创建不同级 ...
- High Performance Animations
http://www.html5rocks.com/zh/tutorials/speed/high-performance-animations/
- CentOS 7 安装桌面环境
首先进入要使用root权限: 使用 yum grouplist可以看现在的安装情况以及支持哪些软件包. 使用 yum groupinstall "GNOME Desktop" &q ...
- 使用ConfigurationManager类读写配置文件
使用ConfigurationManager类 读写配置文件app.config,以下为代码: view plaincopy to clipboard print? using System; usi ...
- C#创建Windows Service(Windows 服务)基础教程
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...