• Treap模板


    非指针Treap

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define INF 0x3f3f3f3f
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    #define pqueue priority_queue
    #define NEW(a,b) memset(a,b,sizeof(a))
    const double pi=4.0*atan(1.0);
    const double e=exp(1.0);
    const int maxn=3e6+8;
    typedef long long LL;
    typedef unsigned long long ULL;
    //typedef pair<LL,LL> P;
    const LL mod=1e9+7;
    const ULL base=1e7+7;
    using namespace std;
    struct node{
        int son[2];
        int siz;
        int key,w;
    }a[100008];
    int tot=0;
    int root=0;
    void up(int i){
        a[i].siz=a[a[i].son[0]].siz+a[a[i].son[1]].siz+1;
    }
    void Rotate(int &i,int d){
        int t=a[i].son[d];
        a[i].son[d]=a[t].son[!d];
        a[t].son[!d]=i;
        up(i);up(t);
        i=t;
    }
    void Insert(int &i,int key){
        if(i==0){
            i=++tot;
            a[i].siz=1;a[i].key=key;a[i].w=rand();
            return ;
        }
        a[i].siz++;
        if(a[i].key>=key) {
            Insert(a[i].son[0],key);
            if(a[a[i].son[0]].w<a[i].w) Rotate(i,0);
        }
        else{
            Insert(a[i].son[1],key);
            if(a[a[i].son[1]].w<a[i].w) Rotate(i,1);
        }
    }
    void Del(int &i,int key){
        if(a[i].key==key){
            if(a[i].son[0]*a[i].son[1]==0)  {i=a[i].son[0]+a[i].son[1];return ;}
            if(a[a[i].son[0]].w>a[a[i].son[1]].w){
                Rotate(i,1);
                Del(a[i].son[0],key);
            }
            else{
                Rotate(i,0);
                Del(a[i].son[1],key);
            }
        }
        else if(a[i].key>key){
            Del(a[i].son[0],key);
        }
        else{
            Del(a[i].son[1],key);
        }
        up(i);
    }
    int Find(int i,int key){
        if(i==0) return 1;
        if(a[i].key>=key) return Find(a[i].son[0],key);
        else return a[a[i].son[0]].siz+Find(a[i].son[1],key)+1;
    }
    int Search(int i,int rak){
        if(a[a[i].son[0]].siz==rak-1) return a[i].key;
        if(a[a[i].son[0]].siz>=rak) return Search(a[i].son[0],rak);
        else  return Search(a[i].son[1],rak-a[a[i].son[0]].siz-1);
    }
    int pre(int i,int key){
        if(i==0) return -10000008;
        if(a[i].key<key) return max(a[i].key,pre(a[i].son[1],key));
        return pre(a[i].son[0],key);
    }
    int bhe(int i,int key){
        if(i==0) return 10000008;
        if(a[i].key>key) return min(a[i].key,bhe(a[i].son[0],key));
        return bhe(a[i].son[1],key);
    }
    int main(){
        int n;
        scanf("%d",&n);
        int opt,x;
        while(n--){
            scanf("%d%d",&opt,&x);
            if(opt==1){
                Insert(root,x);
            }
            if(opt==2){
                Del(root,x);
            }
            if(opt==3){
                printf("%d
    ",Find(root,x));
            }
            if(opt==4){
                printf("%d
    ",Search(root,x));
            }
            if(opt==5){
                printf("%d
    ",pre(root,x));
            }
            if(opt==6){
                printf("%d
    ",bhe(root,x));
            }
        }
    }
  • 相关阅读:
    ASP.NET常用的三十三种代码
    asp.net获取IP地址
    Inside Microsoft Sql Server 2005 TSQL Programming 学习笔记
    动态SQL与SQL注入(一)动态SQL
    (二)SQL 注入
    WCF 安全
    C# 运算符重载和 implicit关键字
    分页那回事
    thinking
    Moss css
  • 原文地址:https://www.cnblogs.com/Profish/p/9395460.html
Copyright © 2020-2023  润新知