bzoj 1269: [AHOI2006]文本编辑器editor

Posted lxy8584099

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj 1269: [AHOI2006]文本编辑器editor相关的知识,希望对你有一定的参考价值。

爆空间的时候返回给我TLE???   darkbzoj判断还是多了换行符??? 。。。

/**************************************************************
    Problem: 1269
    User: lxy8584099
    Language: C++
    Result: Accepted
    Time:2544 ms
    Memory:45712 kb
****************************************************************/
 
#include<queue>
#include<cstdio>
#include<iostream>
#include<cstring>
#define lc (c[x][0])
#define rc (c[x][1])
#define inf (2000000)
using namespace std;
const int N=2e6+50;
int c[N][2],fa[N],size[N],root,tot=2,gb;
char val[N],s[N]; bool turn[N];
queue <int> q;
void pushup(int x) {size[x]=size[lc]+size[rc]+1;} 
void pushdown(int x)
{
    if(!turn[x]) return ; turn[x]^=1,turn[lc]^=1,turn[rc]^=1;
    swap(c[lc][0],c[lc][1]); swap(c[rc][0],c[rc][1]);
}
void rotate(int x)
{
    int y=fa[x],z=fa[y],sx=c[y][1]==x;
    int sy=c[z][1]==y,w=c[x][sx^1];
    fa[w]=y,c[y][sx]=w;fa[y]=x,c[x][sx^1]=y;
    fa[x]=z,c[z][sy]=x;pushup(y),pushup(x);
}
void splay(int x,int goal)
{
    if(x==goal) return ; while(fa[x]!=goal)
    {
        int y=fa[x],z=fa[y]; if(z!=goal)
            rotate((c[y][1]==x)^(c[z][1]==y)?x:y); rotate(x);
    } if(goal==0) root=x;
}
int newp()
{
    if(!q.empty()) {int x=q.front();q.pop();return x;} return ++tot;
}
int build(int l,int r)
{
    if(l>r) return 0; int mid=(l+r)>>1,x=newp();
    fa[lc=build(l,mid-1)]=fa[rc=build(mid+1,r)]=x;
    val[x]=s[mid];size[x]=1;turn[x]=0;pushup(x);return x;
}
int find(int k)
{
    int x=root; while(x)
    {
        pushdown(x);if(size[lc]+1==k) return x;
        if(k<=size[lc]) x=lc; else k-=(size[lc]+1),x=rc;
    }
}
int work(int l,int r)
{
    int L=find(l),R=find(r+2);splay(L,0),splay(R,L); return R;
}
void insert(int len)
{
    int x=build(0,len-1),pl=work(gb+1,gb);c[fa[x]=pl][0]=x,splay(x,0);
}
void recycle(int x)
{
    if(lc) recycle(lc);q.push(x);if(rc) recycle(rc);
}
void erase(int len)
{
    int pl=work(gb+1,gb+len);recycle(c[pl][0]);
    c[pl][0]=fa[c[pl][0]]=0;pushup(pl);
}
void change(int len)
{
    int pl=work(gb+1,gb+len),x=c[pl][0];turn[x]^=1;swap(lc,rc);
}
int main()
{
//freopen("3.in","r",stdin);
//freopen("my.out","w",stdout);
    val[1]=val[2]=#;fa[2]=1;c[1][1]=2;
    size[1]=2;size[2]=1;root=1;gb=0;
    int T;scanf("%d",&T);while(T--)
    {
//      printf("    T:%d
",T);
        char ch[10]; int x; scanf("%s",ch);
        if(ch[0]==M) scanf("%d",&x),gb=x;
        if(ch[0]==I) 
        {
            scanf("%d",&x); cin.getline(s,x+5);
            cin.getline(s,x+5); insert(x);
        }
        if(ch[0]==D) scanf("%d",&x),erase(x);
        if(ch[0]==R) scanf("%d",&x),change(x);
        if(ch[0]==G) printf("%c
",val[find(gb+2)]);
        if(ch[0]==P) gb--; if(ch[0]==N) gb++;
//      dfs(root);printf("  gb:%d
",gb);
    }
    return 0;
}

 

以上是关于bzoj 1269: [AHOI2006]文本编辑器editor的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 1269: [AHOI2006]文本编辑器editor

BZOJ 1269 AHOI2006 文本编辑器editor

[bzoj1269][AHOI2006文本编辑器editor] (splay模版题)

BZOJ 1269 [AHOI2006]文本编辑器editor

1269: [AHOI2006]文本编辑器editor

1269: [AHOI2006]文本编辑器editor