无旋Treap模板

传送门


Code 

#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
class fhq
{
    #define MN 100005
    private:
        int sz;
        int val[MN],pri[MN],ls[MN],rs[MN],siz[MN],cnt;
        inline unsigned int random()
        {
            static unsigned int x=23333;
            return x^=x<<13,x^=x>>17,x^=x<<5;
        }
        inline void combine(int x){siz[x]=1+siz[ls[x]]+siz[rs[x]];}
    public:
        int rt;
        int Merge(int rt1,int rt2)
        {
            if(!rt1||!rt2) return rt2+rt1;
            if(pri[rt1]<pri[rt2])
            {
                rs[rt1]=Merge(rs[rt1],rt2);
                combine(rt1);return rt1;
            }
            else
            {
                ls[rt2]=Merge(rt1,ls[rt2]);
                combine(rt2);return rt2;
            }
        }
        void Split(int x,int k,int&rt1,int&rt2)
        {
            if(!x) return (void)(rt1=rt2=0);
            if(k<=siz[ls[x]])
            {
                Split(ls[x],k,rt1,rt2);
                ls[x]=rt2;combine(x);rt2=x;
            }
            else
            {
                Split(rs[x],k-siz[ls[x]]-1,rt1,rt2);
                rs[x]=rt1;combine(x);rt1=x;
            }
        }
        int Rank(int x,int v)
        {
            if(!x) return 0;
            if(v<val[x]) return Rank(ls[x],v);
            else return siz[ls[x]]+Rank(rs[x],v)+1;
        }
        int Kth(int k)
        {
            register int rt1,rt2,rt3,c;
            Split(rt,k,rt1,rt2);Split(rt1,k-1,rt3,c);
            rt=Merge(rt3,Merge(c,rt2));
            return val[c];
        }
        void Insert(int v)
        {
            val[++sz]=v;pri[sz]=random(),siz[sz]=1;
            register int rk=Rank(rt,v),rt1,rt2;
            Split(rt,rk,rt1,rt2);
            rt=Merge(Merge(rt1,sz),rt2);
        }
        void Delete(int v)
        {
            register int rk=Rank(rt,v),rt1,rt2,rt3,c;
            Split(rt,rk,rt1,rt2);Split(rt1,rk-1,rt3,c);
            rt=Merge(rt3,rt2);
        }
}T;
int main(){
    register int m=read(),opt,x;
    while(m--)
    {
        opt=read(),x=read();
        switch(opt)
        {
            case 1: T.Insert(x);break;
            case 2: T.Delete(x);break;
            case 3: printf("%d\n",T.Rank(T.rt,x-1)+1);break;
            case 4: printf("%d\n",T.Kth(x));break;
            case 5: printf("%d\n",T.Kth(T.Rank(T.rt,x-1)));break;
            case 6: printf("%d\n",T.Kth(T.Rank(T.rt,x)+1));break;
        }
    }
    return 0;
}

传送门


Code 

#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
class fhq
{
    #define MN 100005
    private:
        int sz;
        int val[MN],pri[MN],ls[MN],rs[MN],siz[MN],rev[MN];
        inline unsigned int random()
        {
            static unsigned int x=23333;
            return x^=x<<13,x^=x>>17,x^=x<<5;
        }
        inline void up(int x){siz[x]=1+siz[ls[x]]+siz[rs[x]];}
        inline void down(int x){if(rev[x]) std::swap(ls[x],rs[x]),rev[ls[x]]^=1,rev[rs[x]]^=1,rev[x]=0;}
    public:
        int rt;
        int Merge(int rt1,int rt2)
        {
            if(!rt1||!rt2) return rt2+rt1;
            down(rt1),down(rt2);
            if(pri[rt1]<pri[rt2])
            {
                rs[rt1]=Merge(rs[rt1],rt2);
                up(rt1);return rt1;
            }
            else
            {
                ls[rt2]=Merge(rt1,ls[rt2]);
                up(rt2);return rt2;
            }
        }
        void Split(int x,int k,int&rt1,int&rt2)
        {
            if(!x) return (void)(rt1=rt2=0);
            down(x);
            if(k<=siz[ls[x]])
            {
                Split(ls[x],k,rt1,rt2);
                ls[x]=rt2;up(x);rt2=x;
            }
            else
            {
                Split(rs[x],k-siz[ls[x]]-1,rt1,rt2);
                rs[x]=rt1;up(x);rt1=x;
            }
        }
        int Rank(int x,int v)
        {
            if(!x) return 0;
            if(v<val[x]) return Rank(ls[x],v);
            else return siz[ls[x]]+Rank(rs[x],v)+1;
        }
        void Insert(int v)
        {
            val[++sz]=v;pri[sz]=random(),siz[sz]=1;
            register int rk=Rank(rt,v),rt1,rt2;
            Split(rt,rk,rt1,rt2);
            rt=Merge(Merge(rt1,sz),rt2);
        }
        void Print(int x)
        {
            if(!x) return;down(x);
            Print(ls[x]);printf("%d ",val[x]);Print(rs[x]);
        }
        void Reverse(int l,int r)
        {
            register int rt1,rt2,rt3,rt4;
            Split(rt,l-1,rt1,rt2);Split(rt2,r-l+1,rt3,rt4);
            rev[rt3]^=1;rt=Merge(rt1,Merge(rt3,rt4));
        }
    #undef MN
}T;
int main(){
    register int n,m,i;
    n=read(),m=read();
    for(i=1;i<=n;++i) T.Insert(i);
    while(m--) i=read(),T.Reverse(i,read());
    T.Print(T.rt);puts("");
    return 0;
}



Blog来自PaperCloud,未经允许,请勿转载,TKS!

转载于:https://www.cnblogs.com/PaperCloud/p/10131940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值