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的模板竟 ...
随机推荐
- javascript 字符串加密的几种方法
8进制 /*8进制加密*/ function EnEight(){ var monyer = new Array();var i,s; for(i=0;i<txt.value.length;i+ ...
- Jsonp类
public class JsonpResult : JsonResult { public JsonpResult() { this.Callback = "callback"; ...
- MYSQL版本问题:解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future.
- netcat命令
1 简介 netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netc ...
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【外传】——Attribute Routing
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 题外话:由于这个技术点是新学的,并不属于原系列,但借助了原系列的项目背景,故命名外传系列,以后也可 ...
- yourphp添加KindEditor编辑器
<tr> <td align="right">故障描述</td> <script charset="utf-8" sr ...
- vertical-align0 垂直对齐- 图片 兼容个浏览器
效果: 代码: <html> <head> <style type="text/css"> img.top {vertical-align:t ...
- ServiceBase 备份
using CanDoo.Contracts; using CanDoo.Core.Data; using System; using System.Collections.Generic; usin ...
- Jetty多Connector
有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解 ...
- 解决pydev无法增加jython271 interpreter的问题
============================解决pydev无法增加jython271 interpreter的问题============================ 从jython. ...