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

#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. 组件内导航之beforeRouteUpdate的使用

    使用场景: 组件复用:路由跳转: beforeRouteUpdate (to, from, next) { // 在当前路由改变,但是该组件被复用时调用 // 举例来说,对于一个带有动态参数的路径 / ...

  2. es之索引的别名操作

    1:增加别名 为索引school添加一个别名alias1: 1.1:创建索引 PUT student{ "settings": {"number_of_shards&qu ...

  3. 搭建Ambari 2.6.0 tar 解压缩报错

    背景:我们使用的方式不是wget 去下载ambari的源码包,而是在windows 的 firefox 下直接下载,将文件存储到本地. 执行 tar -zxvf HDP-2.6.3.0-centos7 ...

  4. Docker push image to Docker hub

    1. Before push image to Docker Hub, register an account in https://hub.docker.com/ 2.Input "doc ...

  5. Phaser3 场景Scene之间的传值 -- HTML网页游戏开发

    一.首先当然得有至少有二个场景sceneA.js,sceneB.js 二.从场景A传值到场景B二种方法 1)通过事件this.events.emit('event key',{objKey:objVa ...

  6. git本地分支推送到远程分支

    1.创建的创建和初始化 创建git仓库可以在远端创建一个仓库, 然后check到本地,在本地的文件里创建工程文件,然后提交 也可以将本地现有的工程和远端的空仓库关联 本地创建了一个工程 iOSDemo ...

  7. kafka-manager怎么查看topic里的数据量

    https://jingyan.baidu.com/article/eb9f7b6d367679869364e8d4.html

  8. C++嵌入lua

    需要在C++程序里面嵌入lua 下面是代码,怕忘记,记录一下 #include <stdio.h> #include <stdlib.h> extern "C&quo ...

  9. Spring boot 自定义一个starter pom

    用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...

  10. c++11多线程---线程操作

    1.等待线程执行完成 join() 方法数会阻塞主线程直到目标线程调用完毕,即join会直接执行该子线程的函数体部分. 2.暂停线程(线程休眠) 使用std::this_thread::sleep_f ...