文本编辑器 未完成 Treap

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+1e5;
unsigned int SEED = 17;
inline int Rand(){
    SEED=SEED*1103515245+12345;
    return SEED;
}
typedef pair<int,int> P;
struct TP{
    int lc[maxn],rc[maxn],fix[maxn],wgt[maxn],size[maxn],rev[maxn];
    char val[maxn];
    int tot,root;
    void init(){
        memset(rev,0,sizeof rev);
        tot=1;root=0;
        lc[tot]=rc[tot]=rev[tot]=0;
    }
    int node(char v){
        lc[tot]=rc[tot]=0;
        fix[tot]=Rand(); 
        wgt[tot]=size[tot]=1;
        rev[tot]=0;
        val[tot]=v;
        return tot++;
    }
    void pd(int o){
        if(rev[o]&1){
            swap(lc[o],rc[o]);
            if(lc[o]) rev[lc[o]]+=rev[o];
            if(rc[o]) rev[rc[o]]+=rev[o];
        }
        rev[o]=0;
    }
    void pu(int o){
        size[o]=wgt[o];
        size[o]+=size[lc[o]]+size[rc[o]];
    }
    void print(int o){
        if(lc[o]) print(lc[o]);
        printf("%c",val[o]);
        if(rc[o]) print(rc[o]);
    }
    int merge(int a,int b){
        if(!a) return b;
        if(!b) return a;
        pd(a);pd(b);
        if(fix[a]>fix[b]){
            rc[a]=merge(rc[a],b);
            pu(a);
            return a;
        }
        else{
            lc[b]=merge(a,lc[b]);
            pu(b);
            return b;
        }
    }
    P split(int a,int k){
        if(!a) return P(0,0);
        P p;
        pd(a);
        if(size[lc[a]]>=k){
            p=split(lc[a],k);
            lc[a]=p.second;
            pu(a);
            p.second=a;////
        }
        else{
            p=split(rc[a],k-size[a]-1);//
            rc[a]=p.first;
            pu(a);
            p.first=a;
        }
        return p;
    }
    void insert(int k,int len){
        char c; int tmp;
        P p=split(root,k);
        scanf("%c",&c);
        for(int i = 1; i <= len; i++){
            scanf("%c",&c);
            tmp=node(c);
            p.first=merge(p.first,tmp);
        }
        root=merge(p.first,p.second);
    }
    void del(int k,int len){
        P x,y;
        x=split(root,k);
        y=split(x.second,len);
        root=merge(x.first,y.second);
    }
}tp;
int main(){
    P x,y;
    int n,now,num;char str[111];
    now=0;
    tp.init();
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%s",str);
        if(str[0]=='M'){
            scanf("%d",&now);
        }
        if(str[0]=='I'){
            scanf("%d",&num);
            tp.insert(now,num);
        }
        if(str[0]=='D'){
            scanf("%d",&num);
            tp.del(now,num);
        }
        if(str[0]=='R'){
            scanf("%d",&num);
            x=tp.split(tp.root,now);
            y=tp.split(x.second,num);
            tp.rev[y.first]++;
            x.second=tp.merge(y.first,y.second);
            tp.root=tp.merge(x.first,x.second);
        }
        if(str[0]=='G'){
            x=tp.split(tp.root,now);
            y=tp.split(x.second,1);
            tp.print(y.first);printf("\n");
            x.second=tp.merge(y.first,y.second);
            tp.root=tp.merge(x.first,x.second);
        }
        if(str[0]=='P'){
            now--;
        }
        if(str[0]=='N'){
            now++;
        }
    }
}
posted @ 2017-12-19 01:29  Caturra  阅读(163)  评论(0)    收藏  举报