您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入x数

  2. 删除x数(若有多个相同的数,因只删除一个)

  3. 查询x数的排名(若有多个相同的数,因输出最小的排名)

  4. 查询排名为x的数

  5. 求x的前驱(前驱定义为小于x,且最大的数)

  6. 求x的后继(后继定义为大于x,且最小的数)

--by 洛谷;

http://www.lydsy.com/JudgeOnline/problem.php?id=3224



  话说,没时间写题解啊——

  不过,也就是个平衡树的模板题;

就存下代码吧

  treap哟;

  好像很少见写treap的,但常数还是挺小的orz;

  哎,最近怎么老写模板啊?

  代码如下:

 #include<cstdio>
#include<cstdlib>
using namespace std;
#define INF 2147483647
int n;
struct poo
{
int size,value,key,cnt;
int ch[];
}data[];
int tot,root,x;
int make_data(int );
void insert(int&);
void roll(int&);
int find( );
int rank( );
void del(int&);
int las(int );
int nex(int );
void up(int );
int main()
{
int i,j;
data[].value=INF;data[].key=INF;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d%d",&j,&x);
switch(j){
case :insert(root);break;
case : del(root);break;
case : printf("%d\n",rank( ));break;
case : printf("%d\n",find( ));break;
case : printf("%d\n", las(root));break;
case : printf("%d\n", nex(root));break;
}
}
}
int make_data(int value)
{
tot++;
data[tot].cnt++;
data[tot].key=(rand()/+rand()/);
data[tot].size=;
data[tot].value=value;
return tot;
}
void insert(int &now)
{
if(now==){
now=make_data(x);
return;
}
if(data[now].value==x){
data[now].cnt++;
data[now].size++;
}
else{
int wh=x < data[now].value ? : ;
insert(data[now].ch[wh]);
if(data[now].key>=data[data[now].ch[wh]].key)
roll(now);
}
up(now);
}
void roll(int &now)
{
int wh=data[data[now].ch[]].key<data[data[now].ch[]].key?:;
int son=data[now].ch[wh];
data[now].ch[wh]=data[son].ch[wh^];
data[son].ch[wh^]=now;
up(now);
now=son;
}
int find()
{
int now=root;
int ls,rs;
ls=data[now].ch[];rs=data[now].ch[];
while(x<=data[ls].size||x>data[now].size-data[rs].size){
if(data[ls].size>=x)
now=ls;
else{
x=x+data[rs].size-data[now].size;
now=rs;
}
ls=data[now].ch[];rs=data[now].ch[];
}
return data[now].value;
}
int rank()
{
int now=root,ans=;
int ls=data[now].ch[],rs=data[now].ch[];
while(x!=data[now].value&&x!=)
{
if(x<data[now].value)
now=ls;
else{
ans+=data[now].size-data[rs].size;
now=rs;
}
ls=data[now].ch[];rs=data[now].ch[];
}
return ans+data[ls].size+;
}
void del(int &now)
{
if(data[now].value==x){
if(data[now].cnt==){
if(data[now].ch[]*data[now].ch[]==){
now=data[now].ch[]+data[now].ch[];
return ;
}
roll(now);
int wh=data[data[now].ch[]].value==x?:;
del(data[now].ch[wh]);
}
else{
data[now].size--; data[now].cnt--;
}
}
else{
int wh=data[now].value>x?:;
del(data[now].ch[wh]);
}
up(now);
}
int las(int now)
{
int ans=,an=;
if(!now)return ;
if(data[now].value<x){
ans=data[now].value;
an=las(data[now].ch[]);
ans=an!=?an:ans;
}
else{
ans=las(data[now].ch[]);
}
return ans;
}
int nex(int now)
{
int ans=,an=;
if(!now)return ;
if(data[now].value>x){
ans=data[now].value;
an=nex(data[now].ch[]);
ans=an!=?an:ans;
}
else{
ans=nex(data[now].ch[]);
}
return ans;
}
void up(int now)
{
data[now].size=data[data[now].ch[]].size+data[data[now].ch[]].size+data[now].cnt;
}
//treap on the 2017.1.21
//10
//1 5
//4 1
//1 6
//1 7
//1 10
//1 3
//1 4
//6 2
//1 8
//5 9
//
//14
//1 5
//1 6
//1 7
//1 10
//1 3
//1 4
//1 8
//3 3
//3 4
//3 5
//3 6
//4 5
//4 6
//4 7

祝AC哟;

普通平衡树Tyvj1728、luogu P3369 (treap)的更多相关文章

  1. [luogu P3369]【模板】普通平衡树(Treap/SBT)

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

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

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...

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

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...

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

    二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...

  5. 【数据结构】平衡树splay和fhq—treap

    1.BST二叉搜索树 顾名思义,它是一棵二叉树. 它满足一个性质:每一个节点的权值大于它的左儿子,小于它的右儿子. 当然不只上面那两种树的结构. 那么根据性质,可以得到该节点左子树里的所有值都比它小, ...

  6. 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay

    题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...

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

    二次联通门 : LibreOJ #104. 普通平衡树 #include <cstdio> #include <iostream> #include <algorithm ...

  8. 普通平衡树Tyvj1728、luogu P3369 (splay)

    存个模板,这次是splay的: 题目见这个题解: <--(鼠标移到这儿) 代码如下: #include<cstdio> #define INF 2147483647 using na ...

  9. 【luogu P3369 【模板】普通平衡树(Treap/SBT)】 模板 Scapegoat Tree

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

随机推荐

  1. IntelliJ IDEA中可以用Tab键跳出括号和引号吗?

    在 Settings->Editor->General->Smart Keys 中勾选 Jump outside closing bracket/quote with Tab 即可.

  2. 递归实现快速幂(C++版)

    快速幂是什么? 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 就以a的b次方来介绍: 把b转换成二进制数,该二进制数第i位的权为 ...

  3. 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION

    1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...

  4. 部署一个flask服务记录

    最近使用flask写了一些简单的服务. 服务部署到服务器上进行使用,这个过程会有一些问题,需要进行记录一下. 说明运行的环境情况.使用的是python3.6的虚拟环境,系统是centos7,其他的有u ...

  5. 学习GO第一天,自我感觉可麻利的开干了-GO语言配置、开发、服务器部署

    学习GO第一天,自我感觉可麻利的开干了-GO语言配置.开发.服务器部署 第一步下载 go sdk https://golang.org/dl/ https://storage.googleapis.c ...

  6. windows电脑配置

    1.本地电脑通过修改hosts文件实现域名本地解析 以管理员身份打开记事本 并打开C:\Windows\System32\drivers\etc 路径下的hosts文件 在文件末尾添加如下

  7. UI的线程问题:单线程原因及更新UI的四种方式

    1.UI线程为什么设计为单线程? UI控件的操作不是线程安全的,对于多线程并发访问的时候,如果使用加锁机制会导致: UI控件的操作变得很复杂. 加锁的操作必定会导致效率下降. 所以android系统在 ...

  8. JavaScript数据结构-13.散列碰撞(开链法)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. unity字库精简

    有2种办法,具体看情况使用 1.unity自带功能 选择放入的字体,修改Character项为"Custom set",接着出现Custom Chars中输入你想使用的字符串,字符 ...

  10. C 扩展库 - mysql API

    MySQL API C API Data Structures MYSQL This structure represents handler for one database connection. ...