伸展树(插入、删除区间)BZOJ1269

1269: [AHOI2006]文本编辑器editor

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 2019   Solved: 734
[ Submit][ Status]

Description

这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。 编写一个程序: 建立一个空的文本编辑器。 从输入文件中读入一些操作指令并执行。 对所有执行过的GET操作,将指定的内容写入输出文件。

Input

输入文件中第一行是指令条数N,以下是需要执行的N个操作。除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。

Output

依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

Sample Input

10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Get

Sample Output

B
t

HINT

对输入数据我们有如下假定: MOVE操作不超过50 000个,INSERT、DELETE和ROTATE操作作的总个数不超过6 000,GET操作不超过20 000个,PREV和NEXT操作的总个数不超过20 000。 所有INSERT插入的字符数之和不超过2M(1M=1 024*1 024)。 DELETE操作、ROTATE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作不会把光标移动到非法位置。 输入文件没有错误。


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define Key_value ch[ch[root][1]][0]
using namespace std;
const int maxn=1024*1024*2+10;
int ch[maxn][2],pre[maxn],size[maxn],rev[maxn];
char key[maxn],str[maxn];
int root,tot1,tot2,s[maxn];
int N,pos;
void pushup(int r)
{
    size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
}
void NewNode(int &r,int f,char val)
{
    if(tot2)r=s[tot2--];
    else r=++tot1;
    ch[r][0]=ch[r][1]=0;
    pre[r]=f;
    size[r]=1;
    rev[r]=0;
    key[r]=val;
}
void build(int &x,int l,int r,int f)
{
    if(l>r)return;
    int mid=(l+r)>>1;
    NewNode(x,f,str[mid]);
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    pushup(x);
}
void init()
{
    root=tot1=tot2=pos=0;
    ch[root][0]=ch[root][1]=pre[root]=size[root]=rev[root]=0;
    NewNode(root,0,' ');
    NewNode(ch[root][1],root,' ');
    pushup(ch[root][1]);
    pushup(root);
}
void update_rev(int r)
{
    if(!r)return;
    swap(ch[r][0],ch[r][1]);
    rev[r]^=1;
}
void pushdown(int r)
{
    if(rev[r])
    {
        update_rev(ch[r][0]);
        update_rev(ch[r][1]);
        rev[r]=0;
    }
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(y);
    pushdown(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    pushup(y);
}
void Splay(int r,int goal)
{
    pushdown(r);
    while(pre[r]!=goal)
    {
        if(pre[pre[r]]==goal)
        {
            pushdown(pre[r]);
            pushdown(r);
            Rotate(r,ch[pre[r]][0]==r);
        }
        else
        {
            pushdown(pre[pre[r]]);
            pushdown(pre[r]);
            pushdown(r);
            int y=pre[r];
            int kind=(ch[pre[y]][0]==y);
            if(ch[y][kind]==r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    pushup(r);
    if(goal==0)root=r;
}
int get_kth(int r,int k)
{
    pushdown(r);
    int t=size[ch[r][0]]+1;
    if(t==k)return r;
    if(t>k)return get_kth(ch[r][0],k);
    else return get_kth(ch[r][1],k-t);
}
int get_min(int r)
{
    pushdown(r);
    while(ch[r][0])
    {
        r=ch[r][0];
        pushdown(r);
    }
    return r;
}
void Insert(int x)
{
    Splay(get_kth(root,pos+1),0);
    Splay(get_min(ch[root][1]),root);
    build(Key_value,0,x-1,ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}
void erase(int r)
{
    if(r)
    {
        s[++tot2]=r;
        erase(ch[r][0]);
        erase(ch[r][1]);
    }
}
void Delete(int x)
{
    Splay(get_kth(root,pos+1),0);
    Splay(get_kth(root,pos+2+x),root);
    erase(Key_value);
    pre[Key_value]=0;
    Key_value=0;
    pushup(ch[root][1]);
    pushup(root);
}
void Rotate(int x)
{
    Splay(get_kth(root,pos+1),0);
    Splay(get_kth(root,pos+x+2),root);
    update_rev(Key_value);
    pushup(ch[root][1]);
    pushup(root);
}
void Get()
{
    printf("%c\n",key[get_kth(root,pos+2)]);
}
void Prev()
{
    pos--;
}
void Next()
{
    pos++;
}
int main()
{
    char str1[20];
    int x;
    while(scanf("%d",&N)!=EOF)
    {
        init();
        while(N--)
        {
            scanf("%s",str1);
            if(!strcmp(str1,"Move"))
            {
                scanf("%d",&x);
                pos=x;
            }
            else if(!strcmp(str1,"Insert"))
            {
                scanf("%d",&x);
                getchar();
                gets(str);
                Insert(x);
            }
            else if(!strcmp(str1,"Delete"))
            {
                scanf("%d",&x);
                Delete(x);
            }
            else if(!strcmp(str1,"Rotate"))
            {
                scanf("%d",&x);
                Rotate(x);
            }
            else if(!strcmp(str1,"Get"))Get();
            else if(!strcmp(str1,"Prev"))Prev();
            else Next();
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值