空间消耗非常玄学,有多大开多大就完事了。其实是因为单次操作可能会有数次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的更多相关文章

  1. 模板 - 数据结构 - 可持久化无旋Treap/PersistentFHQTreap

    有可能当树中有键值相同的节点时,貌似是要对Split和Merge均进行复制的,本人实测:只在Split的时候复制得到了一个WA,但只在Merge的时候复制还是AC,可能是恰好又躲过去了.有人说假如确保 ...

  2. 无旋Treap - BZOJ1014火星人 & 可持久化版文艺平衡树

    !前置技能&概念! 二叉搜索树 一棵二叉树,对于任意子树,满足左子树中的任意节点对应元素小于根的对应元素,右子树中的任意节点对应元素大于根对应元素.换言之,就是满足中序遍历为依次访问节点对应元 ...

  3. 无旋treap的简单思想以及模板

    因为学了treap,不想弃坑去学splay,终于理解了无旋treap... 好像普通treap没卵用...(再次大雾) 简单说一下思想免得以后忘记.普通treap因为带旋转操作似乎没卵用,而无旋tre ...

  4. 模板 - 无旋Treap

    一般而言作为一棵平衡树只需要插入,删除,值求排名,排名求值,前驱,后继,六个接口. #include<bits/stdc++.h> using namespace std; typedef ...

  5. 洛谷 - P3391 【模板】文艺平衡树(Splay) - 无旋Treap

    https://www.luogu.org/problem/P3391 使用无旋Treap维护序列,注意的是按顺序插入的序列,所以Insert实际上简化成直接root和Merge合并,但是假如要在序列 ...

  6. 浅谈无旋treap(fhq_treap)

    一.简介 无旋Treap(fhq_treap),是一种不用旋转的treap,其代码复杂度不高,应用范围广(能代替普通treap和splay的所有功能),是一种极其强大的平衡树. 无旋Treap是一个叫 ...

  7. [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...

  8. [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...

  9. 【算法学习】Fhq-Treap(无旋Treap)

    Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...

随机推荐

  1. kali安装docker以及配置阿里云镜像加速

    1.需求 最近需要用到docker比较多,遂安装使用下,第一次用docker搭建测试环境,不得不说,docker真香.期间遇到了比较多奇奇怪怪的问题,网上的教程也比较多比较乱,遂记录一下. 2.安装d ...

  2. F5设备部署

    旁挂组网(组网模式一) 所谓旁挂组网模式,就是指在BIG-IP LTM上只配置一个Vlan,使用一个端口(或者Trunk端口)连接在网络中,所有的处理均在这一个Vlan中运行.通常有三种常见配置模式. ...

  3. Floyd求解最短路

    Floyd算法适用于求解全源最短路.也就是能够求解任意两点间的最短路径并且是适用于含有负权边的图,但是含有负环则不行了!空间复杂度为O(n2).时间复杂度为O(n3).其具体的原理在百度能够找到很多, ...

  4. Laya 使list渲染支持分帧的思路

    Laya 使list渲染支持分帧的思路 @author ixenos 2019-09-06 1.由于Laya的list渲染时没有做分帧处理,只做了延迟帧处理,所以当单页元素较多时,会有大量运算卡帧的情 ...

  5. python 存取字典dict

    数据处理的时候主要通过两个函数(1):np.save(“test.npy”,数据结构) ----存数据(2):data =np.load('test.npy") ----取数据 1.存列表 ...

  6. flex兼容问题

    display:flex作为C3的新属性,还是有的浏览器不支持的,那下面我们就来说一下他的兼容写法 .box{ display: -webkit-box; /* 老版本语法: Safari, iOS, ...

  7. All men are brothers

    All men are brothers 牛客多校第九场E 给定n个人,起初互不认识 然后m各阶段 每个阶段有两个人x.y认识 求每个阶段选出四个人互不认识的方式 并查集 #include<bi ...

  8. 阿里云万网虚拟主机安装配置Https(SSL)教程

    太多太多的用户咨询阿里云虚拟主机是否可以安装SSL数字证书?万网空间是否可以支持HTTPS协议访问网站?答案只有一个:目前阿里云虚拟主机都不支持安装SSL证书!但是,但是,可以曲线实现目标! 1.为了 ...

  9. mysqladmin processlist; show processlist/status/open tables/engine/variables/table status/columns/grants/index/privileges/innodb status/logs/warnings/////; 结果筛选

    mysqladmin showprocesslist可查看完整sql需要权限. SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此 ...

  10. NSIS打包后无法解压7z资源包的问题

    以前产品一直都是好好的.今天突然不行了.找了半天原因,原来发现7Z的压缩属性变成了"LZMA2"了. 要LZMA才行.