BZOJ1269 文本编辑器 (Splay)

题目大意:

维护一个文本编辑器,支持下列操作:
1.将光标移动到某一位置
2.在光标后插入一段字符串
3.删除光标后的一段字符
4.翻转光标后的一段字符
5.输出光标后的一个字符
6.光标–
7.光标++

——PoPoQQQ


题解

Splay模板训练题,注意读入Insert之后要读入一个回车(多余字符),这道题bzoj给我发了一份错误的数据,导致我本机RE交上去AC调了一晚上,太差了!

这里有一个小trick,就是在scanf里面读入%*c表示读一个字符但是并不赋值给任何位置。


代码

#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;

int sum=0;
struct Node {
    char key;
    int siz;
    bool flip;
    Node *ch[2],*fa;
    Node(char);
    void maintain();
    void pushdown();
    int dir(int k) {
        if(k==ch[0]->siz+1) return -1;
        return k>ch[0]->siz+1;
    }
    int son() {
        if(fa->ch[1]==this) return 1;
        if(fa->ch[0]==this) return 0;
        return -1;
    }
}*null,*root;
Node:: Node(char _):key(_) {
    siz=null?1:0;
    flip=false;
    ch[0]=ch[1]=fa=null;
}
void Node:: maintain() {
    siz=ch[0]->siz+1+ch[1]->siz;
    return;
}
void Node:: pushdown() {
    if(flip) {
        std::swap(ch[0],ch[1]);
        ch[0]->flip^=1, ch[1]->flip^=1;
        flip=false;
    }
}

void print(Node* cur) {
    if(cur==null) return;
    print(cur->ch[0]);
    cout<<cur->key;
    print(cur->ch[1]);
}

void Rotate(Node* cur,int dir) {
    Node *tmp=cur->ch[dir^1];
    cur->ch[dir^1]=tmp->ch[dir], tmp->ch[dir]->fa=cur;
    tmp->ch[dir]=cur;
    cur->maintain(), tmp->maintain();
    if(~cur->son()) cur->fa->ch[cur->son()]=tmp;
    tmp->fa=cur->fa, cur->fa=tmp;
}

void Pushdown(Node* cur) {
    if(cur->fa!=null) Pushdown(cur->fa);
    cur->pushdown();
}

void Splay(Node *cur) {
    Pushdown(cur);
    while(~cur->son()) {
        int dir=cur->son();
        if(dir==cur->fa->son()) Rotate(cur->fa->fa,dir^1);
        Rotate(cur->fa,dir^1);
    }
}

Node* _find(Node *cur,int k) {
    cur->pushdown();
    int dir=cur->dir(k);
    if(dir==1) k-=cur->ch[0]->siz+1;
    if(dir==-1) return cur;
    return _find(cur->ch[dir],k);
}

Node* Merge(Node *lhs,Node *rhs) {
    if(lhs==null) return rhs;
    if(rhs==null) return lhs;
    Node *tmp=_find(lhs,lhs->siz);
    Splay(tmp);
    tmp->ch[1]=rhs, rhs->fa=tmp, tmp->maintain();
    return tmp;
}

void Split(Node* org,int k,Node* &lhs,Node* &rhs) {
    if(org==null) {
        lhs=null, rhs=null;
        return;
    } else if(k==0) {
        lhs=null, rhs=org;
        return;
    } else if(k==org->siz) {
        lhs=org, rhs=null;
        return;
    }
    Node *tmp=_find(org,k);
    Splay(tmp);
    lhs=tmp, rhs=tmp->ch[1];
    rhs->fa=null, lhs->ch[1]=null, lhs->maintain();
    return;
}

char s[int(2e7)+111];
void Build(Node* &cur,int l,int r) {
    if(l>r) {cur=null; return;}
    int mid=(l+r)>>1;
    cur=new Node(s[mid]);
    Build(cur->ch[0],l,mid-1), cur->ch[0]->fa=cur;
    Build(cur->ch[1],mid+1,r), cur->ch[1]->fa=cur;
    cur->maintain();
}

void Insert(int pos,int len) {
    int x=root->siz;
    gets(s+1);
    Node *add,*lhs,*rhs;
    Build(add,1,len);
    Split(root,pos,lhs,rhs);
    root=Merge(Merge(lhs,add),rhs);
}

void Delete(int pos,int len) {
    Node *lhs,*mid,*rhs;
    Split(root,pos,lhs,mid);
    Split(mid,len,mid,rhs);
    root=Merge(lhs,rhs);
}

void Reverse(int pos,int len) {
    Node *lhs,*mid,*rhs;
    Split(root,pos,lhs,mid);
    Split(mid,len,mid,rhs);
    mid->flip^=1;
    root=Merge(Merge(lhs,mid),rhs);
}

char Get(int pos) {
    Node *tmp=_find(root,pos);
    return tmp->key;
}

void init_null() {
    null=new Node('\n');
    null->ch[0]=null->ch[1]=null->fa=null;
    root=null;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE
    init_null();
    int cas,len,pos=0;
    scanf("%d%*c",&cas);
    for(int t=0;t<cas;t++) {

        char op[50];
        scanf("%s",op);
        if(op[0]=='M') {
            scanf("%d",&pos);
        } else if(op[0]=='I') {
            scanf("%d%*c",&len);
            sum+=len;
            Insert(pos,len);
        } else if(op[0]=='D') {
            scanf("%d",&len);
            sum-=len;
            Delete(pos,len);
        } else if(op[0]=='R') {
            scanf("%d",&len);
            Reverse(pos,len);
        } else if(op[0]=='P') {
            pos--;
        } else if(op[0]=='N') {
            pos++;
        } else if(op[0]=='G') {
            printf("%c\n",Get(pos+1));
        }
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值