BZOJ 1861 书架

Posted on 2016-08-04 17:05  ziliuziliu  阅读(142)  评论(0编辑  收藏  举报

(╯-_-)╯╧╧

此处为错误代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 300050
#define inf 2147483646
using namespace std;
int n,m,a[maxn],x,y;
int tree[maxn][3],size[maxn],fath[maxn],val[maxn],root,hash[maxn],rr=0;
char s[10];
void pushup(int now)
{
    size[now]=size[tree[now][1]]+size[tree[now][2]]+1;
}
void reset(int x)
{
    fath[x]=tree[x][1]=tree[x][2]=0;
    size[x]=1;
}
void sets(int x,int y)
{
    swap(fath[x],fath[y]);
    if (fath[x]==y)
    {
        fath[x]=fath[y];fath[y]=x;
        
    }
}
void build(int &now,int left,int right,int father)
{
    if (left>right) return;
    int mid=(left+right)>>1;now=mid;
    fath[now]=father;val[now]=a[mid];
    build(tree[now][1],left,mid-1,now);
    build(tree[now][2],mid+1,right,now);
    pushup(now);
}
void rotate(int x,int &k)
{
    int y=fath[x],z=fath[y],l,r;
    if (tree[y][1]==x) l=1;else l=2;
    r=3-l;
    if (y==k) k=x;
    else
    {
        if (tree[z][1]==y) tree[z][1]=x;
        else tree[z][2]=x;
    }
    fath[x]=z;fath[y]=x;fath[tree[x][r]]=y;
    tree[y][l]=tree[x][r];tree[x][r]=y;
    pushup(y);pushup(x);
}
void splay(int x,int &k)
{
    while (x!=k)
    {
        int y=fath[x],z=fath[y];
        if (y!=k)
        {
            if ((tree[y][1]==x)^(tree[z][1]==y)) rotate(x,k);
            else rotate(y,k);
        }
        rotate(x,k);
    }
}
int get_pre(int x)
{
    int ret=tree[x][1];
    while (tree[ret][2])
        ret=tree[ret][2];
    return ret;
}
int get_sub(int x)
{
    int ret=tree[x][2];
    while (tree[ret][1])
        ret=tree[ret][1];
    return ret;
}
void delete_(int x)
{
    splay(x,root);
    int pre=get_pre(root);
    int ls=tree[x][1],rs=tree[x][2];
    root=ls;fath[ls]=fath[rs]=0;splay(pre,root);
    tree[root][2]=rs;fath[rs]=root;pushup(root);
    reset(x);
}
int find_kth(int now,int x)
{
    int r=size[tree[now][1]];
    if (x<=r) return find_kth(tree[now][1],x);
    else if (x>r+1) return find_kth(tree[now][2],x-r-1);
    else 
    {
        rr=now;
        return now;
    }
}
int get_rank(int x)
{
    splay(x,root);
    return size[tree[root][1]];
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&a[i+1]);
        hash[a[i+1]]=i+1;
    }
    a[1]=n+1;a[n+2]=n+2;
    build(root,1,n+2,0);
    for (int i=1;i<=m;i++)
    {
        scanf("%s",s);
        if (s[0]=='T')
        {
            scanf("%d",&x);
            delete_(hash[x]);
            y=find_kth(root,1);
            splay(y,root);
            int rs=tree[root][2];
            reset(root);
            fath[root]=hash[x];fath[rs]=hash[x];tree[hash[x]][1]=root;tree[hash[x]][2]=rs;pushup(hash[x]);
            root=hash[x];
            splay((n+3)>>1,root);
        }
        else if (s[0]=='B')
        {
            scanf("%d",&x);
            delete_(hash[x]);
            y=find_kth(root,n+1);
            splay(y,root);
            int ls=tree[root][1];
            reset(root);
            fath[root]=hash[x];fath[ls]=hash[x];tree[hash[x]][2]=root;tree[hash[x]][1]=ls;pushup(hash[x]);
            root=hash[x];
            splay((n+3)>>1,root);
        }
        else if (s[0]=='I')
        {
            scanf("%d%d",&x,&y);
            if (y==0) continue;
            else if (y==-1) 
            {
                int pre=get_pre(hash[x]);
                sets(pre,hash[x]);
            }
            else
            {
                int sub=get_sub(hash[x]);
                sets(hash[x],sub);
            }
            splay(hash[x],root);
        }
        else if (s[0]=='A')
        {
            scanf("%d",&x);
            printf("%d\n",get_rank(hash[x])-1);
            splay(hash[x],root);
    }
        else
        {
            rr=0;
            scanf("%d",&x);
            printf("%d\n",val[find_kth(root,x+1)]);
            splay(rr,root);
        }
    }
    return 0;
}