[Tyvj 1728]普通平衡树

题目

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

OUTPUT

106465

84185
492737

解题报告

一道裸的平衡树板子题,也是我的Treap首题,留念

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
inline int read(){
int sum(),f();
char ch(getchar());
while(ch<''||ch>''){
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<=''){
sum=sum*+ch-'';
ch=getchar();
}
return sum*f;
}
struct node{
int size,key,v;
node *ch[];
node():size(),key(rand()),v(){
ch[]=ch[]=NULL;
}
node(int x):size(),key(rand()),v(x){
ch[]=ch[]=NULL;
}
}*root;
inline int get_size(node *x){
if(x==NULL)
return ;
return x->size;
}
inline void pushup(node *rt){
rt->size=get_size(rt->ch[])+get_size(rt->ch[])+;
}
inline void ro(node *&rt,int d){
node *tmp(rt->ch[d^]);
rt->ch[d^]=tmp->ch[d];
pushup(rt);
tmp->ch[d]=rt;
pushup(tmp);
rt=tmp;
}
inline void insert(node *&rt,int x){
if(!rt){
rt=new node(x);
return;
}
int d(rt->v>x);
insert(rt->ch[d^],x);
pushup(rt);
if(rt->ch[d^]->key>rt->key)
ro(rt,d);
}
inline void del(node *&rt,int x){
if(rt->v==x){
if(rt->ch[]!=NULL&&rt->ch[]!=NULL){
int d(rt->ch[]->key>rt->ch[]->key);
ro(rt,d);
del(rt->ch[d],x);
}
else{
node *tmp=NULL;
if(rt->ch[]!=NULL)
tmp=rt->ch[];
else
tmp=rt->ch[];
delete rt;
rt=tmp;
}
}
else{
int d(rt->v>x);
del(rt->ch[d^],x);
}
if(rt!=NULL)
pushup(rt);
}
inline int rk(int x){
node *rt(root);
int ret();
while(rt){
if(x>rt->v){
ret+=get_size(rt->ch[])+;
rt=rt->ch[];
}
else
rt=rt->ch[];
}
return ret;
}
inline int kth(int k){
node *rt(root);
while(rt){
if(get_size(rt->ch[])+==k)
return rt->v;
if(get_size(rt->ch[])+>k)
rt=rt->ch[];
else{
k-=get_size(rt->ch[])+;
rt=rt->ch[];
}
}
return ;
}
inline int gg(){
freopen("phs.in","r",stdin);
freopen("phs.out","w",stdout);
srand(time(NULL));
int n(read());
while(n--){
int op(read()),x(read());
if(op==){
insert(root,x);
continue;
}
if(op==){
del(root,x);
continue;
}
if(op==){
printf("%d\n",rk(x)+);
continue;
}
if(op==){
printf("%d\n",kth(x));
continue;
}
if(op==){
printf("%d\n",kth(rk(x)));
continue;
}
if(op==){
printf("%d\n",kth(rk(x+)+));
continue;
}
}
}
int k(gg());
int main(){;}

玄学板子,调的时间比打的时间还长= =
ps:参数类型一定要写对QWQ

[补档][Tyvj 1728]普通平衡树的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 【bzoj】3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 10097  Solved: 4302[Submit][St ...

  9. BZOJ_3224 Tyvj 1728 普通平衡树 【离散化+权值线段树】

    一 题面 Tyvj 1728 普通平衡树 二 分析 比较明显是可以用平衡二叉搜索树(splay)做的. 用权值线段树做,前提就是要先离散化,因为权值线段树维护的值域信息. 板子. 三 AC代码 #in ...

随机推荐

  1. [转]AngularJS 之 ng-options指令

    原文地址 一. 基本下拉效果(lable for value in array) 其中select标签中的ng-model属性必须有,其值为选中的对象或属性值. <div ng-controll ...

  2. java web项目中 读取properties 路径的问题

    可以先获取项目的classPath String classPath = this.getClass().getResource("/").getPath();//获取classP ...

  3. Linux下重启多个 tomcat 服务的脚本

    由于修改tomcat的配置文件或手动操作数据库数据后,tomcat的缓存和redis的缓存很严重,需要经常重启tomcat来释放缓存,经常就是手动重启. # .查找tomcat的进程ID ps -ef ...

  4. 【Android Developers Training】 72. 缩放一个视图

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 简单svg动画实现

    一.将svg嵌入到html中 svg是指可伸缩矢量图形,它使用XML格式定义图像.在html中可以使用<svg>标签直接嵌入svg代码,例如: <svg version=" ...

  6. pyparsing:定制自己的解析器

    在工作中,经常需要解析不同类型的文件,常用的可能就是正则表达式了,简单点的,可能会使用awk.这里要推荐一种比较小众的方式,使用pyparsing来解析文件. pyparsing可以做些什么呢?主要可 ...

  7. HTML5+CSS3静态页面项目-BusinessTheme的总结

    因为期末考试.调整心态等等的种种原因,距离上一次的项目练习已经过了很久了,今天终于有时间继续练习HTML5+CSS3的页面架构和设计稿还原.设计图很长,整个页面分为了好几个区域,所以就不放完整的设计图 ...

  8. Vuejs——Vue生命周期,数据,手动挂载,指令,过滤器

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   原教程: http://cn.vuejs.org/guide/instance.html htt ...

  9. java IO之 File类+字节流 (输入输出 缓冲流 异常处理)

    1. File类

  10. Cent-Linux腾讯课堂学习笔记

    RedHat yum系统下 防火墙 关闭防火墙方法 systemctl stop firewalld 检测防火墙状态 systemctl status firewalld 设置防火墙禁用开机启动 sy ...