bzoj 3224: Tyvj 1728 普通平衡树【非旋treap】

就是非旋treap的板子

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=500005,mod=73939133,bs=997687623;
int q,tot,rt,st=123;
struct qwe
{
    int c[2],f,v,s,w;
}t[N];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int rand()
{
    return st=1ll*st*bs%mod;
}
void ud(int x)
{
    t[x].s=t[t[x].c[0]].s+t[t[x].c[1]].s+1;
}
int hb(int x,int y)
{
    if(!x||!y)
        return x+y;
    if(t[x].w<t[y].w)
    {
        t[x].c[1]=hb(t[x].c[1],y);
        ud(x);
        return x;
    }
    else
    {
        t[y].c[0]=hb(x,t[y].c[0]);
        ud(y);
        return y;
    }
}
void split(int x,int v,int &l,int &r)
{
    if(!x)
    {
        l=r=0;
        return;
    }
    if(t[x].v<=v)
        l=x,split(t[x].c[1],v,t[x].c[1],r);
    else
        r=x,split(t[x].c[0],v,l,t[x].c[0]);
    ud(x);
}
int kth(int x,int k)
{
    while(1)
    {
        if(k<=t[t[x].c[0]].s)
            x=t[x].c[0];
        else if(k==t[t[x].c[0]].s+1)
            return x;
        else
            k-=t[t[x].c[0]].s+1,x=t[x].c[1];
    }
}
void charu(int x)
{
    int a,b;
    split(rt,x,a,b);
    t[++tot].s=1,t[tot].v=x,t[tot].w=rand();
    rt=hb(hb(a,tot),b);
}
void shanchu(int x)
{
    int a,b,c;
    split(rt,x,a,c);
    split(a,x-1,a,b);
    rt=hb(hb(a,hb(t[b].c[0],t[b].c[1])),c);
}
void rank(int x)
{
    int a,b;
    split(rt,x-1,a,b);
    printf("%d\n",t[a].s+1);
    rt=hb(a,b);
}
void real(int x)
{
    printf("%d\n",t[kth(rt,x)].v);
}
void pre(int x)
{
    int a,b;
    split(rt,x-1,a,b);
    printf("%d\n",t[kth(a,t[a].s)].v);
    rt=hb(a,b);
}
void nxt(int x)
{
    int a,b;
    split(rt,x,a,b);
    printf("%d\n",t[kth(b,1)].v);
    rt=hb(a,b);
}
int main()
{
    q=read();
    while(q--)
    {
        int o=read();
        if(o==1)
            charu(read());
        else if(o==2)
            shanchu(read());
        else if(o==3)
            rank(read());
        else if(o==4)
            real(read());
        else if(o==5)
            pre(read());
        else
            nxt(read());
    }
    return 0;
}

转载于:https://www.cnblogs.com/lokiii/p/10069690.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值