bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 5354 Solved: 2196
[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
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
84185
492737
HINT
【思路】
Rank tree
Treap实现名次树。需要多维护s,w域分别表示节点数目与相同键值的数目,相应修改maintain,remove和rank操作。
需要注意的是不能单单在insert的时候安排相同结点放在右子,因为有可能结点通过旋转转上来,这就违反了我们的初衷。
【代码】
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; struct Node{
Node* ch[];
int v,r,s,w;
Node(int x):v(x) { ch[]=ch[]=NULL; s=w=; r=rand(); }
void maintain() {
s=w; //change
if(ch[]!=NULL) s+=ch[]->s;
if(ch[]!=NULL) s+=ch[]->s;
}
int cmp(int x) const {
if(x==v) return -; return x<v?:;
}
};
void rotate(Node* &o,int d) {
Node* k=o->ch[d^]; o->ch[d^]=k->ch[d]; k->ch[d]=o;
o->maintain(); k->maintain(); o=k;
}
//不能只是把大于等于x的放在右子树 有可能旋上来
void insert(Node* &o,int x) {
if(o==NULL) o=new Node(x);
else {
int d=o->cmp(x);
if(d==-) { o->w++; o->maintain(); return; } //相同键值的个数
insert(o->ch[d],x);
if(o->ch[d]->r > o->r) rotate(o,d^);
}
o->maintain();
}
void remove(Node* &o,int x){
if(o==NULL) return ;
int d=o->cmp(x);
if(d==-) {
Node* u=o;
if(o->w>) { o->w--; o->maintain(); return ; } //change2
if(o->ch[]!=NULL && o->ch[]!=NULL) {
int d2=o->ch[]->r > o->ch[]->r? :;
rotate(o,d2); remove(o->ch[d2],x);
}
else {
if(o->ch[]!=NULL) o=o->ch[]; else o=o->ch[];
delete u;
}
}
else remove(o->ch[d],x);
if(o!=NULL) o->maintain();
}
int kth(Node* o,int k) {
if(o==NULL || k<= || k>o->s) return ;
int s=o->ch[]==NULL? :o->ch[]->s,w=o->w;
if(s+<=k && k<=s+w) return o->v;
else if(k<=s) return kth(o->ch[],k);
else return kth(o->ch[],k-s-w);
}
int rank(Node* o,int x) {
if(o==NULL) return ;
int d=o->cmp(x),s=o->ch[]==NULL?:o->ch[]->s;
int f= d==? s+o->w:;
if(d==-) return s+;
else return rank(o->ch[d],x)+f;
}
void query1(Node* o,int x,int& ans) {
if(o==NULL) return ;
if(o->v<x) { ans=o->v; query1(o->ch[],x,ans); }
else query1(o->ch[],x,ans);
}
void query2(Node* o,int x,int& ans) {
if(o==NULL) return ;
if(o->v>x) { ans=o->v; query2(o->ch[],x,ans); }
else query2(o->ch[],x,ans);
}
int n,opt,x,ans;
Node* root=NULL; int main() {
//srand(time(0)); //bzoj上不要用 否则RE
scanf("%d",&n);
FOR(i,,n) {
scanf("%d%d",&opt,&x);
switch(opt) {
case : insert(root,x); break;
case : remove(root,x); break;
case : printf("%d\n",rank(root,x)); break;
case : printf("%d\n",kth(root,x)); break;
case : ans=; query1(root,x,ans); printf("%d\n",ans); break;
case : ans=; query2(root,x,ans); printf("%d\n",ans); break;
}
}
return ;
}
bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)的更多相关文章
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- bzoj3224: Tyvj 1728 普通平衡树(平衡树)
bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- bzoj3224: Tyvj 1728 普通平衡树(splay)
3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: #include<cstdio> #include<cstring> ...
- 【权值线段树】bzoj3224 Tyvj 1728 普通平衡树
一个板子. #include<cstdio> #include<algorithm> using namespace std; #define N 100001 struct ...
- 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
冬令营被平衡树坑了之后,打算苦练一番数据结构(QAQ). 先是打了一下想学好久的替罪羊树. 替罪羊树实现方法很简单,就是在不满足平衡条件的时候暴力重构子树. 调试小结: 1.删除操作分两类情况:如果某 ...
- BZOJ3224 Tyvj 1728 普通平衡树(Treap)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【权值分块】bzoj3224 Tyvj 1728 普通平衡树
权值分块和权值线段树的思想一致,离散化之后可以代替平衡树的部分功能. 部分操作的时间复杂度: 插入 删除 全局排名 全局K大 前驱 后继 全局最值 按值域删除元素 O(1) O(1) O(sqrt(n ...
- 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会
平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...
随机推荐
- c++ fstream中seekg()和seekp()的用法
转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...
- spark-shell - 将结果保存成一个文件
sqlContext.sql(""" SELECT user_no,cust_id,oper_code FROM cui.operation_data_an ...
- Jquery环境搭建前言
- express开发实例
express获取参数有三种方法:官网介绍如下 Checks route params (req.params), ex: /user/:id Checks query string params ( ...
- Laravel 安装记录
系统环境:ubuntu-14.10-server-i386(32) LAMP 系统默认安装 根据:http://laravel.com/docs/5.1 官方文档,准备通过 Composer 安装 ...
- 4070: [Apio2015]雅加达的摩天楼
Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 “do ...
- Array 原型扩展(快速排序,搅乱顺序)
/// 快速快速排序算法Array.prototype.quickSort = function (left, right) { // left = left || 0; // right = rig ...
- 解决java.lang.NoClassDefFoundError: org.jdom.Content
报错:java.lang.NoClassDefFoundError: org.jdom.Content 解决办法: 更新lib目录下的jdom.jar 至最新版本.
- 探索React生态圈
原文地址:http://www.csdn.net/article/2015-08-04/2825370-react 2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲 ...
- uva 11437 - Triangle Fun
计算几何: 直线交点: #include<cstdio> using namespace std; struct node { double x,y; node(,):x(x),y(y){ ...