无旋Treap

Posted baka

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无旋Treap相关的知识,希望对你有一定的参考价值。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;
const int maxn=100100,inf=0x7fffffff;
struct Treap
{
    Treap* ch[2];
    int key,val,size;
    Treap(int v)
        {size=1,val=v,key=rand();ch[0]=ch[1]=NULL;}
    inline void tain()
        {size=1+(ch[0]?ch[0]->size:0)+(ch[1]?ch[1]->size:0);}
}*root;
typedef pair<Treap*,Treap*> D;
inline int size(Treap *o){return o?o->size:0;}
Treap *Merge(Treap *a,Treap* b)
{
    if(!a)return b;
    if(!b)return a;
    if(a->key < b->key)
        {a->ch[1]=Merge(a->ch[1],b);a->tain();return a;}
    else
        {b->ch[0]=Merge(a,b->ch[0]);b->tain();return b;}
}
D Split(Treap *o,int k)
{
    if(!o)return D(NULL,NULL);
    D y;
    if(size(o->ch[0])>=k)
        {y=Split(o->ch[0],k);o->ch[0]=y.second;o->tain();y.second=o;}
    else
        {y=Split(o->ch[1],k-size(o->ch[0])-1);o->ch[1]=y.first;o->tain();y.first=o;}
    return y;
}
int Getkth(Treap *o,int v)
{
    if(o==NULL)return 0;
    return(o->val>=v)?Getkth(o->ch[0],v):Getkth(o->ch[1],v)+size(o->ch[0])+1;
}
inline int Findkth(int k)
{
    D x=Split(root,k-1);
    D y=Split(x.second,1);
    Treap *ans=y.first;
    root=Merge(Merge(x.first,ans),y.second);
    return ans!=NULL?ans->val:0;
}
inline void Insert(int v)
{
    int k=Getkth(root,v);
    D x=Split(root,k);
    Treap *o=new Treap(v);
    root=Merge(Merge(x.first,o),x.second);
}
void Delete(int v)
{
    int k=Getkth(root,v);
    D x=Split(root,k);
    D y=Split(x.second,1);
    root=Merge(x.first,y.second);
}
int main(){
    int m,opt,x;scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&opt,&x);
        switch(opt)
        {
            case 1:Insert(x);break;
            case 2:Delete(x);break;
            case 3:printf("%d\n",Getkth(root,x)+1);break;
            case 4:printf("%d\n",Findkth(x));break;
            case 5:printf("%d\n",Findkth(Getkth(root,x)));break;
            case 6:printf("%d\n",Findkth(Getkth(root,x+1)+1));break;
        }
    }
}

  

以上是关于无旋Treap的主要内容,如果未能解决你的问题,请参考以下文章

[代码] bzoj 1500 维修数列(无旋treap)

[代码] bzoj 3224 普通平衡树(无旋treap)

无旋Treap

无旋Treap

模板 - 数据结构 - 无旋Treap / FHQ Treap

[您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)