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;
}

最新文章

  1. 最常见的 20 个 jQuery 面试问题及答案
  2. 倍数提高工作效率的 Android Studio 奇技
  3. 关于lr调用jar在vuser中可以运行,但是controller中却报错的问题
  4. org.hibernate.HibernateException: A collection with cascade=&quot;all-delete-orphan&quot; was no longer referenced by the owning entity instance:
  5. Python on VS Code
  6. 使用everything把一个文件夹里(包含子目录)的所有图片拷贝到另一个文件夹
  7. 基于哈夫曼编码的文件压缩(c++版)
  8. Dockerfile封装Django镜像
  9. Redis的过期策略和内存淘汰策略
  10. Oracle常见错误:ORA-06550、ORA-00911、ORA-02085
  11. Android studio 搭建测试环境 创建虚拟机
  12. 『PyTorch』第四弹_通过LeNet初识pytorch神经网络_上
  13. mybatis 异常和注意
  14. Adobe After Effects CS6 操作记录
  15. PDFSharp生成PDF.
  16. Masonry插件:内容始终水平居中
  17. hdu 5183 hash表
  18. WPF MessageContract DataContract
  19. Codeception (安装)
  20. thinkphp5.0目录结构

热门文章

  1. wepy-wxss报错
  2. js原生高逼格插件
  3. 对Moment.js的研究
  4. css3 中的2D转换
  5. PHP 大文件上传进度条实现
  6. Shell的简单介绍(一)
  7. (22)Python练习项目集
  8. es之词库热更新解决方案
  9. (转)sql server 事务与try catch
  10. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_1_缓冲流的原理