Treap(模板)
人生第一次平衡树,Treap板子
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime> using namespace std; struct Treenode
{
int size;
int fix;
int weight;
int key;
Treenode *left;
Treenode *right;
}; class Treaptree
{
private:
Treenode *null;
public:
Treenode *root;
Treaptree()
{
null=new Treenode;
null->key=;
null->weight=;
null->size=;
null->left=null;
null->right=null;
null->fix=;
root=null;
} void Treap_left(Treenode *now)
{
Treenode *tmp=now->right;
now->right=tmp->left;
tmp->left=now;
tmp->size=now->size;
now->size=now->left->size+now->right->size+now->weight;
now=tmp;
} void Treap_right(Treenode *now)
{
Treenode *tmp=now->left;
now->left=tmp->right;
tmp->right=now;
tmp->size=now->size;
now->size=now->left->size+now->right->size+now->weight;
now=tmp;
} void insert(Treenode *&now,int key)
{
if(now==null)
{
now=new Treenode;
now->key=key;
now->size=;
now->weight=;
now->fix=rand();
now->left=null;
now->right=null;
}
else if(key==now->key)
{
now->weight++;
}
else if(key<now->key)
{
insert(now->left,key);
if(now->left->fix<now->fix) Treap_right(now);
}
else if(key>now->key)
{
insert(now->right,key);
if(now->left->fix<now->fix) Treap_left(now);
}
now->size=now->left->size+now->right->size+now->weight;
} bool find(Treenode *now,int key)
{
if(now==null)return false;
if(key<now->key)return find(now->left,key);
else if(key>now->key)return find(now->right,key);
else return true;
} void Delete(Treenode *&now,int key)
{
if(now==null)return;
if(key<now->key)Delete(now->left,key);
else if(key>now->key)Delete(now->right,key);
else
{
if(now->weight>)now->weight--;
else
{
if(now->left==null&&now->right==null)
{
delete now;
now=null;
}
else
{
if(now->left->fix<now->right->fix)Treap_left(now);
else Treap_right(now);
Delete(now,key);
}
now->size=now->left->size+now->right->size+now->weight;
}
}
}
}; int main()
{ return ;
}
心若向阳,无言悲伤
Treap(模板)的更多相关文章
- BZOJ 1588: Treap 模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
- [luogu3369]普通平衡树(treap模板)
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- Treap 模板 poj1442&hdu4557
原理可以看hihocoder上面的讲解,很清楚,不多说了. 模板抄lrj训练指南上面的. /** Treap 实现 名次树 功能: 1.找到排名为k的元素 2.值为x的元素的名次 初始化:Node* ...
- 平衡树Treap模板与原理
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
- POJ1442-查询第K大-Treap模板题
模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...
- Treap 模板
感觉平衡树也没有以前想的那么玄乎,(其实set超好用的),非旋式Treap挺好理解,和可并堆,二叉搜索树有很大联系 推荐博客:http://memphis.is-programmer.com/post ...
- 【Treap模板详细注释】BZOJ3224-普通平衡树
模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- 非旋treap模板
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
- codevs 4543 treap 模板
type rec=record lc,rc,v,rnd,size,w,fa:longint; end; var n,root,tot,ans,opt,x,i,po:longint; tr:array[ ...
- Treap模板
平衡树总是有用的,set由于过度封装没有办法实现找比x小的元素有多少个,这就显得很不方便了,所以封装了个Treap,万一以后用的着呢- -01 #pragma warning(disable:4996 ...
随机推荐
- [Algorithm] 4. Ugly Number II
Description Ugly number is a number that only havefactors 2, 3 and 5. Design an algorithm to find th ...
- power coefficient calculation -- post processing
input: unscaled moment of one bladeoutput: power coefficient of a 3-blades wind/tidal turbine matlab ...
- 洛谷 1823 [COI2007] Patrik 音乐会的等待
[题解] 维护一个单调栈即可. 但是因为有相同身高的存在,所以要稍微考虑下相同身高的处理.因为这个卡了一下下QAQ... #include<cstdio> #include<algo ...
- 3.3.3 char 类型
char类型原本用于表示单个字符.不过,现在情况已经有所变化.如今,有些Unicode字符可以用一个char值描述,另外一些Unicode字符则需要两个 char 值. char类 ...
- Display PowerPoint slide show within a VB form or control window
The example below shows how to use VB form/control as a container application to display a PowerPoin ...
- ace & web ide & web code editor
ace & web ide & web code editor web ide https://ace.c9.io/ https://github.com/ajaxorg/ace ht ...
- NIO基础学习——缓冲区
NIO是对I/O处理的进一步抽象,包含了I/O的基础概念.我是基于网上博友的博客和Ron Hitchens写的<JAVA NIO>来学习的. NIO的三大核心内容:缓冲区,通道,选择器. ...
- Mentor.Graphics.FloTHERM.XT.2.3+Mentor.Graphics.Flowmaster.7.9.4
Mentor.Graphics.FloTHERM.XT.2.3 Mentor.Graphics.Flowmaster.7.9.4 AVL.CRUISE.V2015.0-车辆动力学仿真分析平台 AVL. ...
- 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...
- 打造atom成为golang开发神器
在我在Windows系统上开发的日子里.我使用IDE开发数年之久.比如Visual Basic IDE, Borland Delphi IDE, Visual C++ 和最后的Visual Studi ...