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. 浅谈B树

    B树即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如:    ...

  2. linux awk 内置函数详细介绍(实例)

    这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...

  3. sharepoint修改密码

    增加SharePoint2010修改域密码功能 前提SharePoint2010的用户基于AD的,因此修改密码是修改了AD的密码,当然也可以修改本机密码(非域的密码).这里我们讨论修改域密码.我们修改 ...

  4. c++关键字之#define typedef const

    [#define] #define是预处理指令,在编译预处理时进行简单的替换,不作正确性检查. [typedef] typedef只是为了增加可读性而为标识符另起的新名称 在自己的作用域内给一个已经存 ...

  5. kettle使用log4j管理输出日志

    在使用kettle进行数据分析和清洗时日志非常多而且杂乱,使用原有的日志有时找不到异常的位置,有时日志不够详细,说简单一点就是日志不是我们想要的.因而对kettle日志进行相应的管理就想得尤为重要了. ...

  6. Delphi经验总结(3)

    ------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...

  7. (原创)Python文件与文件系统系列(2)——os模块对文件、文件系统操作的支持

    os模块的功能主要包括文件系统部分和进程管理部分,这里介绍其中与文件系统相关的部分. 当请求操作系统执行操作失败时,os模块抛出内置异常 exceptions.OSError 的实例,可以通过 os. ...

  8. DFS:Lake Counting(POJ 2386)

    好吧前几天一直没更新博客,主要是更新博客的确是要耗费一点精力 北大教你数水坑 最近更新博客可能就是一点旧的东西和一些水题,主要是最近对汇编感兴趣了嘻嘻嘻 这一题挺简单的,没什么难度,简单深搜 #inc ...

  9. codeforces 474D.Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/474/D 题目意思:Marmot 吃两种类型的花(实在难以置信呀--):red 或者 white,如果要吃 ...

  10. codeforces A. The Wall 解题报告

    题目链接:http://codeforces.com/problemset/problem/340/A 这道题目理解不难,就是在[a, b]区间内,找出同时能够被x和y整除的个数.第一次想当然的开了两 ...