[rope大法好] STL里面的可持久化平衡树--rope
简单用法:
#include <ext/rope>
using namespace __gnu_cxx;
int a[1000];
rope<int> x;
rope<int> x(a,a + n);
rope<int> a(x); x->at(10);
x[10];
x->push_back(x) // 在末尾添加x
x->insert(pos,x) // 在pos插入x
x->erase(pos,x) // 从pos开始删除x个
x->replace(pos,x) // 从pos开始换成x
x->substr(pos,x) // 提取pos开始x个
例题一:
IOI2012
scrivener
题意
设计支持如下 3 种操作:
1.T x:在文章末尾打下一个小写字母 x。(type 操作)
2.U x:撤销最后的x 次修改操作。(Undo 操作)
(注意Query 操作并不算修改操作)
3.Q x:询问当前文章中第x 个字母并输出。(Query 操作)
操作数n<=100000 在线算法
clj都说这是道rope傻逼题……
#include<cstdio>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int maxn=1e5+10;
rope<char> *his[maxn];
int n;
int d[maxn];
inline int lowbit(int x){ return x&-x; } inline void updata(int x){ while(x<=n){ d[x]++; x+=lowbit(x); } }
inline int get(int x){ int res=0; while(x){ res+=d[x]; x-=lowbit(x); }return res; } inline char getC(){ char ch=getchar(); while(!isalpha(ch))ch=getchar(); return ch; } inline int getint()
{
int res=0; char ch,ok=0;
while(ch=getchar())
{
if(isdigit(ch)){ res*=10;res+=ch-'0';ok=1; }
else if(ok)break;
}
return res;
}
void deb(rope<char> s)
{
for(int i=0;i<s.length();i++)
cout<<s[i];puts("");
} int main()
{
// freopen("type.in","r",stdin);
// freopen("type.out","w",stdout);
n=getint();
his[0]=new rope<char>();
for(int i=1;i<=n;i++)
{
his[i]=new rope<char>(*his[i-1]);
// deb(*his[i]);
char opt=getC();
if(opt=='T')
{
char x=getC();
his[i]->push_back(x);
updata(i);
}
else if(opt=='U')
{
updata(i);
int x=getint();
int l=1,r=i,mid,now=get(i);
while(l<r)
{
mid=(l+r)>>1;
if(now-get(mid)>x) l=mid+1;
else r=mid;
}
his[i]=his[l-1];
}
else if(opt=='Q')
{
int x=getint()-1;
putchar(his[i]->at(x));
putchar('\n');
} // deb(*his[i]);
}
return 0;
}
其中,实现可持久化的操作是:his[i]=new rope<char>(*his[i-1]);
他可以实现O(1)的copy历史版本,因为rope的底层是红黑树,所以copy时只需copy根指针。
一键持久化……
例题二:
BZOJ 3673: 可持久化并查集 by zky
n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
0<n,m<=2*10^4
分析:直接可持久化并查集的fa数组就可以了。
ACCode:
#include <cstdio>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx; const int maxm = 20010; rope<int> *rp[maxm]; int find(int i,int x)
{
if(rp[i]->at(x) == x) return x;
int f = find(i,rp[i]->at(x));
if(f == rp[i]->at(x)) return f;
rp[i]->replace(x,f);
return rp[i]->at(x);
} inline void merge(int i,int x,int y)
{
x = find(i,x),y = find(i,y);
if(x != y) rp[i]->replace(y,x);
} int a[maxm],lastans; int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i ++) a[i] = i;
rp[0] = new rope<int> (a,a + n + 1);
for(int i = 1;i <= m;i ++)
{
rp[i] = new rope<int> (*rp[i - 1]);
int opt; scanf("%d",&opt);
if(opt == 1)
{
int a,b; scanf("%d%d",&a,&b);
merge(i,a/* ^ lastans*/,b/* ^ lastans*/);
}
else if(opt == 2)
{
int k; scanf("%d",&k);
rp[i] = rp[k/* ^ lastans*/];
}
else
{
int a,b; scanf("%d%d",&a,&b);
printf("%d\n",lastans = (find(i,a/* ^ lastans*/) == find(i,b/* ^ lastans*/)));
}
}
return 0;
}
例题三:
AHOI2006文本编辑器editor
题意
设计数据结构支持
插入删除反转字符串
分析:
由于rope的底层实现,insert,erase,get都是logn的
就是翻转不行,不是自己手写的打不了标记啊!!
怎么办?
答:同时维护一正一反两个rope……反转即交换两个子串……Orz……
区间循环位移?简单,拆成多个子串连起来就好了……
区间a变b b变c c变d …… z变a? 呃……维护26个rope?
区间和?滚蛋,那是线段树的活
区间kth?sorry,与数值有关的操作rope一概不支持……
5555 维修数列只能自己写了……
ACCode:
#include <cstdio>
#include <ext/rope>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_cxx;
crope a,b,tmp;
char s[10];
int now,n,len,size;
char str[2000000],rstr[2000000];
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
switch(s[0])
{
case 'M':
{
scanf("%d",&now);
break;
}
case 'P':
{
now--;
break;
}
case 'N':
{
now++;
break;
}
case 'G':
{
putchar(a[now]);
putchar('\n');
break;
}
case 'I':
{
scanf("%d",&size);
len=a.length();
for(int i=0;i<size;i++)
{
do
{
str[i]=getchar();
}
while(str[i]=='\n');
rstr[size-i-1]=str[i];
}
rstr[size]=str[size]='\0';
a.insert(now,str);
b.insert(len-now,rstr);
break;
}
case 'D':
{
scanf("%d",&size);
len=a.length();
a.erase(now,size);
b.erase(len-now-size,size);
break;
}
case 'R':
{
scanf("%d",&size);
len=a.length();
tmp=a.substr(now,size);
a=a.substr(0,now) + b.substr(len-now-size,size) + a.substr(now+size,len-now-size);
b=b.substr(0,len-now-size)+tmp+b.substr(len-now,now);
break;
}
}
}
return 0;
}
END
[rope大法好] STL里面的可持久化平衡树--rope的更多相关文章
- 牛客多校3 C-Shuffle Cards(rope大法解决数组分块)
Shuffle Cards 链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26 ...
- C++ STL rope介绍----可持久化平衡树
大致介绍: rope这个东西,我刚刚知道这玩意,用的不是很多,做个简单的介绍. 官方说明:我是刘邦(我估计你是看不懂的). rope就是一个用可持久化平衡树实现的“重型”string(然而它也可以保存 ...
- C++ STL rope 可持久化平衡树 (可持久化数组)
官方文档好像 GG 了. rope 不属于标准 STL,属于扩展 STL,来自 pb_ds 库 (Policy-Based Data Structures). 基本操作: #include <e ...
- 谈c++ pb_ds库(一)rope大法好
参考资料 1)官方说明 支持 sorry,cena不支持rope 声明 1)头文件 #include<ext/rope> 2)调用命名空间 using namespace __gnu_cx ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- 【LG3835】可持久化平衡树
[LG3835]可持久化平衡树 题面 洛谷 解法一 参考文章 rope大法好 \(rope\)基本操作: #include<ext/rope> using namespace __gnu_ ...
- [cogs2314][HZOI 2015] Persistable Editor - 可持久化平衡树
[cogs2314][HZOI 2015]Persistable Editor - 可持久化平衡树 题目链接 首先吐槽扯淡几句 [题目描述] 维护一种可持久化的文本编辑器,支持下列操作: 1 p st ...
- 可持久化Trie & 可持久化平衡树 专题练习
[xsy1629]可持久化序列 - 可持久化平衡树 http://www.cnblogs.com/Sdchr/p/6258827.html [bzoj4260]REBXOR - Trie 事实上只是一 ...
- [Luogu 3835]【模板】可持久化平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本): 插入x数 删除x数(若有多个相同的数,因只删除一个,如果没有请忽略该操作 ...
随机推荐
- h3c 瘦ap无法上线解决办法(WA4320i-ACN)
瘦ap无法上线的原因主要有两个:1.无法获取IP地址 2 .版本 胖ap转瘦ap: 1.使用网线+web对ap进行管理,默认IP地址为:192.168.0.50,用户名:admin 密码:h3capa ...
- 通过编写c语言程序,运行时实现打印另一个程序的源代码和行号
2017年6月1日程序编写说明: 1.实现行号的打印,实现代码的读取和输出,理解主函数中的参数含义. 2.对fgets函数理解不够 3.对return(1); return 0的含义理解不够 4.未实 ...
- python-day2爬虫基础之爬虫基本架构
今天主要学习了爬虫的基本架构,下边做一下总结: 1.首先要有一个爬虫调度端,来启动爬虫.停止爬虫或者是监视爬虫的运行情况,在爬虫程序中有三个模块,首先是URL管理器来对将要爬取的URL以及爬取过的UR ...
- Python笔记_第四篇_高阶编程_GUI编程之Tkinter_3.数据显示
1. 表格数据显示: 图示: 实例: import tkinter from tkinter import ttk # 创建主窗口__编程头部 win = tkinter.Tk() # 设置标题 wi ...
- 填平新版本Xcode安装插件不成功的坑
一般情况下,安装xcode不成功现象基本上都出现在更新xcode或者重装之后出现的情况,下面原理性德东西,我就不赘述了,度娘上很容易看到,通过这段只是希望大家花费尽量少得时间将xcode插件安装成功. ...
- 2.监控利器nagios手把手企业级实战第一部
1. 什么是Nagios? Nagios是一款开源的网络及服务的监控工具,功能强大,灵活性强,需要注意的是,其服务端只能在linux上面安装. Nagios可以进行分布 ...
- Pytorch基础——使用 RNN 生成简单序列
一.介绍 内容 使用 RNN 进行序列预测 今天我们就从一个基本的使用 RNN 生成简单序列的例子中,来窥探神经网络生成符号序列的秘密. 我们首先让神经网络模型学习形如 0^n 1^n 形式的上下文无 ...
- 吴裕雄--天生自然 pythonTensorFlow图形数据处理:windows操作系统安装指定版本的tensorflow
pip install tensorflow==1.14.0
- 使用DataSnap Server环境搭建注意的问题。
1.Data exploer 的MYSQL文件(Libmysql.dll)放到系统的system32目录即可
- 高精度处理斐波那契序列(C语言)
#include<stdio.h> #include<string.h> //memset,strcpy,strlen函数头文件 int main(void) { ];//用来 ...