人生第一次平衡树,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. HDU - 2041 - 超级楼梯(dp)

    题意: 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? 思路: 如何到第n阶台阶,只能从n-1和n-2台阶上去,那么只需要计算到n-1阶台阶和到n-2阶台 ...

  2. [USACO06JAN] 牛的舞会 The Cow Prom

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  3. 每日命令:(10)cat

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  4. python求两个链表组成的数字的和

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  5. git 安装 使用

    git 安装--------------------------------------yum install git -y git 下载项目----------------------------- ...

  6. plot3d

    plot3d plot3D a format for structured grid data that was popularized by NASA's CFD visualization Aut ...

  7. 【Codeforces 675D】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...

  8. 小数化分数的O(log2n)解法

    具体约束: 给定一个小数x,x满足0<=x<1,且保证给定的x保留了18位小数 输出一个分数,使得分母不超过1e9,分子分母互质,且在满足这些条件的情况下最接近x 了解一下法雷数列和ste ...

  9. 学渣乱搞系列之Tarjan模板合集

    学渣乱搞系列之Tarjan模板合集 by 狂徒归来 一.求强连通子图 #include <iostream> #include <cstdio> #include <cs ...

  10. 复习1背包dp

    背包问题是对于一个有限制的容器,一般计算可以装的物品的价值最值或数量.通常每个物品都有两个属性空间和价值,有时还有数量或别的限制条件,这个因体而异. 背包大概分成3部分,下面会细述这最经典的3种题型 ...