BZOJ3224——Tyvj 1728 普通平衡树
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 普通平衡树的更多相关文章
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- bzoj3224: Tyvj 1728 普通平衡树(平衡树)
bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...
- bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5354 Solved: 2196[Submit][Sta ...
- 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] 普通平衡树 (treap)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...
- 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的模板竟 ...
随机推荐
- python BeautifulSoup4
source form http://www.bkjia.com/ASPjc/908009.html 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Py ...
- 日志模块logging使用心得
在应用程序使用中,日志输出对应用维护人员.开发人员判断程序的问题起重要作用. 那么在python中如何定义程序的日志输出? 推荐使用日志模块logging 需求:实现日志内容输出在文件中和控制器中 i ...
- Yocto开发笔记之《根文件系统裁剪》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...
- mac系统的一些操作常识
mac系统如何显示和隐藏文件 苹果Mac OS X操作系统下,隐藏文件是否显示有很多种设置方法,最简单的要算在Mac终端输入命令.显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写): 显 ...
- StringBuilder 和 StringBuffer
这两者唯一的不同就在于,StringBuffer是线程安全的,而StringBuilder不是.当然线程安全是有成本的,影响性能,而字符串对象及操作,大部分情况下,没有线程安全的问题,适合使用Stri ...
- jquery 获取json文件数据,显示到jsp页面上, 或者html页面上
[{"name":"中国工商银行","code":102},{"name":"中国农业银行",&qu ...
- Rime 鼠须管 配色方案
android: name: "安卓/Android" author: "Patricivs ipatrickmac@me.com" text_color: 0 ...
- 菜鸟疑问之新建网站与新建web应用程序区别
学习asp.net一定少不了这困惑:新建网站与新建web应用程序有什么区别? 新建web应用程序 新建网站 呵呵,其实从名字看一个是webApplication,一个是webSite.名字不同罢了? ...
- centos 7.0 安装
最小化安装的 主要查看硬盘使用时间 需要安装 smartmontools 这个 [root@localhost ~]# yum install -y smartmontools 已加载插件:fast ...
- GoLang之基础
GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...