模板 - 可持久化无旋Treap
空间消耗非常玄学,有多大开多大就完事了。其实是因为单次操作可能会有数次Merge和Split操作,按照下面的版本的话Merge和Split都进行复制,所以一次操作可能复制了4个版本。
四个函数式查询,然后Merge的时候拷贝对应的xy子树,Split的时候拷贝p树。事实上,Merge和Split总是成对出现,只需要在其中喜欢的一个进行可持久化(复制节点)就可以了,比较推荐在Split的时候复制节点。这样单次操作大概复制2个版本。
#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 = 30000000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root[MAXN];
void Init() {
tot = 0;
memset(root,0,sizeof(root));
}
int NewNode(int v) {
++tot;
ch[tot][0] = ch[tot][1] = 0;
val[tot] = v, rnd[tot] = rand();
siz[tot] = 1;
return tot;
}
int CopyNode(int p) {
++tot;
ch[tot][0] = ch[p][0];
ch[tot][1] = ch[p][1];
val[tot] = val[p];
rnd[tot] = rnd[p];
siz[tot] = siz[p];
return tot;
}
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 = CopyNode(p);
SplitValue(ls(y), v, x, ls(y));
PushUp(y);
} else {
x = CopyNode(p);
SplitValue(rs(x), v, rs(x), 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]) {
int p = CopyNode(x);
rs(p) = Merge(rs(p), y);
PushUp(p);
return p;
} else {
int p = CopyNode(y);
ls(p) = Merge(x, ls(p));
PushUp(p);
return p;
}
}
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 v,op, x;
scanf("%d%d%d",&v, &op, &x);
root[i]=root[v];
switch(op) {
case 1:
Insert(root[i], x);
break;
case 2:
Remove(root[i], x);
break;
case 3:
printf("%d\n", GetRank2(root[i], x));
break;
case 4:
printf("%d\n", GetValue2(root[i], x));
break;
case 5:
printf("%d\n", GetPrev2(root[i], x));
break;
case 6:
printf("%d\n", GetNext2(root[i], x));
break;
}
}
return 0;
}
#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 = 20000000 + 5;
int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root[MAXN];
void Init() {
tot = 0;
memset(root,0,sizeof(root));
}
int NewNode(int v) {
++tot;
ch[tot][0] = ch[tot][1] = 0;
val[tot] = v, rnd[tot] = rand();
siz[tot] = 1;
return tot;
}
int CopyNode(int p) {
++tot;
ch[tot][0] = ch[p][0];
ch[tot][1] = ch[p][1];
val[tot] = val[p];
rnd[tot] = rnd[p];
siz[tot] = siz[p];
return tot;
}
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 = CopyNode(p);
SplitValue(ls(y), v, x, ls(y));
PushUp(y);
} else {
x = CopyNode(p);
SplitValue(rs(x), v, rs(x), 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]) {
//int p = CopyNode(x);
int p=x;
rs(p) = Merge(rs(p), y);
PushUp(p);
return p;
} else {
int p=y;
//int p = CopyNode(y);
ls(p) = Merge(x, ls(p));
PushUp(p);
return p;
}
}
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 v,op, x;
scanf("%d%d%d",&v, &op, &x);
root[i]=root[v];
switch(op) {
case 1:
Insert(root[i], x);
break;
case 2:
Remove(root[i], x);
break;
case 3:
printf("%d\n", GetRank2(root[i], x));
break;
case 4:
printf("%d\n", GetValue2(root[i], x));
break;
case 5:
printf("%d\n", GetPrev2(root[i], x));
break;
case 6:
printf("%d\n", GetNext2(root[i], x));
break;
}
}
return 0;
}
模板 - 可持久化无旋Treap的更多相关文章
- 模板 - 数据结构 - 可持久化无旋Treap/PersistentFHQTreap
有可能当树中有键值相同的节点时,貌似是要对Split和Merge均进行复制的,本人实测:只在Split的时候复制得到了一个WA,但只在Merge的时候复制还是AC,可能是恰好又躲过去了.有人说假如确保 ...
- 无旋Treap - BZOJ1014火星人 & 可持久化版文艺平衡树
!前置技能&概念! 二叉搜索树 一棵二叉树,对于任意子树,满足左子树中的任意节点对应元素小于根的对应元素,右子树中的任意节点对应元素大于根对应元素.换言之,就是满足中序遍历为依次访问节点对应元 ...
- 无旋treap的简单思想以及模板
因为学了treap,不想弃坑去学splay,终于理解了无旋treap... 好像普通treap没卵用...(再次大雾) 简单说一下思想免得以后忘记.普通treap因为带旋转操作似乎没卵用,而无旋tre ...
- 模板 - 无旋Treap
一般而言作为一棵平衡树只需要插入,删除,值求排名,排名求值,前驱,后继,六个接口. #include<bits/stdc++.h> using namespace std; typedef ...
- 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap
https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...
- 浅谈无旋treap(fhq_treap)
一.简介 无旋Treap(fhq_treap),是一种不用旋转的treap,其代码复杂度不高,应用范围广(能代替普通treap和splay的所有功能),是一种极其强大的平衡树. 无旋Treap是一个叫 ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
- [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...
- 【算法学习】Fhq-Treap(无旋Treap)
Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...
随机推荐
- pyserial库-串口通讯模块
pySerial 封装了串口通讯模块,支持Linux.Windows.BSD(可能支持所有支持POSIX的操作系统),支持Jython(Java)和IconPython(.NET and Mono). ...
- 【leetcode】1137. N-th Tribonacci Number
题目如下: The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 ...
- 关于反射和动态代理和AOP
package Exercise.reflect; /** * 反射把java中所有的东西都当做对象,甚至是类的本身也作为一种对象,并把它作为Class的对象的实例: * 反射是把类.类的属性.方法都 ...
- darknet-yolov3模型预测框size不正确的原因
问题描述:预测框的中心位置正常,但是预测的框的width和height不正常. 解决方法:使得训练的配置cfg和测试中cfg的输入width, height, anchorbox保持一致! 问题是我在 ...
- React Native 之react-native-sqlite-storage
npm 官网指导: https://www.npmjs.com/package/react-native-sqlite-storage 1. 执行: npm install react-native- ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- scanf() 与 gets()--转载
scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用__gets__函数. gets可以接收空格:而 ...
- CSS3动画@keyframes图片变大变小颜色变化
在我做公司官网的时候也会帮着写一些游戏的静态页,今天产品要求为了突出一个按钮,他要有颜色的变化而且要变大变小,然后我就在网上搜了下呼吸灯和其他的案例,写了个小damo,看着还有些魔性嘞. html: ...
- vue双向绑定原理(简单实现原理附demo)
先上效果图 简单的实现数据的双向绑定首先来了解一个东西:Object.defineProperty() https://developer.mozilla.org/zh-CN/docs/Web/Jav ...
- 关于 Visual stdio 编译报错:error MSB6006: “CL.exe”已退出
网上查看,原因有多种. 1,我自己遇到的是这样的: 环境:VS2019,编译项目 image-master,中间自己重整了原来的目录,移动了很多文件.编译报错:error MSB6006: “CL.e ...