这是我综合poj1442 3481 2352的treap操作 得到treap完全版模板。(经测AC)

结构体Tree

{

  int key; //键值

  int size; //该子树总节点个数

  int pri; //其随机值

  int son[2]; //从nocow一份代码中学来的,0表示左儿子,1表示右儿子,旋转只需一个函数即可

  (int num;) //该节点在序列中的原位置,可添加

}

该Treap模板支持操作:

1. 插入值

2. 删除值

3. 找出第p小的节点的编号(找最大只需find(T[rt].size, rt);)

4. 找出值小于等于key的节点个数

(添加其他类似操作均可自己修改)

#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define MAXN 100005 using namespace std; int cnt=,rt=; //节点编号从1开始 struct Tree
{
int key, size, pri, son[]; //保证父亲的pri大于儿子的pri
void set(int x, int y, int z)
{
key=x;
pri=y;
size=z;
son[]=son[]=;
}
}T[MAXN]; void rotate(int p, int &x)
{
int y=T[x].son[!p];
T[x].size=T[x].size-T[y].size+T[T[y].son[p]].size;
T[x].son[!p]=T[y].son[p];
T[y].size=T[y].size-T[T[y].son[p]].size+T[x].size;
T[y].son[p]=x;
x=y;
} void ins(int key, int &x)
{
if(x == )
T[x = cnt++].set(key, rand(), );
else
{
T[x].size++;
int p=key < T[x].key;
ins(key, T[x].son[!p]);
if(T[x].pri < T[T[x].son[!p]].pri)
rotate(p, x);
}
} void del(int key, int &x) //删除值为key的节点
{
if(T[x].key == key)
{
if(T[x].son[] && T[x].son[])
{
int p=T[T[x].son[]].pri > T[T[x].son[]].pri;
rotate(p, x);
del(key, T[x].son[p]);
}
else
{
if(!T[x].son[])
x=T[x].son[];
else
x=T[x].son[];
}
}
else
{
T[x].size--;
int p=T[x].key > key;
del(key, T[x].son[!p]);
}
} int find(int p, int &x) //找出第p小的节点的编号
{
if(p == T[T[x].son[]].size+)
return x;
if(p > T[T[x].son[]].size+)
find(p-T[T[x].son[]].size-, T[x].son[]);
else
find(p, T[x].son[]);
} int find_NoLarger(int key, int &x) //找出值小于等于key的节点个数
{
if(x == )
return ;
if(T[x].key <= key)
return T[T[x].son[]].size++find_NoLarger(key, T[x].son[]);
else
return find_NoLarger(key, T[x].son[]);
}

treap完全版模板的更多相关文章

  1. discuz手机版模板开发

    1.触屏版模板手机路径 discuz X3触屏版模板路径:/template/default/touch/forum/discuz.htm(主页面模板) discuz X3标准版模板路径:/templ ...

  2. Treap仿set 模板

    Treap仿set 模板 蓝书232 &代码: #include <cstdio> #include <bitset> #include <iostream> ...

  3. 非旋treap (fhq treap) 指针版

    传送门 看了一圈,好像真的没什么用指针的呢.. 明明觉得指针很好看(什么??你说RE???听不见听不见) 其实我觉得用数组的话不RE直接WA调起来不是更困难嘛,毕竟通过gdb还可以知道哪里RE,WA就 ...

  4. 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...

  5. treap数组版

    然而就是将指针的地方换成int引用 就是存个代码 #include<cstdio> #include<iostream> #include<cstdlib> #in ...

  6. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

  7. [C++]竞赛模板·数据统计与IO(重定向版与非重定向版)

      /* 数据统计与IO 重定向版模板 描述:本机测试用文件数据流重定向,一旦提交到比赛就自动“删除”重定向语句 */ # define LOCAL #include<stdio.h> # ...

  8. ACM算法模板

    旧版模板下载链接: here 新版的模板目前不提供电子版,正在抽时间做一些修正以及添加一些新内容. 新模板如有需要纸质版的,可以自付打印费进行打印.购买链接:https://weidian.com/i ...

  9. Treap + 无旋转Treap 学习笔记

    普通的Treap模板 今天自己实现成功 /* * @Author: chenkexing * @Date: 2019-08-02 20:30:39 * @Last Modified by: chenk ...

随机推荐

  1. oracle中操作数据

    使用特定格式插入日期值 insert into emp values (,', to_date('1988-11-11','yyyy-mm-dd'), ); ,); 使用子查询插入数据 create ...

  2. linux-CentOS6.4下安装oracle11g详解

    参考地址:http://dengqsintyt.iteye.com/blog/1991930

  3. Android使用XML做动画UI

    在Android应用程序,使用动画效果,能带给用户更好的感觉.做动画可以通过XML或Android代码.本教程中,介绍使用XML来做动画.在这里,介绍基本的动画,如淡入,淡出,旋转等. 效果: htt ...

  4. WPFMediaKit照相功能

    最近写的一个WPF照相功能,往各位吐槽,提供优化 在WPF 设计器中添加如下代码 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.C ...

  5. C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List

    testJsonAsXMLNodeAttribute using Newtonsoft.Json; using System; using System.Collections.Generic; us ...

  6. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. Java7 新特性 switch 可以使用String

    今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...

  8. C#枚举注释实例

    public enum 枚举名称    {        /// <summary>        /// 注释描述1        /// </summary>        ...

  9. java和javascript真的有关系=。=

    相同点:1. 内存管理,两者都采用GC来对内存进行回收.因此Java与javascript的内存泄露情况十分相似. 2. 代码编译为机器码后由中间件执行:Java使用前会编译为字节码后由JVM执行,V ...

  10. linux源码阅读笔记 move_to_user_mode()解析

    在linux 0.11版本源代码中,在文件linux/include/asm/system.h中有一个宏定义  move_to_user_mode() 1 #define move_to_user_m ...