一、简介
rope,顾名思义就是“绳”。
本来是一种可以快速处理 string 类型修改的数据类型,但是因为其可以 复制的原因,导致可以在一些情况作为可持久化数组使用。
二、定义
使用前需要先调用头文件 #include <ext/rope> 和命名空间 using namespace __gnu_cxx;。
定义是符合 STL 格式的,即 rope<T>,其中 T 为存储的数据类型。
例如,rope<int>t 就是定义一个存储 int 类型的 rope 且命名为 。
三、使用
rope 支持以下几种操作:
-
push_back(x)在末尾插入 () -
insert(pos,x)在 处插入 () -
erase(pos,x)在 处删除 个元素() -
size返回 rope 的大小() -
replace(pos,x)将 处的元素替换成 () -
substr(pos,x)从 处开始提取 个元素() -
copy(pos,x,s)从 处开始复制 个元素到 中() -
[x]或at(x)访问第 个元素() -
=将一个 rope 复制给另一个 rope()
四、复杂度
空间复杂度是 的。
时间复杂度见上。
五、例题
#include<bits/stdc++.h>
using namespace std;
#include<ext/rope>
using namespace __gnu_cxx;
vector<rope<int> >t(1000006);
int n,m;
int main()
{
read(n,m);
for(int i=1;i<=n;i++)
t[0].push_back(read());
for(int i=1;i<=m;i++)
{
int v,opt,loc;
read(v,opt,loc);
t[i]=t[v];
if(opt==1)
t[i].replace(loc-1,read());
else
write(t[i][loc-1],'\n');
}
return 0;
}#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
int n,m,l,r;
rope<int>a,b,tmp;
int main()
{
int len;
cin>>n>>m;
for(int i=0;i<n;i++)
{
a.push_back(i+1);
b.push_back(n-i);
}
while(m--)
{
cin>>l>>r;
l--;
r--;
int x=r-l+1;
tmp=a.substr(l,r-l+1);
a=a.substr(0,l)+b.substr(n-r-1,r-l+1)+a.substr(r+1,n-r-1);
b=b.substr(0,n-r-1)+tmp+b.substr(n-l,l);
}
for(int i=0;i<n;i++)
cout<<a[i]<<' ';
return 0;
}