https://www.luogu.org/problem/P4008

无旋Treap也可以维护序列。

千万要注意要先判断p节点存在才进行Show操作,不然输出一个'\0'(或者RecBin里面的东西)草。

假如有限制同时存在的节点数量的话,UnBuild操作是显得重要的。

当然这里最主要的是类似笛卡尔树的O(n)建立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 = 2000000 + 5;
char val[MAXN];
int ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root; int cur;
stack<int> RecBin; inline void Init() {
root = 0, tot = 0;
cur = 0;
srand(19260817);
while(RecBin.size())
RecBin.pop();
} inline void PushUp(int p) {
siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
} void SplitRank(int p, int rk, int &x, int &y) {
if(!p) {
x = y = 0;
return;
}
//PushDown(p);
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;
//这个是小根Treap
if(rnd[x] < rnd[y]) {
//PushDown(x);
rs(x) = Merge(rs(x), y);
PushUp(x);
return x;
} else {
//PushDown(y);
ls(y) = Merge(x, ls(y));
PushUp(y);
return y;
}
} inline int NewNode(char v) {
int p;
if(RecBin.size()) {
p = RecBin.top();
RecBin.pop();
} else
p = ++tot;
ch[p][0] = ch[p][1] = 0;
val[p] = v;
rnd[p] = rand();
siz[p] = 1;
return p;
} void Show(int p) {
if(!p)
return;
Show(ls(p));
putchar(val[p]);
Show(rs(p));
} //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);
} inline void Move() {
scanf("%d", &cur);
} inline void Insert(int &root) {
int x = 0, y = 0, z = 0, n;
SplitRank(root, cur, x, z);
scanf("%d", &n);
buf[n] = '\0';
for(int i = 0; i < n; ++i) {
char ch = getchar();
while(ch < 32 || ch > 126 || ch == '\r' || ch == '\n')
ch = getchar();
buf[i] = ch;
}
y = Build(n);
root = Merge(Merge(x, y), z);
} inline void Delete(int &root) {
int x = 0, y = 0, z = 0, n;
SplitRank(root, cur, x, y);
scanf("%d", &n);
SplitRank(y, n, y, z);
//会不会太慢了
UnBuild(y);
//y=Merge(ls(y),rs(y));
root = Merge(x, z);
} inline void Get(int &root) {
int x = 0, y = 0, z = 0, n = 0;
SplitRank(root, cur, x, y);
scanf("%d", &n);
SplitRank(y, n, y, z);
Show(y);
puts("");
root = Merge(Merge(x, y), z);
} inline void Prev() {
--cur;
} inline void Next() {
++cur;
} char op[20];
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int t;
scanf("%d", &t);
Init();
while(t--) {
scanf("%s ", op);
switch(op[0]) {
case 'M':
Move();
break;
case 'I':
Insert(root);
break;
case 'D':
Delete(root);
break;
case 'G':
Get(root);
break;
case 'P':
Prev();
break;
case 'N':
Next();
break;
}
}
return 0;
}

洛谷 - P4008 - 文本编辑器 - 无旋Treap的更多相关文章

  1. 洛谷 - P4567 - 文本编辑器 - 无旋Treap

    https://www.luogu.org/problem/P4567 事实证明无旋Treap是不是不可能会比Splay快? #include<bits/stdc++.h> using n ...

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

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

  3. [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

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

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

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

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

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

  6. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

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

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

  8. 无旋treap的区间操作实现

    最近真的不爽...一道维修数列就做了我1上午+下午1h+1晚上+晚上1h+上午2h... 一道不错的自虐题... 由于这一片主要讲思想,代码我放这里了 不会无旋treap的童鞋可以进这里 呵呵... ...

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

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

随机推荐

  1. java(jdk8)死锁案例

    package testSynchorized; public class SynchronizedDeadLockTest { static Object src1 = new Object(); ...

  2. 【leetcode】1175. Prime Arrangements

    题目如下: Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-inde ...

  3. jq元素左边距

    获取页面某一元素的绝对X,Y坐标,可以用offset():var X = $(‘#DivID’).offset().top;var Y = $(‘#DivID’).offset().left; 获取相 ...

  4. React native 之 Promise

    关键词:Promise Promise.all Promise是什么?=> https://www.runoob.com/w3cnote/es6-promise.html Promise.all ...

  5. spring,配置文件applictionContext.xml,Mybatis mybatis.xml,springMVC spring整合springMVC mybatis

  6. 【CF1252F】Regular Forestation(重心,树同构)

    题意:给定一棵n个点的树,问删去某个点之后所有的树同构,这样分割出来的树最多能有几棵 n<=4000 思路:分割成至少两个size相等的联通块之后size必定小于n/2,与树的重心的定义相同 预 ...

  7. js判断浏览器的类型

    我们很多时候都需要判断浏览器,下面就是一些常用的判断 var ua = navigator.userAgent.toLowerCase(); //用来判断移动端类型 var isWeixin = /M ...

  8. [CSP-S模拟测试]:ants(回滚莫队)

    题目描述 然而贪玩的$dirty$又开始了他的第三个游戏. $dirty$抓来了$n$只蚂蚁,并且赋予每只蚂蚁不同的编号,编号从$1$到$n$.最开始,它们按某个顺序排成一列.现在$dirty$想要进 ...

  9. 基于vue模块化开发后台系统——构建项目

    文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 在熟悉上一篇说到准备工具之后, ...

  10. Otto

    导入依赖:implementation 'com.squareup:otto:1.3.8'1定义一个类继承Bus,并且设置单列模式注册和声明订阅者发送事件,最后解除注册与EventBus相同Event ...