http://www.lydsy.com/JudgeOnline/problem.php?id=3224

无力吐槽,无力吐槽,无力吐槽.......

bzoj竟然不能用time(0)我竟然不造!!re成一片。。。。。

(不管re没re,我也在我程序中找到了很多bug,,,一一修复了。。我的treap写的真渣。

这次我发现了treap的很多问题,有一个细节的地方。就是null的weight必须要最大(或最小),你的堆是最小(或最大)的话,所以要将null的weight的初值设置一下,否则在删除操作的时候会吧null旋转上去。。然后就,,

这个题还有一个hentai的地方,就是求前驱后继以及排名和第k小,全是坑,首先是有多个相同的要最小的,然后又是求前驱后继不是在树里面有的。。经过观赏大神们的代码,我一一解决了。现在放上代码

#include <cstdio>
#include <cstdlib> using namespace std;
const int oo=~0u>>1; struct Treap {
struct node {
node* ch[2];
int key, size, wei, cnt; //多加一个维
node(int _key, node* f) { ch[0]=ch[1]=f; key=_key; size=cnt=1; wei=rand(); }
void pushup() { size=ch[0]->size+ch[1]->size+cnt; } //用cnt来更新
}*null, *root;
Treap() {
null=new node(0, 0);
null->size=null->cnt=0; null->wei=oo; //细节
root=null;
}
void rot(node* &rt, bool d) {
node* c=rt->ch[!d]; rt->ch[!d]=c->ch[d]; c->ch[d]=rt;
rt->pushup(); c->pushup();
rt=c;
}
void insert(const int &key, node* &rt) {
if(rt==null) { rt=new node(key, null); return; }
if(key==rt->key) {
rt->cnt++; rt->size++;
return;
}
bool d=key>rt->key;
insert(key, rt->ch[d]);
if(rt->wei>rt->ch[d]->wei) rot(rt, !d); //我是弄成小根堆
rt->pushup();
}
void remove(const int &key, node* &rt) {
if(rt==null) return;
bool d=key>rt->key;
if(key==rt->key) {
if(rt->cnt>1) { rt->cnt--; rt->size--; return; }
d=rt->ch[0]->wei>rt->ch[1]->wei; //巧妙的用上了null的weight最大
if(rt->ch[d]==null) {
delete rt;
rt=null;
return;
}
rot(rt, !d);
remove(key, rt->ch[!d]);
}
else remove(key, rt->ch[d]);
rt->pushup();
}
node* select(int k, node* rt) {
int s=rt->ch[0]->size+rt->cnt;
if(k>=rt->ch[0]->size+1 && k<=s) return rt; //这里要注意,因为有多个相同值,所以要判断区间
if(s>k) return select(k, rt->ch[0]);
else return select(k-s, rt->ch[1]);
}
int rank(const int &key, node* rt) {
if(rt==null) return 0;
int s=rt->ch[0]->size+rt->cnt;
if(key==rt->key) return rt->ch[0]->size+1; //这里要注意,返回的要是第一个
if(key<rt->key) return rank(key, rt->ch[0]);
else return s+rank(key, rt->ch[1]);
}
int suc(const int &k) {
node* t=root;
int ret=0;
while(t!=null) {
if(t->key>k) {
ret=t->key;
t=t->ch[0];
}
else t=t->ch[1];
}
return ret;
}
int pre(const int &k) {
node* t=root;
int ret=0;
while(t!=null) {
if(t->key<k) {
ret=t->key;
t=t->ch[1];
}
else t=t->ch[0];
}
return ret;
}
}; int main() {
int n, a, b;
Treap tree;
scanf("%d", &n);
while(n--) {
scanf("%d%d", &a, &b);
if(a==1) tree.insert(b, tree.root);
else if(a==2) tree.remove(b, tree.root);
else if(a==3) printf("%d\n", tree.rank(b, tree.root));
else if(a==4) printf("%d\n", tree.select(b, tree.root)->key);
else if(a==5) printf("%d\n", tree.pre(b));
else if(a==6) printf("%d\n", tree.suc(b));
}
return 0;
}

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

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000
2.每个数的数据范围:[-1e7,1e7]

Source

【BZOJ】3224: Tyvj 1728 普通平衡树(某不科学的oj)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 22483  Solved: 10130[Submit][S ...

  7. BZOJ 3224 Tyvj 1728 普通平衡树模板

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...

  8. bzoj 3224: Tyvj 1728 普通平衡树 && loj 104 普通平衡树 (splay树)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.ne ...

  9. bzoj 3224/Tyvj 1728 普通平衡树(splay)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  10. fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树

    题面:[模板]普通平衡树 代码: #include<cstdio> #include<cstring> #include<iostream> #include< ...

随机推荐

  1. HDOJ 1203 I NEED A OFFER!(01背包)

    10397507 2014-03-25 23:30:21 Accepted 1203 0MS 480K 428 B C++ 泽泽 题目链接:http://acm.hdu.edu.cn/showprob ...

  2. css 中的度量单位

    px 相对长度单位.像素(Pixel). 像素是相对于显示器屏幕分辨率而言的.譬如,WONDOWS的用户所使用的分辨率一般是96像素/英寸.而MAC的用户所使用的分辨率一般是72像素/英寸. em 相 ...

  3. object-c 基本数据类型

    1.基本数据类型   int  float  double  char   布尔类型   枚举类型 2.对象类型和id类型  就是类类型或协议所声明的指针类型.  id类型可以表示任何类型,一般只表示 ...

  4. 25.在从1到n的正数中1出现的次数[NumberOf1Between1_N]

    [题目] 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. [分析] 这是一道广为流传的goo ...

  5. 配置SecureCRT连接本地虚拟机中的Linux系统

    转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...

  6. 大白菜U盘启动盘制作工具V5.0如何制作启动系统U盘

    1:切换到ISO模式或者直接点击主程序左上角的ISO制作,程序会切换到ISO制作界面. 2:在路径里选好ios文件,点击按钮. 3:打开ISO模式的一键制作启动U盘,点击ISO模式里的按钮,按照图中推 ...

  7. 在qq中可以使用添加标签功能

    而在sina中不可以,现在就保持一致吧!那么每天使用的日志主要是记录工作项目上的问题还有生活的感受

  8. CI当开启URL重写的时候,报错500 Internal Server Error

    Internal Server Error The server encountered an internal error or misconfiguration and was unable to ...

  9. rabbitMq使用(mac平台)

    1.下载 wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.3/rabbitmq-server-mac-standalone-3.5 ...

  10. linux下用top命令查看cpu利用率超过100%

    今天跑了一个非常耗时的批量插入操作..通过top命令查看cpu以及内存的使用的时候,cpu的时候查过了120%..以前没注意..通过在top的情况下按大键盘的1,查看的cpu的核数为4核. 通过网上查 ...