码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-28 18:06:21      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:etl   stdin   push   ace   shu   val   style   wap   printf   

爆空间的时候返回给我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\n",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\n",val[find(gb+2)]);
        if(ch[0]==P) gb--; if(ch[0]==N) gb++;
//      dfs(root);printf("  gb:%d\n",gb);
    }
    return 0;
}

 

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

标签:etl   stdin   push   ace   shu   val   style   wap   printf   

原文地址:https://www.cnblogs.com/lxy8584099/p/10451964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!