[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数(若有多个相同的数,因只删除一个,如果没有请忽略该操作 ...
随机推荐
- Mac技巧-如何切换至 Mac 地图应用的卫星视图模式
如何切换至Mac地图应用的卫星视图模式?很多刚接触MAC电脑的小伙伴并不是很清楚,今天MACW小编就教教大家切换至 Mac 地图应用的卫星视图模式该怎么做.原文:https://www.macw.co ...
- 201609-1 最大波动 Java
用绝对值,后一天减去前一天即可 import java.util.Scanner; public class Main { public static void main(String[] args) ...
- 吴裕雄--天生自然 JAVA开发学习:条件语句
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out. ...
- C到C++转变简述
从 C 到 C++ 语言的转变 1.平时使用还以 printf, scanf 为主 printf 和 scanf 与 cin cout 相比效率更快 2.头文件改成如下,C++对C语言兼容 #incl ...
- JavaWeb中的高级知识总结
知识结构图 文件下载 默认情况下,如果浏览器可以处理Content-Type响应头中指定数据类型,浏览器就会直接处理,比如显示出HTML页面(text/html),或者显示出照片(image/png) ...
- 初识API网关,API-gateway
1.API-gateway(含义) 所有API的调用统一接入API网关层,由网关层负责接入和输出. API Gateway是一个服务器,也可以说是进入系统的唯一节点.这跟面向对象设计模式中的Facad ...
- JS中的let变量
介绍JS中的let变量: let允许你声明一个作用域被限制在块级中的变量.语句或者表达式.在Function中局部变量推荐使用let变量,避免变量名冲突. 作用域规则 let 声明的变量只在其声明的块 ...
- 17.3.13---join函数
1-----Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成 ...
- 通过java语言实现MD5加密
通过java语言实现MD5加密public static String getMd5(String str) { try { MessageDigest md5 = MessageDigest.get ...
- WMS出库单重复
发货通知单?WMS备货单选项勾选 不自动复制?新增?