人生第一次平衡树,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(模板)的更多相关文章

  1. BZOJ 1588: Treap 模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12171  Solved: 4352 Description ...

  2. [luogu3369]普通平衡树(treap模板)

    解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  3. Treap 模板 poj1442&hdu4557

    原理可以看hihocoder上面的讲解,很清楚,不多说了. 模板抄lrj训练指南上面的. /** Treap 实现 名次树 功能: 1.找到排名为k的元素 2.值为x的元素的名次 初始化:Node* ...

  4. 平衡树Treap模板与原理

    这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...

  5. POJ1442-查询第K大-Treap模板题

    模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...

  6. Treap 模板

    感觉平衡树也没有以前想的那么玄乎,(其实set超好用的),非旋式Treap挺好理解,和可并堆,二叉搜索树有很大联系 推荐博客:http://memphis.is-programmer.com/post ...

  7. 【Treap模板详细注释】BZOJ3224-普通平衡树

    模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  8. 非旋treap模板

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

  9. 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[ ...

  10. Treap模板

    平衡树总是有用的,set由于过度封装没有办法实现找比x小的元素有多少个,这就显得很不方便了,所以封装了个Treap,万一以后用的着呢- -01 #pragma warning(disable:4996 ...

随机推荐

  1. 洛谷—— P1450 [HAOI2008]硬币购物

    P1450 [HAOI2008]硬币购物 硬币购物一共有$4$种硬币.面值分别为$c1,c2,c3,c4$.某人去商店买东西,去了$tot$次.每次带$di$枚$ci$硬币,买$si$的价值的东西.请 ...

  2. centos7安装:license information(license not accepted)

    安装centos7的时候明明已经选择了默认的许可证信息,不知道哪里出错了,安装到最后,就会显示license information(license not accepted)的信息.解决方法如下: ...

  3. 在vue项目中快速使用element UI

    推荐使用npm安装 1.安装:npm install element-ui -S 2.整体引入: 在你项目的main.js中写入: import ElementUI from 'element-ui' ...

  4. 洛谷 4302 BZOJ 1090 SCOI2003 字符串折叠 UVA1630 Folding(输出方案版)

    [题解] 区间DP.  设f[i][j]表示i~j的最小代价.再枚举中间点k,很容易想到转移方程为f[i][j]=min(f[i][j],f[i][k]+f[k][j]),同时如果i~k可以通过重复获 ...

  5. 洛谷 2777 [AHOI2016初中组]自行车比赛

    [题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...

  6. JavaScript及Java对JSON的相关处理

    JavaScript中JSON字符串与JSON对象的互转及JSON对象的取值: var jsonString = '{"key1":"value1"," ...

  7. c++ 上机实验题

    c++语言俺是不会啦,但是朋友考试需要,那只能勉为其难的入门下做做考试题了. 以下就是具体的题目和答案: ----------------------------------------------- ...

  8. noip模拟赛 运

    [问题背景]zhx 和妹子们玩数数游戏.[问题描述]仅包含 4 或 7 的数被称为幸运数.一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列.两个子序列被定义为不同的当且仅当其中的元 ...

  9. leetcode算法学习----155. 最小栈(MinStack )

    下面题目是LeetCode算法155题: https://leetcode.com/problems/min-stack/ 题目1:最小函数min()栈 设计一个支持 push,pop,top 操作, ...

  10. js二叉树,前序/中序/后序(最大最小值,排序)

    function Node(data,left,right) { this.left=left this.right=right this.data=data } function Btr() { t ...