1、题目大意:数据结构题,是treap,全都是treap比较基本的操作

2、分析:没啥思考的

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
    Node *ch[2];
    int r, v, s, num;
    bool operator < (const Node& rhs) const{
        return r < rhs.r;
    }
    int cmp(int x){
        if(x < v) return 0;
        if(x == v) return -1;
        return 1;
    }
    int cmp1(int x){
        int k = num;
        if(ch[0]) k += ch[0] -> s;
        if(k - num + 1 <= x && x <= k) return -1;
        if(x <= k - num) return 0;
        return 1;
    }
    void maintain(){
        s = num;
        if(ch[0]) s += ch[0] -> s;
        if(ch[1]) s += ch[1] -> s;
    }
};
struct treap{
    Node ft[5000000];
    int tot;
    Node *root;
    void rotate(Node* &o, int d){
        Node* k = o -> ch[d ^ 1];
        o -> ch[d ^ 1] = k -> ch[d];
        k -> ch[d] = o;
        o -> maintain();
        k -> maintain();
        o = k;
    }
    void insert(Node* &o, int x){
        if(o == NULL){
            o = &ft[tot ++];
            o -> ch[0] = o -> ch[1] = NULL;
            o -> v = x;
            o -> r = rand();
            o -> num = o -> s = 1;
            return;
        }
        int d = o -> cmp(x);
        if(d == -1) o -> num ++;
        else {
            insert(o -> ch[d], x);
            if(o -> ch[d] > o) rotate(o, d ^ 1);
        }
        o -> maintain();
    }
    void remove(Node* &o, int x){
        int d = o -> cmp(x);
        if(d == -1){
            if(o -> num > 1) o -> num --;
            else if(o -> ch[0] && o -> ch[1]) {
                int d2 = 0;
                if(o -> ch[0] > o -> ch[1]) d2 = 1;
                rotate(o, d2);
                remove(o -> ch[d2], x);
            }
            else {
                if(o -> ch[0]){
                    o = o -> ch[0];
                }
                else o = o -> ch[1];
            }
        }
        else remove(o -> ch[d], x);
        if(o) o -> maintain();
    }
    int find(Node* &o, int x){
        if(o == NULL) return 0;
        int d = o -> cmp(x);
        if(d == -1) return o -> num;
        return find(o -> ch[d], x);
    }
    int less_k(Node* &o, int k){
        if(o == NULL) return 0;
        int d = o -> cmp(k);
        int yy = o -> num;
        if(o -> ch[0]) yy += o -> ch[0] -> s;
        if(d == -1) return yy - o -> num;
        else if(d == 0) return less_k(o -> ch[0], k);
        else return less_k(o -> ch[1], k) + yy;
    }
    int kth(Node* &o, int k){
        int d = o -> cmp1(k);
        int yy = o -> num;
        if(o -> ch[0]) yy += o -> ch[0] -> s;
        if(d == -1) return o -> v;
        if(d == 0) return kth(o -> ch[0], k);
        return kth(o -> ch[1], k - yy);
    }
} wt;
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++){
        int op, x;
        scanf("%d%d", &op, &x);
        if(op == 1){
            wt.insert(wt.root, x);
        }
        else if(op == 2){
            wt.remove(wt.root, x);
        }
        else if(op == 3){
            printf("%d\n", wt.less_k(wt.root, x) + 1);
        }
        else if(op == 4){
            printf("%d\n", wt.kth(wt.root, x));
        }
        else if(op == 5){
            int yy = wt.less_k(wt.root, x);
            printf("%d\n", wt.kth(wt.root, yy));
        }
        else{
            int yy = wt.less_k(wt.root, x);
            yy += wt.find(wt.root, x) + 1;
            printf("%d\n", wt.kth(wt.root, yy));
        }
    }
    return 0;
} 

BZOJ3224——Tyvj 1728 普通平衡树的更多相关文章

  1. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  2. bzoj3224: Tyvj 1728 普通平衡树(平衡树)

    bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...

  3. bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5354  Solved: 2196[Submit][Sta ...

  4. bzoj3224: Tyvj 1728 普通平衡树(splay)

    3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: #include<cstdio> #include<cstring> ...

  5. 【权值线段树】bzoj3224 Tyvj 1728 普通平衡树

    一个板子. #include<cstdio> #include<algorithm> using namespace std; #define N 100001 struct ...

  6. [BZOJ3224] [Tyvj 1728] 普通平衡树 (treap)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...

  7. BZOJ3224 Tyvj 1728 普通平衡树(Treap)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. 【权值分块】bzoj3224 Tyvj 1728 普通平衡树

    权值分块和权值线段树的思想一致,离散化之后可以代替平衡树的部分功能. 部分操作的时间复杂度: 插入 删除 全局排名 全局K大 前驱 后继 全局最值 按值域删除元素 O(1) O(1) O(sqrt(n ...

  9. 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会

    平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...

随机推荐

  1. BZOJ4446: [Scoi2015]小凸玩密室

    用ui,j表示走完i的子树后走到i的深度为j的祖先的兄弟的最小代价: 用vi,j表示走完i的子树后走到i的深度为j的祖先的最小代价,用u算出v. 枚举起点,计算答案. #include<bits ...

  2. uC/OS-II实现TEST.MAK块

    ################################################################################                     ...

  3. wpf listbox 内的内容显示问题,需要设置里面的itemsPresenter

    有时候控件并非维护本身逻辑,而是依赖于父子元素的,如了上诉的ContentPresenter,我们还有一个非常常用的ListBox控件,因为继承自ItemsControl,所以有一个ItemsPane ...

  4. fixed的left:50%,漂浮

    .floor-box{width: 44px; border: 1px solid #ccc; position: %; z-index: } 漂浮距离,距中间50% .floor-box{width ...

  5. jQM基本代码

    <div data-role="page"> <div data-role="header" data-position="fixe ...

  6. linq按条件sum

    var result = fruit.GroupBy(i => i.date) .Select(i => new { date = i.Key, no_of_apple = i.Where ...

  7. 在yii2验证之前执行一些额外自定义验证

    <?php $form = ActiveForm::begin([ 'id' => $model->formName(), 'action' => ['/apitools/de ...

  8. border opacity

    div { border: 1px solid rgb(127, 0, 0); border: 1px solid rgba(255, 0, 0, .5); -webkit-background-cl ...

  9. PHP与Javascript的混合测试

    js调用php <?php $num=88; ?> <script> var a = <?php echo $num;?>; alert(a); </scri ...

  10. VisualStudio基本使用(2)-使用VS2013来编译C语言程序

    切换到C语言程序所有目录,执行cl test.c