[LOJ120] 持久化序列

使用 rope,需要引入头文件 ext/rope,并且使用命名空间 __gnu_cxx

定义 rope<T> rp;

简单用例

rope<int> *rp[maxn];
his[0]=new rope<char>();
his[i]=new rope<char>(*his[i-1]);

常见操作

代码 功能
push_back(x) 在末尾追加元素 x
insert(pos,x) 插入元素 x 使得它成为序列第 pos
erase(pos,x) pos 开始删除 k
replace(pos,x) pos 开始替换成 x
at(x) 访问第 x 个元素

原题代码

#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N = 300005;

int n,tot,opt,t,k,x;
rope <int>* s[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>n;
    s[0]=new rope<int>();
    for(int i=1;i<=n;i++) {
        cin>>opt>>t>>k;
        switch (opt) {
        case 1:
            cin>>x;
            s[++tot]=new rope<int>(*s[t]);
            s[tot]->insert(k-1,x);
            break;
        case 2:
            s[++tot]=new rope<int>(*s[t]);
            s[tot]->erase(k-1,1);
            break;
        default:
            cout<<s[t]->at(k-1)<<endl;
        }
    }
}

原文地址:https://www.cnblogs.com/mollnn/p/12531787.html