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. TCP 介绍

    TCP介绍 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的计算机网络O ...

  2. python调用ggsci.exe程序

    需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制. 简单GUI

  3. Yocto开发笔记之《工具使用:TFTP & NFS & SSH》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== TFTP工 ...

  4. mysql数据库添加索引优化查询效率

    项目中如果表中的数据过多的话,会影响查询的效率,那么我们需要想办法优化查询,通常添加索引就是我们的选择之一: 1.添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `t ...

  5. wpf 窗体内容旋转效果 网摘

    <Window x:Class="simplewpf.chuangtixuanzzhuan"        xmlns="http://schemas.micros ...

  6. 电脑中的Bois是什么

    电脑中的Bois是什么 BOIS= Basic Input/Output System,基本输入输出系统,全称是ROM-BOIS,是只读存储器基本输入/输出系统的简写,它实际是一组被固化到电脑中,为电 ...

  7. Android学习笔记——Button

    该工程的功能是实现在activity中显示一个TextView和一个Button 以下代码是MainActivity中的代码 package com.example.button; import an ...

  8. Garbage Collection C++

    http://herbsutter.com/2011/10/25/garbage-collection-synopsis-and-c/ http://www.codeproject.com/Artic ...

  9. css教程

    网址:http://www.aa25.cn/layout/index.shtml

  10. MSA:多重比对序列的格式及其应用

    多重比对序列的格式及其应用   这里对多重序列比对格式(Multiple sequence alignment – MSA)进行总结.在做系统演化分析.序列功能分析.基因预测等,都需要涉及到多重序列比 ...