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

TLE代码存档

时间:2017-07-16 22:37:35      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:upd   log   roo   代码   bsp   insert   dig   else   void   

#include <ctype.h>
#include <cstdio>
#define N 200010

void read(int &x)
{
    x=0;
    char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) {x=x*10+ch-0;ch=getchar();} 
}
int M;

struct Tree
{
    int Fre,size,dis,fa;
    int child[2];
}tr[N];
int cnt,root;
int get_son(int x) {return tr[tr[x].fa].child[1]==x;} 
void update(int x)
{
    tr[x].size=tr[x].Fre;
    if(tr[x].child[0])
        tr[x].size+=tr[tr[x].child[0]].size;
    if(tr[x].child[1])
        tr[x].size+=tr[tr[x].child[1]].size;
}
void rotate(int x)
{
    int fa=tr[x].fa;
    int grand=tr[fa].fa;
    int pos=get_son(x);
    tr[fa].child[pos]=tr[x].child[pos^1];
    tr[tr[fa].child [pos]].fa=fa;
    tr[x].child [pos^1]=fa;
    tr[fa].fa=x;
    tr[x].fa=grand;
    if(grand)
        tr[grand].child[tr[grand].child[1]==fa]=x;
    update(fa);
    update(x);
}
void splay(int x)
{
    for(int fa;fa=tr[x].fa;rotate(x))
        if(tr[fa].fa)
            rotate(get_son(x)==get_son(fa)?fa:x);
    root=x;
}
void insert(int x)
{
    if(!root)
    {
        cnt++;
        tr[cnt].dis=x;
        tr[cnt].size=1;
        tr[cnt].Fre=1;
        root=cnt;
        return;
    }
    int fa=0,now=root;
    while(true)
    {
        if(tr[now].dis==x)
        {
            tr[now].dis++;
            tr[now].Fre++;
            splay(now);
            return;
        }
        fa=now;
        now=tr[now].child[x>tr[fa].dis];
        if(!now)
        {
            cnt++;
            tr[fa].child[x>tr[fa].dis]=cnt;
            tr[cnt].fa=fa;
            tr[cnt].dis=x;
            tr[cnt].size=1;
            tr[cnt].Fre=1;
            splay(cnt);
            return;
        }
    }
}
int get_xth(int x)
{
    int now=root;
    while(true)
    {
        if(tr[now].child[0]&&x<=tr[tr[now].child[0]].size)
        {
            now=tr[now].child[0];
            continue;
        }
        int tmp=(tr[now].child[0]?tr[tr[now].child[0]].size:0)+tr[now].Fre;
        if(x<=tmp) return tr[now].dis;
        x-=tmp;
        now=tr[now].child[1];
    }
}
int get_rank(int x)
{
    int now=root;
    int ans=0;
    while(true)
    {
        if(x<tr[now].dis)
        {
            now=tr[now].child[0];
            continue;
        }
        ans+=tr[now].child[0]?tr[tr[now].child[0]].size:0;
        if(tr[now].dis==x)
        {
            splay(now);
            return ans+1;
        }
        ans+=tr[now].Fre;
        now=tr[now].child[1];
    }
}
int find_suffix()
{
    int now=tr[root].child[1];
    while(tr[now].child[0])
        now=tr[now].child[0];
    return now;
}
void clear(int x)
{
    tr[x].child[1]=0;
    tr[x].child[1]=1;
    tr[x].size=0;
    tr[x].Fre=0;
    tr[x].dis=0;
    tr[x].fa=0;
}
int get_value(int x)
{
    return tr[x].dis;
}
int find_prefix()
{
    int now=tr[root].child[0];
    while(tr[now].child[1])
        now=tr[now].child[1];
    return now;
}
void Delete(int x)
{
    get_rank(x);
    if(tr[root].Fre>1)
    {
        tr[root].Fre--;
        tr[root].size--;
        return;
    }
    if(!tr[root].child[0]&&!tr[root].child[1])
    {
        clear(root);
        root=0;
        return;
    }
    if(!tr[root].child[0])
    {
        int tmp=root;
        root=tr[root].child[1];
        tr[root].fa=0;
        clear(tmp);
        return;
    }
    if(!tr[root].child[1])
    {
        int tmp=root;
        root=tr[root].child[0];
        tr[root].fa=0;
        clear(tmp);
        return;
    }
    int prefix=find_prefix();
    int tmp=root;
    splay(prefix);
    tr[root].child[1]=tr[tmp].child[1];
    tr[tr[tmp].child[1]].fa=root;
    clear(tmp);
    update(root);
}
int main(int argc,char *argv[])
{
    read(M);
    int opt,x;
    for(;M--;)
    {
        read(opt);
        read(x);
        if(opt==1)
            insert(x);
        else if(opt==2)
            Delete(x);
        else if(opt==3)
            printf("%d\n",get_rank(x));
        else if(opt==4)
            printf("%d\n",get_xth(x));
        else if(opt==5)
        {
            insert(x);
            printf("%d\n",get_value(find_prefix()));
            Delete(x);
        }
        else 
        {
            insert(x);
            printf("%d\n",get_value(find_suffix()));
            Delete(x);
        }
    }
    return 0;
}

 

TLE代码存档

标签:upd   log   roo   代码   bsp   insert   dig   else   void   

原文地址:http://www.cnblogs.com/ruojisun/p/7192095.html

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