hdu 3487

splay

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std;

int son[maxn][2],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[maxn];
int n,m;

void push_up(int rt)
{
    size[rt]=size[lch(rt)]+size[rch(rt)]+1;
}

void push_down(int rt)
{
    if(flg[rt])
    {
        int tmp=rch(rt);
        rch(rt)=lch(rt);
        lch(rt)=tmp;
        if(rch(rt))
            flg[rch(rt)]^=1;
        if(lch(rt))
            flg[lch(rt)]^=1;
        flg[rt]=0;
    }
}

void newnode(int &rt,int f,int v)
{
    rt=++cnt;
    rch(rt)=lch(rt)=0;
    fa[rt]=f;
    val[rt]=v;
    size[rt]=1;
    flg[rt]=0;
}

void build(int l,int r,int &rt,int f)
{
    if(l>r)return;
    int m=(l+r)>>1;
    newnode(rt,f,num[m]);
    build(l,m-1,lch(rt),rt);
    build(m+1,r,rch(rt),rt);
    push_up(rt);
}

void ini()
{
    lch(0)=rch(0)=0;
    fa[0]=size[0]=val[0]=0;
    root=0;
    cnt=0;
    newnode(root,0,0);
    newnode(rch(root),root,n+1);
    build(1,n,lch(rch(root)),rch(root));
    push_up(rch(root));
    push_up(root);
}

void rotate(int x,int kind)//0->left,1->right
{
    push_down(x);
    int y=fa[x];
    son[y][kind^1]=son[x][kind];
    fa[son[x][kind]]=y;
    if(fa[y])
        son[fa[y]][son[fa[y]][1]==y]=x;
    fa[x]=fa[y];
    son[x][kind]=y;
    fa[y]=x;
    push_up(y);
}

void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
    if(rt!=goal)
    {
        push_down(rt);
        while(fa[rt]!=goal)
        {
            if(lch(fa[rt])==rt)
                rotate(rt,1);
            else rotate(rt,0);
        }
        push_up(rt);
        if(!goal)root=rt;
    }
}

int select(int k)
{
    int rt=root;
    push_down(rt);
    while(size[lch(rt)]+1!=k)
    {
        if(size[lch(rt)]+1>=k)
            rt=lch(rt);
        else
        {
            k-=(size[lch(rt)]+1);
            rt=rch(rt);
        }
        push_down(rt);//不加就超时;
    }
    return rt;
}

void cut(int a,int b,int c)
{
    a=select(a-1);
    splay(a,0);
    b=select(b+1);
    splay(b,a);
    int res=lch(b);
    lch(b)=0;
    push_up(b);push_up(a);
    a=select(c);b=select(c+1);
    splay(a,0);splay(b,a);
    lch(b)=res;
    fa[res]=b;
}

void flip(int a,int b)
{
    a=select(a-1);
    splay(a,0);
    b=select(b+1);
    splay(b,a);
    flg[lch(b)]^=1;
}

int cot=1;
void dfs(int rt)
{
    push_down(rt);
    if(lch(rt))
        dfs(lch(rt));
    if(val[rt]>0&&val[rt]<=n&&cot<n)
    {
        printf("%d ",val[rt]);
        cot++;
    }
    else if(val[rt]>0&&val[rt]<=n&&cot==n)
    {
        printf("%d",val[rt]);
    }
    if(rch(rt))
        dfs(rch(rt));
}

char s[10];

int main()
{
    for(int i=1; i<maxn; i++)
        num[i]=i;
    while(scanf("%d%d",&n,&m)&&n>0)
    {

        ini();
        int a,b,c;
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                cut(a+1,b+1,c+1);
            }
            else
            {
                scanf("%d%d",&a,&b);
                flip(a+1,b+1);
            }
        }
        cot=1;
        a=select(2);b=select(n+1);
        splay(a,0);splay(b,a);
        dfs(root);
        puts("");
    }
    return 0;
}
View Code

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std;

int son[maxn][2],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[maxn];
int n,m;

void push_up(int rt)
{
    size[rt]=size[lch(rt)]+size[rch(rt)]+1;
}

void push_down(int rt)
{
    if(flg[rt])
    {
        int tmp=rch(rt);
        rch(rt)=lch(rt);
        lch(rt)=tmp;
        flg[rch(rt)]^=1;
        flg[lch(rt)]^=1;
        flg[rt]=0;
    }
}

void newnode(int &rt,int f,int v)
{
    rt=++cnt;
    rch(rt)=lch(rt)=0;
    fa[rt]=f;
    val[rt]=v;
    size[rt]=1;
    flg[rt]=0;
}

void build(int l,int r,int &rt,int f)
{
    if(l>r)return;
    int m=(l+r)>>1;
    newnode(rt,f,num[m]);
    build(l,m-1,lch(rt),rt);
    build(m+1,r,rch(rt),rt);
    push_up(rt);
}

void rotate(int x,int kind)//0->left,1->right
{
    push_down(x);
    int y=fa[x];
    son[y][kind^1]=son[x][kind];
    fa[son[x][kind]]=y;
    if(fa[y])
        son[fa[y]][son[fa[y]][1]==y]=x;
    fa[x]=fa[y];
    son[x][kind]=y;
    fa[y]=x;
    push_up(y);
}

void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
    push_down(rt);
    while(fa[rt]!=goal)
    {
        int y=fa[rt];
        if(fa[y]==goal)
            rotate(rt,son[y][0]==rt);
        else
        {
            int kind=son[fa[y]][0]==y;
            if(son[y][kind]==rt)
            {
                rotate(rt,kind^1);
                rotate(rt,kind);
            }
            else
            {
                rotate(y,kind);
                rotate(rt,kind);
            }
        }
    }
    push_up(rt);
    if(goal==0) root=rt;
}

int select(int k)
{
    int rt=root;
    while(size[lch(rt)]!=k)
    {
        if(size[lch(rt)]>k)
            rt=lch(rt);
        else
        {
            k-=(size[lch(rt)]+1);
            rt=rch(rt);
        }
        push_down(rt);
    }
    return rt;
}

void rotate_to(int k,int goal)//将第k节点旋转到goal的右儿子节点;
{
    int rt=root;
    rt=select(k);
    splay(rt,goal);
}

void cut(int a,int b,int c)
{
    rotate_to(a-1,0);
    rotate_to(b+1,root);
    int x=rch(root);
    int tmp=lch(x);
    lch(x)=0;
    push_up(x);
    push_up(root);
    rotate_to(c,0);
    rotate_to(c+1,root);
    fa[tmp]=rch(root);
    lch(rch(root))=tmp;
    push_up(rch(root));
    push_up(root);
}

void flip(int a,int b)
{
    rotate_to(a-1,0);
    rotate_to(b+1,root);
    flg[lch(rch(root))]^=1;
}

void dfs(int rt)
{
    push_down(rt);
    if(lch(rt))
        dfs(lch(rt));
    if(val[rt]!=0)
    printf(" %d",val[rt]);
    if(rch(rt))
        dfs(rch(rt));
}

void ini()
{
    lch(0)=rch(0)=0;
    fa[0]=size[0]=0;
    root=0;
    cnt=0;
    newnode(root,0,0);
    newnode(rch(root),root,0);
    push_up(root);
    build(1,n,lch(rch(root)),rch(root));
    push_up(rch(root));
    push_up(root);
}

char s[10];

int main()
{
    while(scanf("%d%d",&n,&m)&&n>0)
    {
        for(int i=1; i<=n; i++)
            num[i]=i;
        ini();
        int a,b,c;
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                cut(a,b,c);
//                rotate_to(1,0);
//                printf("%d",val[root]);
//                dfs(rch(root));
//                puts("");
            }
            else
            {
                scanf("%d%d",&a,&b);
                flip(a,b);
            }
        }
        rotate_to(1,0);
        printf("%d",val[root]);
        dfs(rch(root));
        puts("");
    }
    return 0;
}
View Code

 

posted @ 2014-03-13 21:53  Yours1103  阅读(149)  评论(0编辑  收藏  举报