treap 模版

struct Treap {
    struct node {
        node *son[2];
        int key,siz,wei,cnt;
        node(int _key,node *f) {
            son[0]=son[1]=f;
            key=_key;siz=cnt=1;wei=rand();
        }
        void pushup() {
            siz=son[0]->siz+son[1]->siz+cnt;
        }
    }*null,*root;
    Treap() {
        null=new node(0,0);
        null->siz=null->siz=0;
        null->wei=inf;root=null;//  INF视情况而定
    }
    void rot(node* &rt,bool d) {
        node* c=rt->son[!d];rt->son[!d]=c->son[d];
        c->son[d]=rt;rt->pushup();c->pushup();rt=c;
    }
    void insert(const int &key,node* &rt) {
        if (rt==null) {
            rt=new node(key,null);return ;
        }
        if (key==rt->key) {
            rt->cnt++;rt->siz++;return ;
        }
        bool d=key>rt->key;
        insert(key,rt->son[d]);
        if (rt->wei>rt->son[d]->wei) rot(rt,!d);
        rt->pushup();
    }
    void remove(const int &key,node* &rt) {
        if (rt==null) return ;
        bool d=key>rt->key;
        if (key==rt->key) {
            if (rt->cnt>1) {
                rt->cnt--;rt->siz--;return ;
            }
            d=rt->son[0]->wei>rt->son[1]->wei;
            if (rt->son[d]==null) {
                delete rt;rt=null;return ;
            }
            rot(rt,!d);remove(key,rt->son[!d]);
        } else remove(key,rt->son[d]);
        rt->pushup();
    }
    node* select(int k,node* rt) {
        int s=rt->son[0]->siz+rt->cnt;
        if (k>=rt->son[0]->siz+1&&k<=s) return rt;
        if (s>k) return select(k,rt->son[0]);
        else return select(k-s,rt->son[1]);
    }
    int rank(const int &key,node* rt) {
        if (rt==null) return 0;
        int s=rt->son[0]->siz+rt->cnt;
        if (key==rt->key) return rt->son[0]->siz+1;
        if (key<rt->key) return rank(key,rt->son[0]);
        else return s+rank(key,rt->son[1]);
    }
    int pre(const int &k) {
        node* t=root;int ret=0;
        while (t!=null)
        if (t->key<k) ret=t->key,t=t->son[1];
        else t=t->son[0];
        return ret;
    }
    int sub(const int &k) {
        node* t=root;int ret=0;
        while (t!=null)
        if (t->key>k) ret=t->key,t=t->son[0];
        else t=t->son[1];
        return ret;
    }
};
时间: 2024-08-01 18:42:15

treap 模版的相关文章

treap模版代码

treap模版暂存. 以后修改整理. #include<cstdio> #include<iostream> #include <time.h> #include<cstdlib> using namespace std; struct Node { Node *ch[2];//左右子树 int r;//优先级.数值越大,优先级越高 int v;//值 int cmp(int x)const { if(x==v) return -1; return x<

treap模版

#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct Node { Node *ch[2]; int r; int v; int s; Node(int v): v(v) { ch[0] = ch[1] = NULL; r = rand(); s = 1; } bool operator < (const Node& rhs) const{ r

POJ 1442 Black Box treap求区间第k大

题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; struct Node { Node *ch[2]; int r; int v; int s; Node(){} Node(int v): v(v)

BZOJ1588 [HNOI2002] 营业额统计

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 又是一道Treap模版题……总做模版题不好…… 另外牢记:BZOJ上用srand(time(0))会RE! 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <ctime> 6 #defin

平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是将有向树的所有边看成无向边形成的树状图.树是一种递归的数据结构,所以我们研究树也是按照递归的方式去研究的. 2.什么是二叉树. 我们给出二叉树的递归定义如下: (1)空树是一个二叉树. (2)单个节点是一个二叉树. (3)如果一棵树中,以它的左右子节点为根形成的子树都是二叉树,那么这棵树本身也是二叉

【bzoj3224】普通平衡树——treap

我的第一道treap题目,treap的模版题. 代码是对着hzw的敲的,一边敲一边理解... 主要是熟悉一下treap的各种基本操作,详细细节看代码. #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; struct point { int l,r,v,rnd,size,w; }tree[100005]; int n,size

二叉查找树模版

不过自己整理的一份模版.怕时间久了会忘掉.主程序里面是自己做的一些測试.可以完毕输出查找插入和删除四种功能.接下来会在这个程序上完毕平衡树Treap的部分功能 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; co

BZOJ 3224 普通平衡树(treap模板题)

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前

Treap

先推荐一篇文章和黄学长的代码http://hzwer.com/1712.html    https://wenku.baidu.com/view/c8c11e1e650e52ea55189887.html 黄学长的代码既不用指针又很短,真心推荐 Treap,顾名思义,Tree+Heap,它既满足二叉搜索树的性质,又满足堆的性质 对于二叉搜索树,如果我们把数有序加入,那么它的时间效率会退化成O(n). 我们引入一个随机数(即下文描述的修正值),使得随机数满足堆的性质(小根堆或大根堆,不一定要是完全