核心思想

主要的思想与treap是一样的。通过让二叉查找树同时满足堆(随机参数)的性质来防止深度过大。与普通treap不同的是非旋treap通过树的分裂与合并来实现这一点,而非旋转。

核心操作

Update

如果是要实现类似于 set<int> 的功能,可以不用这一部分。本文以 loj104 为例,我们需要在这里更新节点的 \(Size\) 信息。

void node::Update()
{
Size = Count;
if (LeftChild != NULL)
Size += LeftChild->Size;
if (RightChild != NULL)
Size += RightChild->Size;
return;
}

Split

将一个非旋treap按关键字 \(x\) 分成两个,其中一个树中关键字大小不超过 \(b\) ,另一个树中关键字严格大于 \(x\)。

std::pair<node *, node *> Split(node *Rt, int x)
{
if (Rt == NULL)
return std::pair<node *, node *>(NULL, NULL);
if (x < Rt->Value)
{
std::pair<node *, node *> Temp = Split(Rt->LeftChild, x);
Rt->LeftChild = Temp.second;
Rt->Update();
return std::pair<node *, node *>(Temp.first, Rt);
}
else
{
std::pair<node *, node *> Temp = Split(Rt->RightChild, x);
Rt->RightChild = Temp.first;
Rt->Update();
return std::pair<node *, node *>(Rt, Temp.second);
}
}

Merge

合并两棵非旋treap,其中一棵中关键字严格小于另外一棵,使得新的非旋treap同时满足二叉查找树和堆的性质。

可以递归实现,每次合并使随机的 priority 小的(或大的)在上即可。

node *Merge(node *l, node *r)
{
if (l == NULL)
return r;
if (r == NULL)
return l;
if (l->Priority <= r->Priority)
{
l->RightChild = Merge(l->RightChild, r);
l->Update();
return l;
}
else
{
r->LeftChild = Merge(l, r->LeftChild);
r->Update();
return r;
}
}

其他操作

Insert & Delete

首先查询是否需要改变节点的数量。如果不需要,直接修改 Size 即可。否则:

Insert: 将树分成 小于 \(x\) 和 大于 \(x\) 两部分,然后合并这两棵树和新节点 \(x\) 。

Delete:将树分成 小于 \(x\) 、等于 \(x\) 和大于 \(x\) 三个部分,然后删除等于 \(x\) 的部分并且合并 小于 \(x\) 的部分和 大于 \(s\) 的部分。

void Insert(int x)
{
node *T = Find(x);
if (T != NULL)
{
Update(x, 1);
return;
}
std::pair<node *, node *> Temp = Split(Root, x);
Temp.first = Merge(Temp.first, new node(x));
Root = Merge(Temp.first, Temp.second);
return;
} int Delete(int x)
{
node *T = Find(x);
if (T == NULL)
return 1;
if (T->Count > 1)
{
Update(x, -1);
return 0;
}
std::pair<node *, node *> Temp1 = Split(Root, x - 1);
std::pair<node *, node *> Temp2 = Split(Temp1.second, x);
delete Temp2.first;
Root = Merge(Temp1.first, Temp2.second);
return 0;
}

Rank & Query & Precursor & Successor

这些就和一般的二叉查找树差不多,就不赘述了。

参考程序

loj 104

#include <cstdio>
#include <algorithm> const int INF = 1e7 + 10; struct node
{
int Value, Priority, Size, Count;
node *LeftChild, *RightChild;
node()
{
Value = Priority = Size = Count = 0;
LeftChild = RightChild = NULL;
return;
}
node(int _Value)
{
Value = _Value;
Priority = rand();
Size = Count = 1;
LeftChild = RightChild = NULL;
return;
}
inline void Update();
};
struct noneRotateTreap
{
node *Root;
noneRotateTreap()
{
Root = NULL;
return;
}
inline std::pair<node *, node *> Split(node *Rt, int x);
inline node *Merge(node *x, node *y);
inline node *Find(int x);
inline void Update(int x, int State);
inline void Insert(int x);
inline int Delete(int x);
inline int Rank(int x);
inline int Query(int x);
inline int Precursor(int x);
inline int Successor(int x);
};
noneRotateTreap NoneRotateTreap; int main()
{
srand((unsigned long long)"非旋treap呀");
int n, Opt, x;
scanf("%d", &n);
for (; n; --n)
{
scanf("%d%d", &Opt, &x);
switch (Opt)
{
case 1:
NoneRotateTreap.Insert(x);
break;
case 2:
NoneRotateTreap.Delete(x);
break;
case 3:
printf("%d\n", NoneRotateTreap.Rank(x));
break;
case 4:
printf("%d\n", NoneRotateTreap.Query(x));
break;
case 5:
printf("%d\n", NoneRotateTreap.Precursor(x));
break;
case 6:
printf("%d\n", NoneRotateTreap.Successor(x));
default:
break;
}
}
return 0;
} inline void node::Update()
{
Size = Count;
if (LeftChild != NULL)
Size += LeftChild->Size;
if (RightChild != NULL)
Size += RightChild->Size;
return;
} inline std::pair<node *, node *> noneRotateTreap::Split(node *Rt, int x)
{
if (Rt == NULL)
return std::pair<node *, node *>(NULL, NULL);
if (x < Rt->Value)
{
std::pair<node *, node *> Temp = Split(Rt->LeftChild, x);
Rt->LeftChild = Temp.second;
Rt->Update();
return std::pair<node *, node *>(Temp.first, Rt);
}
else
{
std::pair<node *, node *> Temp = Split(Rt->RightChild, x);
Rt->RightChild = Temp.first;
Rt->Update();
return std::pair<node *, node *>(Rt, Temp.second);
}
} inline node *noneRotateTreap::Merge(node *l, node *r)
{
if (l == NULL)
return r;
if (r == NULL)
return l;
if (l->Priority <= r->Priority)
{
l->RightChild = Merge(l->RightChild, r);
l->Update();
return l;
}
else
{
r->LeftChild = Merge(l, r->LeftChild);
r->Update();
return r;
}
} inline node *noneRotateTreap::Find(int x)
{
node *Rt = Root;
while (Rt)
{
if (Rt->Value == x)
return Rt;
if (x < Rt->Value)
Rt = Rt->LeftChild;
else
Rt = Rt->RightChild;
}
return NULL;
} inline void noneRotateTreap::Update(int x, int State)
{
node *Rt = Root;
while (Rt)
{
Rt->Size += State;
if (Rt->Value == x)
{
Rt->Count += State;
return;
}
if (x < Rt->Value)
Rt = Rt->LeftChild;
else
Rt = Rt->RightChild;
}
return;
} inline void noneRotateTreap::Insert(int x)
{
node *T = Find(x);
if (T != NULL)
{
Update(x, 1);
return;
}
std::pair<node *, node *> Temp = Split(Root, x);
Temp.first = Merge(Temp.first, new node(x));
Root = Merge(Temp.first, Temp.second);
return;
} inline int noneRotateTreap::Delete(int x)
{
node *T = Find(x);
if (T == NULL)
return 1;
if (T->Count > 1)
{
Update(x, -1);
return 0;
}
std::pair<node *, node *> Temp1 = Split(Root, x - 1);
std::pair<node *, node *> Temp2 = Split(Temp1.second, x);
delete Temp2.first;
Root = Merge(Temp1.first, Temp2.second);
return 0;
} #define LCS (Rt->LeftChild ? Rt->LeftChild->Size : 0) inline int noneRotateTreap::Rank(int x)
{
node *Rt = Root;
int Ans = 0;
while (Rt)
{
if (Rt->Value == x)
return Ans + LCS + 1;
if (x < Rt->Value)
Rt = Rt->LeftChild;
else
Ans += LCS + Rt->Count, Rt = Rt->RightChild;
}
return Ans;
} inline int noneRotateTreap::Query(int x)
{
node *Rt = Root;
while (Rt)
{
if (LCS < x && x <= LCS + Rt->Count)
return Rt->Value;
if (x <= LCS)
Rt = Rt->LeftChild;
else
x -= LCS + Rt->Count, Rt = Rt->RightChild;
}
return 0;
} #undef LCS inline int noneRotateTreap::Precursor(int x)
{
int Ans = INF;
node *Rt = Root;
while (Rt)
{
if (Rt->Value < x)
Ans = Rt->Value, Rt = Rt->RightChild;
else
Rt = Rt->LeftChild;
}
return Ans;
} inline int noneRotateTreap::Successor(int x)
{
int Ans = -INF;
node *Rt = Root;
while (Rt)
{
if (Rt->Value > x)
Ans = Rt->Value, Rt = Rt->LeftChild;
else
Rt = Rt->RightChild;
}
return Ans;
}

非旋treap的更多相关文章

  1. [模板] 平衡树: Splay, 非旋Treap, 替罪羊树

    简介 二叉搜索树, 可以维护一个集合/序列, 同时维护节点的 \(size\), 因此可以支持 insert(v), delete(v), kth(p,k), rank(v)等操作. 另外, prev ...

  2. 非旋 treap 结构体数组版(无指针)详解,有图有真相

    非旋  $treap$ (FHQ treap)的简单入门 前置技能 建议在掌握普通 treap 以及 左偏堆(也就是可并堆)食用本blog 原理 以随机数维护平衡,使树高期望为logn级别, FHQ  ...

  3. 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)

    原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...

  4. 2827: 千山鸟飞绝 非旋treap

    国际惯例的题面:看起来很不可做的样子,我们先来整理一下题意吧.就是,维护每个点曾经拥有过的最大的两个属性值,支持把点的位置移动.我们用map对每个位置进行离散化,对每个位置建立一个平衡树.为了方便分离 ...

  5. 2081.09.22 Kuma(非旋treap)

    描述 有N张卡片,编号从0到n-1, 刚开始从0到n-1按顺序排好. 现有一个操作, 对于p. l,表示从第p张卡片之后的l张卡片拿到 最前面. 例如n=7的时候, 刚开始卡片序列为0 1 2 3 4 ...

  6. 2018.08.27 rollcall(非旋treap)

    描述 初始有一个空集,依次插入N个数Ai.有M次询问Bj,表示询问第Bj个数加入集合后的排名为j的数是多少 输入 第一行是两个整数N,M 接下来一行有N个整数,Ai 接下来一行有M个整数Bj,保证数据 ...

  7. 2018.08.06 bzoj1500: [NOI2005]维修数列(非旋treap)

    传送门 平衡树好题. 我仍然是用的fhqtreap,感觉速度还行. 维护也比线段树splay什么的写起来简单. %%%非旋treap大法好. 代码: #include<bits/stdc++.h ...

  8. 2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)

    传送门 经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay). 代码: #include<bits/stdc++. ...

  9. 2018.07.24 loj#107. 维护全序集(非旋treap)

    传送门 就是普通平衡树,可以拿来练非旋treap" role="presentation" style="position: relative;"&g ...

  10. 2018.07.06 BZOJ 1588: HNOI2002营业额统计(非旋treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

随机推荐

  1. SQL优化中的重要概念:死锁

    原文:SQL优化中的重要概念:死锁 上面几篇文章讲到 事务.锁定.阻塞,最后还有一种比较极端的情况,就是死锁,这也是锁定.阻塞的一种情况. 死锁是当两个事务分别锁定了资源,而又继续请求对方已获取的资源 ...

  2. ligerui tab 部分记载

    打开一个Tab $(".strength_box").click(function () { var id = $(this).attr("data"); va ...

  3. XML-RPC-1概述

    XML-RPC是一个远程过程调用(远端程序呼叫)(remote procedure call,RPC)的分布式计算协议,通过XML将调用函数封装,并使用HTTP协议作为传送机制.   中文名 XML- ...

  4. 4、java基础:JDK8接口新特性以及接口和抽象类区别

    在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...

  5. 提高前端开发效率的N种方法

    一.使用固定的html模板和css公共样式 事先把模板建好,每次需要用的时候直接拿来就行,不再需要为浏览器兼容问题考虑太多时间 这里我整理了一套,希望对大家有帮助:http://www.cnblogs ...

  6. 【vue开发】 父组件传值给子组件时 ,watch props 监听不到解决方案

    解决方案: watch:{ data:{ immediate:true, handler:function(){ } }} 示例:  

  7. 【Mysql MHA】CentOS7.6+Mysql8.0.16 入坑

    1.防火墙 firewall-cmd --add-port=/tcp --permanent firewall-cmd --reload 2.SELINUX sed -i 's/SELINUX=enf ...

  8. Bind Mounts and File System Mount Order

         When you use the bind option of the mount command, you must be sure that the file systems are m ...

  9. Scrapy 中常用的中间件和管道组件

    Pipeline用法 储存到MongoDB pipline.py中的代码 import pymongo class MongoPipeline(object): def __init__(self, ...

  10. Linux命令——shutdown、halt、poweroff、reboot、cal、date

    shutdown shutdown在关机的时候会通知所有用户 shutdown –r now 现在重启 shutdown now 现在关机 shutdown +5 过5分钟关机 shutdown –c ...