模板 - 无旋Treap
一般而言作为一棵平衡树只需要插入,删除,值求排名,排名求值,前驱,后继,六个接口。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
const int MAXN = 100000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
void Init() {
tot = root = 0;
}
void PushUp(int p) {
siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
}
void SplitValue(int p, int v, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
if(v < val[p]) {
y = p;
SplitValue(ls(p), v, x, ls(p));
PushUp(y);
} else {
x = p;
SplitValue(rs(p), v, rs(p), y);
PushUp(x);
}
}
void SplitRank(int p, int rk, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
if(rk <= siz[ls(p)]) {
y = p;
SplitRank(ls(p), rk, x, ls(p));
PushUp(y);
} else {
x = p;
SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
PushUp(x);
}
}
int Merge(int x, int y) {
if(!x || !y)
return x | y;
if(rnd[x] < rnd[y]) {
rs(x) = Merge(rs(x), y);
PushUp(x);
return x;
} else {
ls(y) = Merge(x, ls(y));
PushUp(y);
return y;
}
}
int NewNode(int v) {
++tot;
ch[tot][0] = ch[tot][1] = 0;
val[tot] = v, rnd[tot] = rand();
siz[tot] = 1;
return tot;
}
void Insert(int &root, int v) {
int x = 0, y = 0;
SplitValue(root, v, x, y);
root = Merge(Merge(x, NewNode(v)), y);
}
void Remove(int &root, int v) {
int x = 0, y = 0, z = 0;
SplitValue(root, v, x, z);
SplitValue(x, v - 1, x, y);
y = Merge(ls(y), rs(y));
root = Merge(Merge(x, y), z);
}
int GetRank(int &root, int v) {
int x = 0, y = 0;
SplitValue(root, v - 1, x, y);
int rk = siz[x] + 1;
root = Merge(x, y);
return rk;
}
int GetValue(int &root, int rk) {
int x = 0, y = 0, z = 0;
SplitRank(root, rk, x, z);
SplitRank(x, rk - 1, x, y);
int v = val[y];
root = Merge(Merge(x, y), z);
return v;
}
int GetPrev(int &root, int v) {
int x = 0, y = 0;
SplitValue(root, v - 1, x, y);
int prev = GetValue(x, siz[x]);
root = Merge(x, y);
return prev;
}
int GetNext(int &root, int v) {
int x = 0, y = 0;
SplitValue(root, v, x, y);
int next = GetValue(y, 1);
root = Merge(x, y);
return next;
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n;
scanf("%d", &n);
Init();
for(int i = 1; i <= n; ++i) {
int op, x;
scanf("%d%d", &op, &x);
switch(op) {
case 1:
Insert(root, x);
break;
case 2:
Remove(root, x);
break;
case 3:
printf("%d\n", GetRank(root, x));
break;
case 4:
printf("%d\n", GetValue(root, x));
break;
case 5:
printf("%d\n", GetPrev(root, x));
break;
case 6:
printf("%d\n", GetNext(root, x));
break;
}
}
return 0;
}
实际上还有一些奇奇怪怪的,Treap也是可以自底向上O(n)建树。
附带一个回收操作。
//O(n)建树,返回新树的根
int st[MAXN], stop;
char buf[MAXN];
inline int Build(int n) {
stop = 0;
for(int i = 0; i < n; ++i) {
int tmp = NewNode(buf[i]), last = 0;
while(stop && rnd[st[stop]] > rnd[tmp]) {
last = st[stop];
PushUp(last);
st[stop--] = 0;
}
if(stop)
rs(st[stop]) = tmp;
ls(tmp) = last;
st[++stop] = tmp;
}
while(stop)
PushUp(st[stop--]);
return st[1];
}
//O(n)回收整棵树
inline void UnBuild(int p) {
if(!p)
return;
UnBuild(ls(p));
UnBuild(rs(p));
RecBin.push(p);
}
无旋Treap最主要可以维护翻转序列,这个时候需要SplitRank。每次Merge前要把进入的那棵树的lazy下传,而分裂则是把p的lazy下传。
查询操作不进行修改还是非递归的版本,倒是写了很久。以后要注意,带有rand的调试,假如下载数据之后还是和本地跑得不一样,可能是不同平台rand实现的问题。但是提交一定要用系统的rand,否则自己的生成器可能会因为不够随机而被卡。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls(p) ch[p][0]
#define rs(p) ch[p][1]
const int MAXN = 100000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
void Init() {
tot = root = 0;
}
void PushUp(int p) {
siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
}
void SplitValue(int p, int v, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
if(v < val[p]) {
y = p;
SplitValue(ls(p), v, x, ls(p));
PushUp(y);
} else {
x = p;
SplitValue(rs(p), v, rs(p), y);
PushUp(x);
}
}
void SplitRank(int p, int rk, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
if(rk <= siz[ls(p)]) {
y = p;
SplitRank(ls(p), rk, x, ls(p));
PushUp(y);
} else {
x = p;
SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
PushUp(x);
}
}
int Merge(int x, int y) {
if(!x || !y)
return x | y;
if(rnd[x] < rnd[y]) {
rs(x) = Merge(rs(x), y);
PushUp(x);
return x;
} else {
ls(y) = Merge(x, ls(y));
PushUp(y);
return y;
}
}
int NewNode(int v) {
++tot;
ch[tot][0] = ch[tot][1] = 0;
val[tot] = v, rnd[tot] = rand();
siz[tot] = 1;
return tot;
}
void Insert(int &root, int v) {
int x = 0, y = 0;
SplitValue(root, v, x, y);
root = Merge(Merge(x, NewNode(v)), y);
}
void Remove(int &root, int v) {
int x = 0, y = 0, z = 0;
SplitValue(root, v, x, z);
SplitValue(x, v - 1, x, y);
y = Merge(ls(y), rs(y));
root = Merge(Merge(x, y), z);
}
int GetRank2(int p, int v) {
int rk = 1;
while(p) {
if(v < val[p])
p = ls(p);
else if(v == val[p])
p = ls(p);
else {
rk += siz[ls(p)] + 1;
p = rs(p);
}
}
return rk;
}
int GetValue2(int p, int rk) {
while(p) {
if(rk <= siz[ls(p)])
p = ls(p);
else if(rk == siz[ls(p)] + 1)
return val[p];
else {
rk -= siz[ls(p)] + 1;
p = rs(p);
}
}
}
int GetPrev2(int p, int v) {
int prev;
while(p) {
if(v <= val[p])
p = ls(p);
else {
prev = val[p];
p = rs(p);
}
}
return prev;
}
int GetNext2(int p, int v) {
int next;
while(p) {
if(v < val[p]) {
next = val[p];
p = ls(p);
} else
p = rs(p);
}
return next;
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n;
scanf("%d", &n);
Init();
for(int i = 1; i <= n; ++i) {
int op, x;
scanf("%d%d", &op, &x);
switch(op) {
case 1:
Insert(root, x);
break;
case 2:
Remove(root, x);
break;
case 3:
printf("%d\n", GetRank2(root, x));
break;
case 4:
printf("%d\n", GetValue2(root, x));
break;
case 5:
printf("%d\n", GetPrev2(root, x));
break;
case 6:
printf("%d\n", GetNext2(root, x));
break;
}
}
return 0;
}
模板 - 无旋Treap的更多相关文章
- [模板] 无旋Treap (C++ class)
注意!本帖不是算法介绍!只是贴代码(逃) //嫌stdlib的rand太慢,手打了一个 /* Author: hotwords */ typedef unsigned int tkey; class ...
- 模板——无旋Treap
#include "bits/stdc++.h" using namespace std; inline int read(){ ,k=;char ch=getchar(); :, ...
- 无旋treap的简单思想以及模板
因为学了treap,不想弃坑去学splay,终于理解了无旋treap... 好像普通treap没卵用...(再次大雾) 简单说一下思想免得以后忘记.普通treap因为带旋转操作似乎没卵用,而无旋tre ...
- 模板 - 数据结构 - 可持久化无旋Treap/PersistentFHQTreap
有可能当树中有键值相同的节点时,貌似是要对Split和Merge均进行复制的,本人实测:只在Split的时候复制得到了一个WA,但只在Merge的时候复制还是AC,可能是恰好又躲过去了.有人说假如确保 ...
- 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap
https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...
- 浅谈无旋treap(fhq_treap)
一.简介 无旋Treap(fhq_treap),是一种不用旋转的treap,其代码复杂度不高,应用范围广(能代替普通treap和splay的所有功能),是一种极其强大的平衡树. 无旋Treap是一个叫 ...
- [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec Mem ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
- [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...
随机推荐
- spring mvc @Valid 数据验证
//对书的单价校验不能是空,价格在20-100之间 @DecimalMax(value = "100", message = "价格不超过100元") ...
- C# 向上取整数
PageCount = personInfodb.get_count(selected_id); //数据总数 PageCount = (int)Math.Ceiling(PageCount / (P ...
- BZOJ3438 小M的作物(和拓展)
题目链接:戳我 我们如果要选择一种种植情况的话,一定是其他的选择都不可行了.这种决策问题用最小割来处理最好不过. 建图方式--A为源点,B为汇点.然后将每个点分别向A,B连边,边权为种植它的价值.组合 ...
- [BZOJ3456]城市规划:DP+NTT+多项式求逆
写在前面的话 昨天听吕老板讲课,数数题感觉十分的神仙. 于是,ErkkiErkko这个小蒟蒻也要去学数数题了. 分析 Miskcoo orz 带标号无向连通图计数. \(f(x)\)表示\(x\)个点 ...
- Mysql中 BLOB字段转String的方法
转:https://www.cnblogs.com/renjie0520/p/5242350.html 1.通过sql直接转换 select CONVERT (*** USING utf8) AS u ...
- CentOS7服务器配置
CentOS7服务器配置 1.更换yum软件源 下载阿里源 cd /etc/yum.repos.d sudo wget -nc http://mirrors.aliyun.com/repo/Cento ...
- VC程序禁用提示框
程序需要24小时不中断 如果错误提示了的话 runtime error 监控程序就不能重启 下面是网上找的 方便以后用到 http://blog.csdn.net/yuzhiyuxia/article ...
- leetcode-mid- math-166. Fraction to Recurring Decimal
mycode 73.92% 如何判断同号? 1)res = "-" if ((numerator>0) ^ (denominator>0)) else " ...
- ros the public key is not available
W: An error occurred during the signature verification. The repository is not updated and the previo ...
- leetcode 回文二叉树
C++最简单的方法,遍历存在vector<int> ivec容器中,然后头尾对应比较 O(n)时间,O(n)空间 /** * Definition for singly-linked li ...