https://blog.csdn.net/piaocoder/article/details/48720007
// 头文件
#include <ext/rope>
using namespace __gnu_cxx;
rope<int> a;
rope<char> a;
a.push_back(x) // 在末尾插入
a.pop_back(x) // 删除最后一个元素
a.size() // 返回长度
a.insert(int pos, x) // 在pos插入x
a.erase(int pos, int sum) // 在pos删除sum个元素
a.replace(int pos, x) // 将pos替换为x
a.substr(int pos, int sum) // 在pos处截取sum个元素
a.at(x) a[x] //访问第x个元素
crope a 等价于 rope<char> a
与vector区别:
vector的erase是a.erase(a.begin(),a.end());
而rope 的 erase 是a.erase(pos,sum) 表示从pos开始,删除sum个元素
如果要做成段的转移,则可以用a.insert(pos,a.substr(pos2,sum))
用一道例题吧
题目链接:https://www.nowcoder.com/acm/contest/141/C
题意:给定一段1-n的序列,给定m次操作,每次操作给出p和q,即从p开始后q张卡片移到最前面,问最终卡片顺序。
题解:
用rope(具体见另一篇博客)
刚开始想把p和q转化成l和r区间,后来发现不转化反而符合rope的条件。就直接来了。
数据加强后好像tle了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <ext/rope>
#include <list>
using namespace std;
using namespace __gnu_cxx;
#define INIT(x) memset(x,0,sizeof(x))
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
inline void read(int &x)
{
int f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
inline void print(int x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
print(x/10);
putchar(x%10+'0');
}
int n,m;
rope<int> a;
int main()
{
read(n),read(m);
for(int i=1;i<=n;i++)
{
a.push_back(i);
}
for(int i=0;i<m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
a.insert(0,a.substr(l-1,r));
a.erase(l+r-1,r);
}
for(auto i:a)
{
cout<<i<<" ";
}
return 0;
}