[AHOI 2006] 可可的文本编辑器

模板题,读入太鬼畜就抄了sxysxy的神奇读入大法

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<ctime>
#include<cctype>
using namespace std;
typedef long long LL;
struct NODE{
    NODE *ls,*rs;
    int r,v,s;
    int rev;
    #define size(x) ((x)?(x)->s:0)
    inline NODE(int v):r(rand()),v(v){
        ls=rs=NULL;
        s=1;
        rev=0;
    }
    inline void pushup(){
        s=1+size(ls)+size(rs);
    }
    inline void reverse(){
        if(!this) return;
        swap(ls,rs);
        rev^=1;
    }
    inline void pushdown(){
        if(this && this->rev){
            ls->reverse();
            rs->reverse();
            rev=0;
        }
    }
};
typedef NODE *pnode;
typedef pair<NODE*,NODE*> droot;
NODE *merge(NODE *a,NODE *b){
    if(!a){b->pushup();return b;}
    if(!b){a->pushup();return a;}
    if(a->r < b->r){
        a->pushdown();
        a->rs=merge(a->rs,b);
        a->pushup();
        return a;
    }
    else {
        b->pushdown();
        b->ls=merge(a,b->ls);
        b->pushup();
        return b;
    }
}
droot split(NODE *x,int k){
    if(!x) return droot(NULL,NULL);
    x->pushdown();
    droot r;
    if(size(x->ls) >= k){
        r=split(x->ls,k);
        x->ls=r.second;
        x->pushup();
        r.second=x;
    }
    else {
        r=split(x->rs,k-size(x->ls)-1);
        x->rs=r.first;
        x->pushup();
        r.first=x;
    }
    return r;
}
NODE *build(int *a,int n){
    pnode *stk=new pnode[n];
    NODE *last;
    int tp=0;
    for(int i=1;i<=n;i++){
        NODE *p=new NODE(a[i]);
        last=NULL;
        while(tp && stk[tp-1]->r > p->r){
            stk[tp-1]->pushup();
            last=stk[tp-1];
            stk[--tp]=NULL;
        }
        if(tp) stk[tp-1]->rs=p;
        p->ls=last;
        stk[tp++]=p;
    }
    while(tp) stk[--tp]->pushup();
    NODE *r=stk[0];
    delete []stk;
    return r;
}
int tmp[1048579*2];
 
char buf[1<<18],*fs,*ft;
inline char readc(){
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++;
}
inline int get_num(){
    int r;
    char c;
    bool s=false;
    while(c=readc()){
        if(c >= '0' && c <= '9'){
            r=c^0x30;
            break;
        }
        else if(c == '-') s=true;
    }
    while(isdigit(c=readc()))
        r=(r<<3)+(r<<1)+(c^0x30);
    return s?-r:r;
}
void readn(int n){
    while(readc() != '\n');
    for(int i=1;i<=n;i++) tmp[i]=readc();readc();
}
void dfs(NODE *o){
    if(!o) return;
    dfs(o->ls);
    if(o->v >= 32 && o->v <= 126) putchar(o->v);
    dfs(o->rs);
}
inline void clear(int n){
    while(n--) readc();
}
 
int n;
int main(){
    freopen("editor.in","r",stdin);
    freopen("editor.out","w",stdout);
    int T=get_num();
    int cur=0;
    NODE *t=NULL;
    while(T--){
        char cmd[2];
        while(!isalpha(cmd[0]=readc()));
        if(cmd[0] == 'M'){
            cur=get_num();
        }
        else if(cmd[0] == 'I'){
            n=get_num();
            readn(n);
            NODE *troot=build(tmp,n);
            if(!t) t=troot;
            else {
                droot ps=split(t,cur);
                t=merge(ps.first,merge(troot,ps.second));
            }
        }
        else if(cmd[0] == 'D'){
            n=get_num();
            droot ps=split(t,cur);
            droot ps2=split(ps.second,n);
            t=merge(ps.first,ps2.second);
        }
        else if(cmd[0] == 'R'){
            n=get_num();
            droot ps=split(t,cur);
            droot ps2=split(ps.second,n);
            ps2.first->reverse();
            t=merge(ps.first,merge(ps2.first,ps2.second));
        }
        else if(cmd[0] == 'G'){
            clear(2);
            n=1;
            droot ps=split(t,cur);
            droot ps2=split(ps.second,n);
            dfs(ps2.first);
            putchar('\n');
            t=merge(ps.first,merge(ps2.first,ps2.second));
        }
        else if(cmd[0] == 'P') cur--,clear(3);
        else if(cmd[0] == 'N') cur++,clear(3);
    }
    return(0);
}

 

posted on 2017-03-01 16:43  KCkowk  阅读(83)  评论(0编辑  收藏  举报