fhq treap ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : LibreOJ #104. 普通平衡树

#include <cstdio>
#include <iostream>
#include <algorithm>
const int BUF = 12312323;
char Buf[BUF], *buf = Buf;

inline void read (int &now)
{
    bool temp = false;
    for (now = 0; !isdigit (*buf); ++ buf)
        if (*buf == ‘-‘) temp = true;
    for (; isdigit (*buf); now = now *10 + *buf - ‘0‘, ++ buf);
    if (temp) now = -now;
}

struct T_D
{
    T_D *L, *R;
    int key, r, s;
    inline void Updata ()
    {
        s = 1 + (L ? L->s : 0) + (R ? R->s : 0);
    }
};

#define Max 1231231
struct D
{
    T_D *x, *y; D () {}
    D (T_D *_x, T_D *_y) : x (_x), y (_y) {}
};
class Fhq_Treap
{
    private :

        T_D poor[Max], *Ta, *Root;

        inline T_D *New (int _x)
        {
            T_D *now = ++ Ta;
            now->r = rand (), now->key = _x;
            now->s = 1, now->L = now->R = NULL;
            return now;
        }

        D Split (T_D *now, int k)
        {
            if (now == NULL) return D (NULL, NULL);
            D res;
            if ((now->L ? now->L->s : 0) >= k)
            {
                res = Split (now->L, k);
                now->L = res.y, now->Updata ();
                res.y = now;
            }
            else
            {
                res = Split (now->R, k - (now->L ? now->L->s : 0) - 1);
                now->R = res.x, now->Updata ();
                res.x = now;
            }
            return res;
        }

        T_D *Merge (T_D *A, T_D *B)
        {
            if (A == NULL) return B;
            if (B == NULL) return A;
            if (A->r < B->r)
            {
                A->R = Merge (A->R, B);
                A->Updata (); return A;
            }
            else
            {
                B->L = Merge (A, B->L);
                B->Updata (); return B;
            }
        }

        int Get_rank (T_D *now, int k)
        {
            if (now == NULL) return 0;
            return k <= now->key ? Get_rank (now->L, k) : (Get_rank (now->R, k) + (now->L ? now->L->s : 0) + 1);
        }

    public :

        Fhq_Treap () { Ta = poor; }
        inline int Get_rank (int k)
        {
            return Get_rank (Root, k) + 1;
        }

        int Find_kth (int k)
        {
            D x = Split (Root, k - 1);
            D y = Split (x.y, 1);
            T_D *res = y.x;
            Root = Merge (Merge (x.x, res), y.y);
            return res->key;
        }

        void Insert (int key)
        {
            int k = Get_rank (Root, key);
            D x = Split (Root, k);
            T_D *now = New (key);
            Root = Merge (Merge (x.x, now), x.y);
        }

        void Delete (int key)
        {
            int k = Get_rank (Root, key);
            D x = Split (Root, k);
            D y = Split (x.y, 1);
            Root = Merge (x.x, y.y);
        }

        int Find_Pre (int key)
        {
            int k = Get_rank (Root, key);
            D x = Split (Root, k - 1);
            D y = Split (x.y, 1);
            T_D *res = y.x;
            Root = Merge (Merge (x.x, res), y.y);
            return res->key;
        }

        int Find_Suc (int key)
        {
            int k = Get_rank (Root, key + 1);
            D x = Split (Root, k);
            D y = Split (x.y, 1);
            T_D *res = y.x;
            Root = Merge (Merge (x.x, res),  y.y);
            return res->key;
        }
};

Fhq_Treap Tree;
int Main ()
{
    fread (buf, 1, BUF, stdin);
    int N, M; register int i;
    read (N); int x, type;
    for (i = 1; i <= N; ++ i)
    {
        read (type), read (x);
         if (type == 1)
            Tree.Insert (x);
        else if (type == 2)
            Tree.Delete (x);
        else if (type == 3)
            printf ("%d\n", Tree.Get_rank (x));
        else if (type == 4)
            printf ("%d\n", Tree.Find_kth (x));
        else if (type == 5)
            printf ("%d\n", Tree.Find_Pre (x));
        else printf ("%d\n", Tree.Find_Suc (x));
    }
    return 0;
}

int ZlycerQan = Main ();
int main (int argc, char *argv[]) {;}
时间: 2025-01-03 22:33:30

fhq treap ------ luogu P3369 【模板】普通平衡树(Treap/SBT)的相关文章

luoguP3369[模板]普通平衡树(Treap/SBT) 题解

链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<iomanip> #include<algorithm> #include<ctime> #include<queue> #incl

数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now) { now = 0; register char word = getchar (); bool temp = false; while (wor

替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #include <vector> #define Max_ 100010 #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now) { register char w

红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) #include <cstdio> #define Max 100001 #define Red true #define Black false #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (i

算法模板——平衡树Treap 2

实现功能:同平衡树Treap 1(BZOJ3224 / tyvj1728) 这次的模板有了不少的改进,显然更加美观了,几乎每个部分都有了不少简化,尤其是删除部分,这个参照了hzwer神犇的写法,在此鸣谢,然后,贴模板走人 1 var 2 i,j,k,l,m,n,head,tot:longint; 3 a,b,lef,rig,fix:array[0..100010] of longint; 4 function min(x,y:longint):longint; 5 begin 6 if x<y

BZOJ1208: [HNOI2004]宠物收养所 平衡树 Treap 模板题

Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被 主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希 望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值.这样他就能够很方便的处理整个领养宠物的过程 了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少. 1. 被遗弃的宠物过多时,假若到来一个

treap完全版模板

这是我综合poj1442 3481 2352的treap操作 得到treap完全版模板.(经测AC) 从NOCOW——Treap中一份代码中模仿加工精致而成. 结构体Tree { int key; //键值 int size; //该子树总节点个数 int pri; //其随机值 int son[2]; //从nocow一份代码中学来的,0表示左儿子,1表示右儿子,旋转只需一个函数即可 (int num;) //该节点在序列中的原位置,可添加 } 该Treap模板支持操作: 1. 插入值 2.

hiho 1325 : 平衡树&#183;Treap

#1325 : 平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉排序树和堆啊,你看这两种数据结构都是构造了一个二叉树,一个节点有一个父亲和两个儿子. 如果用1..n的数组来存储的话,对于二叉树上的一个编号为k的节点,其父亲节点刚好是k/2.并且它的两个儿子节点分别为k*2和k*2+1,计算起来非常方便呢. 小Hi:没错,但是小Hi你知道有一种办

luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<iomanip> #include<algorithm> #include<ctime> #include<queue> #inc