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. [linux] LVM原磁盘扩容(非增加磁盘)

    阿里云机器,ECS运行时磁盘由300G扩容到500,重启机器生效后登录.可以看到/data目录总大小300G,/dev/vdb已经扩容到500G. [root@HD1g-elasticsearch2 ...

  2. PSP Daily新增功能说明书

    1.选择输入类别时可以记录原来的输入,支持用户选择记录清空功能 2.添加“恢复最近”button,点击这个按钮可以跳出一个页面显示最近的excel记录,用户可以通过勾选相应的excel文件名,恢复选中 ...

  3. Daily Scrumming* 2015.10.29(Day 10)

    一.总体情况总结 二.今明两天任务表 Member Today’s Task Tomorrow’s Task 江昊 了解微信API,与社团服务平台创业公司嗨社团创始人沟通,了解其平台运营方案与商业模式 ...

  4. spring冲刺第四天

    昨天进行了地图的初步编写,但是存在BUG. 今天上网查找了错误的原因,改进了源代码,使程序可以执行. 遇到的问题;感觉地图界面太简单,需要作出更多的场景,这就需要不断的完善.

  5. SqlServer中的dbo是什么意思

    出处:http://andylin02.iteye.com/blog/486296 SqlServer中的dbo是什么意思? DBO是每个数据库的默认用户,具有所有者权限,即DbOwner 通过用DB ...

  6. 浅学CSS

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 属性(property)是您希望设置的样式属性(sty ...

  7. 正则化项L1和L2

    本文从以下六个方面,详细阐述正则化L1和L2: 一. 正则化概述 二. 稀疏模型与特征选择 三. 正则化直观理解 四. 正则化参数选择 五. L1和L2正则化区别 六. 正则化问题讨论 一. 正则化概 ...

  8. Docker使用阿里云镜像加速

    1.进入阿里云镜像加速页面: https://cr.console.aliyun.com/#/accelerator 2.修改/etc/docker/daemon.json文件配置,没有则新建: { ...

  9. startActivityForResult()的用法(超好用啊)

    最近做的一个小东西遇到这样的情况,我从一个页面MainActivity修改一些内容,需要跳转到一个新的EditActivity去做修改操作,修改完成后就回到之前的MainActivity,因为信息被修 ...

  10. Linux命令01

    Linux简介及Ubuntu安装 Linux,免费开源,多用户多任务系统.基于Linux有多个版本的衍生.RedHat.Ubuntu.Debian 安装VMware或VirtualBox虚拟机.具体安 ...