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的模板竟 ...
随机推荐
- Saltstack远程执行(四)
Saltstack远程执行 语法例:salt '*' cmd.run 'w' - 命令:salt - 目标:'*' - 模块:cmd.run,自带150+模块,也可以自己写模块 - 返回:执行 ...
- jQuery $.fn 方法扩展~
//以下代码紧跟在引进的jquery.js代码后面 <script type="Text/JavaScript"> $(function (){ //扩展myName方 ...
- javascript字符串截取的substring、substr和slice
本文详细的介绍了javascript中substring().substr()和slice()三个JS字符串截取的方法,substring()方法用于提取字符串中介于两个指定下标之间的字符.subst ...
- VirtualBox5中安装的CentOS6.7安装增强工具
1.安装编译内核的相关组件 yum install kernel-devel gcc 2.安装VirtualBox增强功能 sh ./VBoxLinuxAdditions.run 3.重启系统 reb ...
- thinkphp安装 版本 3.1.3
基础版: 只有thinkphp基础运行功能 完整版:基础运行能力,还有图片.上传等各种处理类(建议下载完整版) 重要的三个变量 define('APP_DEBUG',True); // 定义应用目录d ...
- imageserver
https://bitbucket.org/tamtam-nl/tamtam-nuget-imageserver/overview https://www.nuget.org/packages/Tam ...
- margin-bottom在IE6和IE7下失效的解决办法
IE6/7下margin-bottom无效一般出现在容器里某元素设置后在父容器内无效,这个时候只需要在父容器中加入以下两句css,基本上所有的浏览器都兼容了: overflow:hidden;zoom ...
- 2014 牡丹江区域赛 B D I
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...
- [歪谈]我们该怎么正确面对"批评"
这两天看到网上有类似这样的话题:遇到批评你是如何面对? 其实标题中没有“领导”,并不是专指:遇到“领导”批评你是如何面对? 在IT界(其他行业和领域就不谈了). 批评分三个层面: 1. ...
- Linux运维初级教程(一)Shell脚本
序,掌握shell脚本是linux运维工程师的最基础技能. 一.脚本语言 与高级语言不通,脚本语言运行时需要调用相应的解释器来翻译脚本中的内容. 常见的脚本语言有Shell脚本.Python脚本.ph ...