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. proxy配置

    关于config.js里面proxy的配置:                  proxy: { '/api': { target: 'http://192.168.***.**:8500', cha ...

  2. 运算符、流程控制(if、while)笔记

    目录 算术运算符 比较运算符(返回一个bool值) 逻辑运算符(把多个条件同时叠加) 赋值运算符 身份运算符 位运算符 成员运算符 python运算优先级 流程控制:向一个方向变化 if判断 单分支结 ...

  3. handy源码阅读(六):udp类

    分为UdpServer类和UdpConn类. struct UdpServer : public std::enable_shared_from_this<UdpServer>, priv ...

  4. RN组件的生命周期

    来自:http://www.devio.org/tags/#React%20Native

  5. github上.md的编写

    # algs4 一:大标题 =========== 二:中标题 ------------ 三:1~6级标题 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 # ...

  6. jq刷新页面

    //页面加载时绑定按钮点击事件$(function(){    $("#按钮id").click(function(){        refresh();    });});// ...

  7. 文本跟随鼠标并且显示x轴和y轴位置

    一.文本跟随鼠标并且显示x轴和y轴位置 代码如下: window.onload=function(){ var mydiv = document.createElement("div&quo ...

  8. 三、angular7登录请求和路由带参传递

    在 app.module.ts 中引入 HttpClientModule 并注入 import {HttpClientModule} from '@angular/common/http'; impo ...

  9. UVA 540 Team Queue(模拟+队列)

    题目代号:UVA 540 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page ...

  10. 进阶1:Linux 环境准备_ 设置网络IP_安装火狐浏览器

    VM 已安装 centos6.5 Final 已安装 配置好了Linux  IP ,并能ping  通 ,例如 : ping www.baidu.com 设置LINUX 网络IP: https://j ...