一般而言作为一棵平衡树只需要插入,删除,值求排名,排名求值,前驱,后继,六个接口。

#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的更多相关文章

  1. [模板] 无旋Treap (C++ class)

    注意!本帖不是算法介绍!只是贴代码(逃) //嫌stdlib的rand太慢,手打了一个 /* Author: hotwords */ typedef unsigned int tkey; class ...

  2. 模板——无旋Treap

    #include "bits/stdc++.h" using namespace std; inline int read(){ ,k=;char ch=getchar(); :, ...

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

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

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

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

  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:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec  Mem ...

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

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

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

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

随机推荐

  1. centos7下open--v!(p/n)部署

    一,client-server 路由模式 使用tun,openssl,lzo压缩,启用转发,生成证书,关闭selinux 同步下时间 #1安装 yum -y install openvpn easy- ...

  2. word和画图

    文档和画图收费文档:edu.51cto.com/course/course_id-4992.htmledu.51cto.com/course/course_id-4991.html

  3. cvpr 2019 workshop&oral session

    1. Verification and Certification of Neural Networks神经网络的验证与认证 2. Automated Analysis of Marine Video ...

  4. Mac 安装 MongoDB 数据库

    1. 使用 brew install mongodb 安装 (参见下图) 2. 安装成功如下图 (成功与否可参考 方框内字符) 3. 启动 MongoDB 数据库 3.1 先创建数据库存储目录 /da ...

  5. [CSP-S模拟测试]:Walker(数学)

    题目传送门(内部题86) 输入格式 第一行$n$接下来$n$行,每行四个浮点数,分别表示变换前的坐标和变换后的坐标 输出格式 第一行浮点数$\theta$以弧度制表示第二行浮点数$scale$第三行两 ...

  6. python 不灭

    进程与线程的区别? 1进程是CPU资源分配的最小单元,线程是CPU计算的最小单元. 2一个进程中可以有多个线程 3对于python来说,它的进程与线程与其它语言有差异,它是有GIL锁,保证同一进程中, ...

  7. git创建公钥匙

    目的: 使用SSH公钥可以让你在你的电脑和码云通讯的时候使用安全连接(git的remote要使用SSH地址) 1.打开终端进入.ssh目录输入当下命令 cd ~/.ssh 如果.ssh文件夹不存在,执 ...

  8. architecture 20190628

    https://abp.io/  --ABP v2 官网 https://grpc.io/ --gRPC官网 https://devblogs.microsoft.com/dotnet/introdu ...

  9. 用Jquery选择器计算table中的某一列某一行的合计

    核心算法: $('#tableId tr').each(function() { $(this).find('td:eq(columnIndex)').each(function() { totalA ...

  10. 改变主程序的入口 main

    main只是开发工具所规定的一个特殊函数名称而已.它既不是程序的入口,也不是必须要有的函数. 程序的入口点记录在可执行文件中的一个数据,该数据标明程序从哪个位置开始执行,这个数据是连接程序的时候由li ...