人生第一次平衡树,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 ;
}

心若向阳,无言悲伤

最新文章

  1. Winform的&quot;透明&quot;
  2. 支持Android iOS,firefox(其它未测)的图片上传客户端预览、缩放、裁切。
  3. Android VideoView播放视频
  4. Puppet
  5. 用Objective-C的foundation框架解决表达式求值问题
  6. poj 1860 (Bellman_Ford判断正环)
  7. Android AndroidManifest 清单文件以及权限详解!【转】
  8. 构建安全的Xml Web Service系列之SSL篇
  9. Javascript基础知识小测试(一)
  10. spring定时器的使用
  11. Go - concurrency
  12. NPOI插入图片到excel指定单元格
  13. HTTP Basic和Digest认证介绍与计算
  14. MIME 参考手册
  15. [例子] nginx负载均衡搭建及测试
  16. Tensoflow API笔记(N) 设备指定
  17. Go的json解析:Marshal与Unmarshal
  18. shell 获取随机字符串
  19. 【轻松前端之旅】元素,标记,属性,&lt;html&gt;标签
  20. ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: Cannot run program &quot;hive&quot;: error=2, No such file or directory

热门文章

  1. UVA - 12113 Overlapping Squares(dfs+回溯)
  2. 60.通过应用层join实现用户与博客的关联
  3. java 十四周总结
  4. STM32 配置PC13~PC15
  5. 洛谷 4172 [WC2006]水管局长
  6. Spring MVC_Hello World
  7. JSOI最大值 (线段树)
  8. 将完整的Maven远程存储库下载到本地存储库(别试了,不太可取)
  9. linux 创建 bootable iso 文件
  10. RMAN RECOVERY