3224: Tyvj 1728 普通平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 16656  Solved: 7255
[Submit][Status][Discuss]

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.每个数的数据范围:[-2e9,2e9]

Source

贴模板:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int fa[MAXN],ch[MAXN][],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{
root=sz=;
memset(ch,,sizeof(ch));
memset(fa,,sizeof(fa));
memset(cnt,,sizeof(cnt));
memset(size,,sizeof(size));
}
inline void clear(int x) { fa[x]=ch[x][]=ch[x][]=cnt[x]=size[x]=; }
inline int get(int x) { return ch[fa[x]][]==x; }
inline void update(int x)
{
if(x){
size[x]=cnt[x];
if(ch[x][]) size[x]+=size[ch[x][]];
if(ch[x][]) size[x]+=size[ch[x][]];
}
}
inline void rotate(int x)
{
int father=fa[x],ffather=fa[father],which=get(x);
ch[father][which]=ch[x][!which];fa[ch[father][which]]=father;
ch[x][!which]=father;fa[father]=x;
fa[x]=ffather;
if(ffather) ch[ffather][ch[ffather][]==father]=x;
update(father);
update(x);
}
inline void splay(int x)
{
for(int father;(father=fa[x]);rotate(x))
if(fa[father])
rotate((get(x)==get(father)?father:x));
root=x;
}
inline void insert(int x)
{
if(root==) { root=++sz;fa[sz]=ch[sz][]=ch[sz][]=;cnt[sz]=size[sz]=;key[sz]=x;return; }
int now=root,father=;
while(){
if(key[now]==x) { cnt[now]++;update(now);update(father);splay(now);return; }
father=now;
now=ch[father][key[now]<x];
if(now==){
sz++;
fa[sz]=father;
ch[father][key[father]<x]=sz;
ch[sz][]=ch[sz][]=;
cnt[sz]=size[sz]=;
key[sz]=x;
update(father);
splay(sz);
return;
}
}
}
inline int find(int x)//找到x的位置
{
int now=root,ans=;
while(){
if(x<key[now]) now=ch[now][];
else{
if(ch[now][]) ans+=size[ch[now][]];
if(x==key[now]) { splay(now);return ans+; }
ans+=cnt[now];
now=ch[now][];
}
}
}
inline int rank(int x)//找到排名为x的数
{
int now=root;
while(){
if(ch[now][]&&x<=size[ch[now][]]) now=ch[now][];
else{
int tmp=(ch[now][]?size[ch[now][]]:)+cnt[now];
if(x<=tmp) return key[now];
x=x-tmp;
now=ch[now][];
}
}
}
inline int pre()//找前驱
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];
return now;
}
inline int suf()//找后继
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];
return now;
}
inline void del(int x)//删去一个x
{
find(x);
if(cnt[root]>) { cnt[root]--;update(root);return; }
else if(!ch[root][]&&!ch[root][]) { root=sz=;clear(root);return; }
else if(!ch[root][]){
int oldroot=root;
root=ch[root][];
fa[root]=;
clear(oldroot);
return;
}else if(!ch[root][]){
int oldroot=root;
root=ch[root][];
fa[root]=;
clear(oldroot);
return;
}
int leftbig=pre(),oldroot=root;
splay(leftbig);
ch[root][]=ch[oldroot][];
fa[ch[root][]]=root;
clear(oldroot);
update(root);
}
int main()
{
//freopen("in.txt","r",stdin);
int n,a,b;
init();
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
if(a==) insert(b);
else if(a==) del(b);
else if(a==) printf("%d\n",find(b));
else if(a==) printf("%d\n",rank(b));
else if(a==) { insert(b);printf("%d\n",key[pre()]);del(b); }
else if(a==) { insert(b);printf("%d\n",key[suf()]);del(b); }
}
return ;
}

bzoj 3224的更多相关文章

  1. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  2. BZOJ 3224 普通平衡树(Treap模板题)

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

  3. [bzoj 3224]手写treap

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...

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

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

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

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

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

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

  7. BZOJ 3224 普通平衡树

    这个是第一份完整的treap代码.嗯...虽然抄的百度的版,但还是不错的. !bzoj上不能用srand. #include<iostream>#include<cstdio> ...

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

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

  9. BZOJ 3224 普通平衡树(树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3224 题意:维护以下操作:(1)插入x:(2)删除x(若有多个相同的数,只删除一个)(3 ...

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

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

随机推荐

  1. java控制台编译通过,运行出现找不到或无法加载主类的情况

    参考链接:http://www.knowsky.com/1046493.html 当建了一个包之后(假设建的包的名字为com),找到该java文件的com目录,发现编译能够通过,但是运行的时候出现了一 ...

  2. spring boot的maven项目报404错误

    $.ajax({ async: false, type: "POST", url:'searchFileSource', contentType : "applicati ...

  3. Data truncation: Truncated incorrect DOUBLE value:

    在写sql查询语句queryRunner.update(connection,"update account set balance=? where name=?",account ...

  4. windows多线程(六) 互斥量Mutex与关键段CriticalSection比较

    一.关键段CS 和 互斥量Mutex 的相同点:都有线程拥有权 关键段和互斥量都有线程拥有权,即可以被一个线程拥有.在 前面讲关键段CS的文章中有说到,关键段结构体的第四个参数保存着拥有该关键段的线程 ...

  5. linux使用密文生成os账户

    1.生成账户 [hufangrui@xxx ~]$ openssl passwd -1Password: Verifying - Password: $1$szzkROBZ$GYxffRLp8K5vW ...

  6. Fastqc使用说明

    用FastQC检查二代测序原始数据的质量 2013-01-28 21:28:10|  分类: Bioinformatics |  标签:bioinformatics  deep-seq   |举报 | ...

  7. 实现LinearLayout(垂直布局,Gravity内容排布)

    首先上Gravity的代码,Android原版的Gravity搞得挺复杂的,太高端了.但基本思路是使用位运算来做常量,我就自己消化了一些,按自己的思路来实现. 先上代码,在做分析. package k ...

  8. python利用unittest测试框架组织测试用例的5种方法

    利用unittest测试框架可以编写测试用例,执行方式分两大类:利用main方法和利用testsuite,其中利用测试套件来组织测试用例可以有4种写法. 在此之前,先了解几个概念 TestCase:所 ...

  9. 浅谈C++/JAVA/C#运行机制和执行效率

    估计有很多同学都对C++/JAVA/C#这三大热门语言的运行机制和执行效率有或多或少的困惑,自己也有,但是经过前期的学习,了解了三者在这两方面的区别,就废话不说了,进入主题吧.         一.运 ...

  10. Nim游戏学习笔记