zoukankan      html  css  js  c++  java
  • splay poj3481

    三种操作  

    1 k  p  插入一个点

    2 输出p最大对应的k 删除这个点

    3  输出p最小对应的k 删除这个点

    splay  维护一下 一不小心就会超时 

    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    #include<string.h>
    
    using namespace std;
    #define inf 1000000000
    #define MAXN 1000100
    
    int pa[MAXN],ch[MAXN][2],ki[MAXN],pi[MAXN];
    int root,cnt;
    
    void Rotate(int x,int kind)
    {
        int y=pa[x];
        int z=pa[y];
        ch[y][!kind]=ch[x][kind];
        pa[ch[x][kind]]=y;
        if(pa[y])ch[pa[y]][ch[pa[y]][1]==y]=x;
        pa[x]=pa[y];
        ch[x][kind]=y;
        pa[y]=x;
    }
    void splay(int x,int goal)
    {
        while(pa[x]!=goal)
        {
            if(pa[pa[x]]==goal)
            {
                Rotate(x,ch[pa[x]][0]==x);
            }
            else
            {
                int y=pa[x];
                int kind=ch[pa[y]][0]==y;
                if(ch[y][kind]==x)
                {
                    Rotate(x,!kind);
                    Rotate(x,kind);
                }
                else
                {
                    Rotate(y,kind);
                    Rotate(x,kind);
                }
            }
        }
        if(goal==0)root=x;
    }
    
    void newnode(int &x,int k,int p,int father)
    {
        x=++cnt;
        pa[x]=father;
        ch[x][0]=ch[x][1]=0;
        pi[x]=p;
        ki[x]=k;
    }
    void Insert(int k,int p)
    {
        int rt=root;
        int r=root;
        while(rt!=0)
        {
            r=rt;
            if(pi[rt]<p)rt=ch[rt][1];
            else rt=ch[rt][0];
        }
        newnode(ch[r][pi[r]<p],k,p,r);
        splay(ch[r][pi[r]<p],root);
        ch[0][0]=ch[0][1]=0;
    }
    
    int f_max(int x)
    {
        while(ch[x][1])
            x=ch[x][1];
        if(x==root)
            root=ch[x][0];
        ch[pa[x]][1]=ch[x][0];
        pa[ch[x][0]]=pa[x];
        return ki[x];
    }
    int f_min(int x)
    {
        while(ch[x][0])
            x=ch[x][0];
        if(x==root)
            root=ch[x][1];
        ch[pa[x]][0]=ch[x][1];
        pa[ch[x][1]]=pa[x];
        return ki[x];
    }
    int main()
    {
        root=cnt=0;
        int n;
    
        while(scanf("%d",&n)!=EOF&&n)
        {
            if(n==1)
            {
                int k,p;
                scanf("%d%d",&k,&p);
                Insert(k,p);
            }
            else if(n==2)
                printf("%d
    ",f_max(root));
            else
                printf("%d
    ",f_min(root));
        }
    
        return 0;
    }
  • 相关阅读:
    [POJ] 食物链
    [POJ] Palindrome
    [POJ] The Triangle
    [Cpp primer] Library vector Type
    Shift Operations on C
    Masking operations
    [CSAPP] The Unicode Standard for text coding
    [Cpp primer] Library string Type
    [Cpp primer] range for (c++11)
    [Cpp primer] Namespace using Declarations
  • 原文地址:https://www.cnblogs.com/cherryMJY/p/6117648.html
Copyright © 2011-2022 走看看