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的更多相关文章

  1. fhq treap最终模板

    新学习了fhq treap,厉害了 先贴个神犇的版, from memphis /* Treap[Merge,Split] by Memphis */ #include<cstdio> # ...

  2. 山寨Unity3D?搜狐畅游的免费开源游戏引擎Genesis-3D

    在CSDN上看到了<搜狐畅游发布3D游戏引擎Genesis-3D 基于MIT协议开源>(http://www.csdn.net/article/2013-11-21/2817585-cha ...

  3. 让我们山寨一张Windows Azure Global的壁纸

    用过国际版Azure的同学都见过一个显示了机器中主要信息的壁纸,而这个壁纸是通过Sysinternals的一款叫做bginfo来实现的,这款软件的好处是对于批量管理主(虚拟)机的管理员和使用方都很实用 ...

  4. [翻译+山寨]Hangfire Highlighter Tutorial

    前言 Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的后台任务,而无需自行定制开发和管理基于Windows ...

  5. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  6. BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]

    1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1318  Solved: 498[Submit][ ...

  7. 非旋treap模板

    bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...

  8. 从零开始山寨Caffe·陆:IO系统(一)

    你说你学过操作系统这门课?写个无Bug的生产者和消费者模型试试! ——你真的学好了操作系统这门课嘛? 在第壹章,展示过这样图: 其中,左半部分构成了新版Caffe最恼人.最庞大的IO系统. 也是历来最 ...

  9. NanUI for Winform 使用示例【第一集】——山寨个代码编辑器

    NanUI for Winform从昨天写博客发布到现在获得了和多朋友的关注,首先感谢大家的关注和支持!请看昨天本人的博文<NanUI for Winform发布,让Winform界面设计拥有无 ...

随机推荐

  1. http协议详解<一>

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://7826443.blog.51cto.com/7816443/1729227 写在 ...

  2. 详解反射->Type.System

    反射先了解 一:system.Type 获取基本信息: Type.Name   //类名 Type.FullName //完整路径 Type.Namespace //空间名 public class ...

  3. 【WPF】Winform调用WPF窗体注意事项

     1.需要添加一些引用 2.调用处使用如下方法进行调用 Window win= new Window(); ElementHost.EnableModelessKeyboardInterop(win) ...

  4. 记录使用gogs,drone搭建自动部署测试环境

    使用gogs,drone,docker搭建自动部署测试环境 Gogs是一个使用go语言开发的自助git服务,支持所有平台 Docker是使用go开发的开源容器引擎 Drone是一个基于容器技术的持续集 ...

  5. wireshark排查打印机问题

    抓包工具排除故障 前言:上网慢,可能是内网堵了.装上wireshark,可抓到广播包,多播包,以及发给自己的包.如果想抓lan内其他人之间的通信包,那就要在sw上做端口镜像. 背景 调试打印机的人发现 ...

  6. MTK 平台上如何给 camera 添加一种 preview size

    1,首先检查该项目所使用的是哪一颗sensor, 就以OV2659 为例OV2659 是一颗2M 的摄像头,Sensor 吐出的数据分辨率能达到 1600*1200,肯定是支持 1280*720 的分 ...

  7. android ContentResolver详解

    查询出来的cursor的初始位置是指向第一条记录的前一个位置的cursor.moveToFirst()指向查询结果的第一个位置.一般通过判断cursor.moveToFirst()的值为true或fa ...

  8. pandas groupby

    pandas.DataFrame.groupby DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, gr ...

  9. 20135231 —— Linux 基础入门学习

    20135231 何佳 学习计时:共12小时 读书:5 代码:2 作业:2 博客:3 一.学习目标 1. 能够独立安装Linux操作系统 2. 能够熟练使用Linux系统的基本命令 3. 熟练使用Li ...

  10. 20155226田皓宇关于优秀技能经验以及c语言学习感悟和对JAVA的展望

    读老师文章后关于一项优秀技能的经验有感 1.首先我自我剖析认为,我是没有哪一个方面能做到强于身边90%的人的,我只能说有些方面略强于身边的人.比如唱歌.办公软件的应用(word.excel)等.但我不 ...