3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 16656  Solved: 7255
[Submit][Status][Discuss]

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]

Source

贴模板:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int fa[MAXN],ch[MAXN][],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{
root=sz=;
memset(ch,,sizeof(ch));
memset(fa,,sizeof(fa));
memset(cnt,,sizeof(cnt));
memset(size,,sizeof(size));
}
inline void clear(int x) { fa[x]=ch[x][]=ch[x][]=cnt[x]=size[x]=; }
inline int get(int x) { return ch[fa[x]][]==x; }
inline void update(int x)
{
if(x){
size[x]=cnt[x];
if(ch[x][]) size[x]+=size[ch[x][]];
if(ch[x][]) size[x]+=size[ch[x][]];
}
}
inline void rotate(int x)
{
int father=fa[x],ffather=fa[father],which=get(x);
ch[father][which]=ch[x][!which];fa[ch[father][which]]=father;
ch[x][!which]=father;fa[father]=x;
fa[x]=ffather;
if(ffather) ch[ffather][ch[ffather][]==father]=x;
update(father);
update(x);
}
inline void splay(int x)
{
for(int father;(father=fa[x]);rotate(x))
if(fa[father])
rotate((get(x)==get(father)?father:x));
root=x;
}
inline void insert(int x)
{
if(root==) { root=++sz;fa[sz]=ch[sz][]=ch[sz][]=;cnt[sz]=size[sz]=;key[sz]=x;return; }
int now=root,father=;
while(){
if(key[now]==x) { cnt[now]++;update(now);update(father);splay(now);return; }
father=now;
now=ch[father][key[now]<x];
if(now==){
sz++;
fa[sz]=father;
ch[father][key[father]<x]=sz;
ch[sz][]=ch[sz][]=;
cnt[sz]=size[sz]=;
key[sz]=x;
update(father);
splay(sz);
return;
}
}
}
inline int find(int x)//找到x的位置
{
int now=root,ans=;
while(){
if(x<key[now]) now=ch[now][];
else{
if(ch[now][]) ans+=size[ch[now][]];
if(x==key[now]) { splay(now);return ans+; }
ans+=cnt[now];
now=ch[now][];
}
}
}
inline int rank(int x)//找到排名为x的数
{
int now=root;
while(){
if(ch[now][]&&x<=size[ch[now][]]) now=ch[now][];
else{
int tmp=(ch[now][]?size[ch[now][]]:)+cnt[now];
if(x<=tmp) return key[now];
x=x-tmp;
now=ch[now][];
}
}
}
inline int pre()//找前驱
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];
return now;
}
inline int suf()//找后继
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];
return now;
}
inline void del(int x)//删去一个x
{
find(x);
if(cnt[root]>) { cnt[root]--;update(root);return; }
else if(!ch[root][]&&!ch[root][]) { root=sz=;clear(root);return; }
else if(!ch[root][]){
int oldroot=root;
root=ch[root][];
fa[root]=;
clear(oldroot);
return;
}else if(!ch[root][]){
int oldroot=root;
root=ch[root][];
fa[root]=;
clear(oldroot);
return;
}
int leftbig=pre(),oldroot=root;
splay(leftbig);
ch[root][]=ch[oldroot][];
fa[ch[root][]]=root;
clear(oldroot);
update(root);
}
int main()
{
//freopen("in.txt","r",stdin);
int n,a,b;
init();
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
if(a==) insert(b);
else if(a==) del(b);
else if(a==) printf("%d\n",find(b));
else if(a==) printf("%d\n",rank(b));
else if(a==) { insert(b);printf("%d\n",key[pre()]);del(b); }
else if(a==) { insert(b);printf("%d\n",key[suf()]);del(b); }
}
return ;
}

bzoj 3224的更多相关文章

  1. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  2. BZOJ 3224 普通平衡树(Treap模板题)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][ ...

  3. [bzoj 3224]手写treap

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...

  4. BZOJ 3224: Tyvj 1728 普通平衡树

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

  5. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  6. BZOJ 3224: Tyvj 1728 普通平衡树 treap

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

  7. BZOJ 3224 普通平衡树

    这个是第一份完整的treap代码.嗯...虽然抄的百度的版,但还是不错的. !bzoj上不能用srand. #include<iostream>#include<cstdio> ...

  8. BZOJ 3224: Tyvj 1728 普通平衡树 vector

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

  9. BZOJ 3224 普通平衡树(树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3224 题意:维护以下操作:(1)插入x:(2)删除x(若有多个相同的数,只删除一个)(3 ...

  10. BZOJ 3224: Tyvj 1728 普通平衡树(BST)

    treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...

随机推荐

  1. +new Date()的用法

    var s=+newDate();   var s=+newDate(); 解释如下:=+是不存在的; +new Date()是一个东西; +相当于.valueOf(); 看到回复补充一下.getTi ...

  2. 2018年第九届蓝桥杯【C++省赛B组】

    2标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...

  3. 作业要求 20181127-5 Beta发布用户使用报告

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2450 一.用户反馈 反馈截图(部分) 三.用户反馈情况统计图

  4. "Hello World!"团队负责人领跑衫感言

    时间:2017年12月7日 团队名称:“Hello World!” 团队项目:空天猎 团队成员:陈建宇(项目负责人).刘淑霞.黄泽宇.方铭.贾男男.刘耀泽.刘成志 感言正文: 记<软件工程> ...

  5. 第二阶段Sprint冲刺会议3

     进展:讨论视频录制的具体功能,查看有关资料,开始着手编写有关代码.

  6. 安卓开发神器vysor+adb wifi

    准备: 1.vysor需要FQ从google应用商店下载,装在google上,目前知道的免费的vysor的作用是电脑显示手机屏幕并且能操控手机. 步骤:FQ后就能下载了,FQ方法不赘述.

  7. HDU 4514 湫湫系列故事——设计风景线 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4514 湫湫系列故事--设计风景线 Time Limit: 5000/2000 MS (Java/Ot ...

  8. Failed to execute request because the App-Domain could not be created. Error: 0x8007000e 存储空间不足,无法完成此操作。

    今天业务发现,服务器上的网站无法访问了. 页面报错: Server Application UnavailableThe web application you are attempting to a ...

  9. 关于mybatis的思考(2)——mybatis映射文件的深入理解

    1.配置文件 mybatis进行持久化操作是以SqlSessionFactory对象为基础的,这个对象是整个数据库映射关系经过编译后的内存镜像. InputStream inputStream = R ...

  10. 软工网络15团队作业8——Beta阶段敏捷冲刺(Day6)

    提供当天站立式会议照片一张 每个人的工作 1.讨论项目每个成员的昨天进展 赵铭: 数据库整理. 吴慧婷:我的世界界面完成部分. 陈敏: 我的世界功能--学习情况功能完成. 吴雅娟:我的世界功能--学习 ...