zoukankan      html  css  js  c++  java
  • 新鞋

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    #include<stdlib.h>
    struct tree{
        int key;//域值 
        int l; int lm;//左编号 左长 
        int r;    int rm;
        int sum; 
        int f;//编号 
        int times;
    }a[999999];
    int root=0,tot=0;
    void turnleft(int now)
    {
        if(a[now].f ==root)
            root=now;    
        int ff= a[a[now].f ].f ;
        int fa=a[now].f;
        a[a[now].f ].f =now;
        a[now].f=ff;
        if(ff!=0)
        {
            if(a[ff].l ==a[now].f )        
            a[ff].l=now;
            if(a[ff].r==a[now].f)
            a[ff].r=now;
        }
        if(a[now].l!=0)
            a[a[now].l].f=fa;
        a[fa].r=a[now].l;
        a[now].l=fa;    
        //以下是子树大小 
        a[fa].rm=a[now].lm;
        a[now].lm+=a[fa].lm+1 ;    
    }
    void turnright(int now)
    {
        if(a[now].f==root)
            root=now;
        int fa=a[now].f;
        int ff=a[fa].f;
        a[fa].f=now;
        a[now].f=ff;
        if(ff!=0)
        {
            if(a[ff].l==fa)
                a[ff].l=now;
            if(a[ff].r ==fa)
                a[ff].r=now; 
        }
        if(a[now].r!=0)
            a[a[now].r].f=fa;
        a[fa].l=a[now].r;
        a[now].r=fa;
        a[fa].lm=a[now].rm;
        a[now].rm+=a[fa].rm+1;
    }
    void add(int x)
    {
        ++tot;
        a[tot].key =x;
        a[tot].sum=rand()%1000050;
        a[tot].times=1;
        if(tot==1)
        {
            root=tot;
            return ;
        }
        int pre=root,now=root;
        while(now)
        {
            if(x==a[now].key )
            {
                a[now].times++;
                return;//有重复元素 
            }
            if(x<a[now].key )
            {
                a[now].lm++;
                pre=now;
                now=a[now].l;            
                continue;
            }
            if(x>a[now].key )
            {
                a[now].rm++;
                pre=now;
                now=a[now].r;
                continue;
            }
        }
        if(x<a[pre].key )
            a[pre].l=now;
        if(x>a[pre].key )
            a[pre].r=now;
        a[now].f=pre;    now=tot; 
        while(a[now].sum<a[a[now].f].sum)
        {
            if(a[a[now].f ].l==now)
            {
                turnright(now);
                continue;
            }
            if(a[a[now].f].r==now)
            {
                turnleft(now);
                continue;
            }
        }
    }
    /*
    void add(int x,int now)
    {
        
        if(a[now].lm ==0&&x<a[now].key )
        {
            a[++tot].key=x;
            a[now].l=tot;
            a[tot].f=now;
            a[now].lm++;
            return ;
        }
        if(a[now].rm ==0&&x>=a[now].key )
        {
            a[++tot].key=x;
            a[now].r=tot;
            a[tot].f=now;
            a[now].rm++;
            return ;
        }    
        //if(x==a[now].key)    return ;
        if(x<a[now].key)    add(x,a[now].l),a[now].lm++;else
        if(x>=a[now].key)    add(x,a[now].r),a[now].rm++;
        return ;
    }
    */
    int findw(int x,int now)
    {
        if(a[now].key==x)    return now;    
        if(x<a[now].key)    return    findw(x,a[now].l);
        if(x>a[now].key)    return findw(x,a[now].r);
    }
    void balance()
    {
        while(a[root].lm >a[root].rm+tot/4)
        {
            a[root].f=a[root].l;
            a[a[root].l ].r=root;
            root=a[root].l;
        }
        while(a[root].lm <=a[root].rm+tot/4)
        {
            a[root].f=a[root].r;
            a[a[root].r ].l=root;
            root=a[root].r;
        }    
        return ;
    } 
    /*
    void delet(int x)
    {
        int now=findw(x,root);
        if(now==root)
        {
            if(a[now].lm >a[now].rm )
            {
                
                a[a[root].l].r=a[root].r;
                root=a[root].l;
            }
        }
    }
    */
    void delet(int x)
    {
        int now=findw(x,root);
        while(!a[now].l||!a[now].r)
        {
            if(a[now].l&&a[now].r==0)
            {
                turnright(a[now].l);
                continue;
            }
            if(a[now].r==0&&a[now].r)
            {
                turnleft(a[now].r);
                continue;
            }
            if(a[now].l&&a[now].r)
            {
                if(a[a[now].l].sum<a[a[now].r].sum)
                turnright(a[now].l);
                else turnleft(a[now].r);
            }
            if(a[now].f)
            {
                if(a[a[now].f].l==now)
                    a[a[now].f].l=0;
                if(a[a[now].f].r==now)
                    a[a[now].f].r=0;
            }
        }
        return ;
    }
    int find3(int x,int now)
    {
        if(a[now].lm ==x-1)    return a[now].key ;
        if(a[now].lm >x-1)    return find3(x,a[now].l );
        if(a[now].lm<x-1) return find3(x-a[now].lm-1,a[now].r );
    }
    int find4(int x,int now)
    {
        if(a[now].key==x)    return a[now].lm+1;
        if(x<a[now].key)    return    find4(x,a[now].l);
        if(x>a[now].key)    return find4(x,a[now].r)+a[now].lm+1;
    }
    int find5(int x,int now)
    {
        int w=a[now].key ;
        if(w==x)    return w;
        if(w>x)    
        {
            if(a[now].lm ==0)    return w;
            return find5(x,a[now].l );
        }
        if(w<x)
        {
            if(a[now].rm ==0)    return a[a[now].f ].key;
            return find5(x,a[now].r );
        }
    }
    int find6(int x)
    {
        int now=findw(x,root);    
        if(a[now].lm!=0)
            return a[a[now].l].key;
        while(a[now].key >x)
        {
            now=a[now].f ;
        }
        return a[now].key ;
    }
    int find7(int x)
    {
        int now=findw(x,root);
        if(a[now].rm!=0)
        return a[a[now].r].key;
        while(a[now].key <x)
        {
            now=a[now].f ;
        }
        return a[now].key ;
    }
    
    int main() 
    {
        int p,x,n;
        int pp[20],z[20];
        root=0,tot=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        scanf("%d%d",&pp[i],&z[i]);    
        for(int i=1;i<=n;i++)
        {
        
            p=pp[i];x=z[i];    
            if(p==1)    add(x);
            if(p==3)    printf("%d
    ",find3(x,root));//找第K小值 
            if(p==4)    printf("%d
    ",find4(x,root));
            if(p==5)    printf("%d
    ",find5(x,root));
            if(p==6)    printf("%d
    ",find6(x));        
            if(p==7)    printf("%d
    ",find7(x));
        }    
        return 0;
    }     
  • 相关阅读:
    Sqlserver的Transaction做Rollback的时候要小心(转载)
    注意Sqlserver中使用with(nolock)后实际上还是会加架构锁,只是不对要查询的数据加S锁而已(转载)
    为什么Sql Server的查询有时候第一次执行很慢,第二次,第三次执行就变快了
    Sql Server 中如果使用TransactionScope开启一个分布式事务,使用该事务两个并发的连接会互相死锁吗
    Css中路径data:image/png;base64的用法详解 (转载)
    android获取mp4视频文件总时长和视频宽高<转>
    “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)instead”
    android 除法运算保留小数点
    Directshow 采集音视频数据H264+AAC+rtmp效果还不错
    VS2010中将CString转换为const char*
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/6836504.html
Copyright © 2011-2022 走看看