bzoj 3224
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
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
84185
492737
HINT
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的更多相关文章
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- BZOJ 3224 普通平衡树(Treap模板题)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14301 Solved: 6208 [Submit][ ...
- [bzoj 3224]手写treap
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9629 Solved: 4091[Submit][Sta ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- BZOJ 3224: Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ 3224 普通平衡树
这个是第一份完整的treap代码.嗯...虽然抄的百度的版,但还是不错的. !bzoj上不能用srand. #include<iostream>#include<cstdio> ...
- BZOJ 3224: Tyvj 1728 普通平衡树 vector
3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...
- BZOJ 3224 普通平衡树(树状数组)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3224 题意:维护以下操作:(1)插入x:(2)删除x(若有多个相同的数,只删除一个)(3 ...
- BZOJ 3224: Tyvj 1728 普通平衡树(BST)
treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...
随机推荐
- 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!
本文主要展示的是通过使用python和PyInstaller来构建恶意软件的一些poc. 利用Python编写Windows恶意代码!自娱自乐!勿用于非法用途!众所周知的,恶意软件如果影响到了他人的生 ...
- 第35次Scrum会议(11/23)【欢迎来怼】
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文小组照片 二.开会信息 时间:2017/11/23 17:03~17:24,总计21min.地点:东北师 ...
- HDU 1003 最大连续子段和
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...
- APP案例分析之华为浏览器
第一部分 对华为浏览器的调研,评测 1.对华为浏览器的第一次上手体验 我会使用华为浏览器呢,是因为我买的手机是华为nova,该浏览器也是手机里面预装的.刚开始用的时候也没太注意,感觉跟以前用的其他浏 ...
- EditorUtility类的说明
SetDirty这个函数告诉引擎,相关对象所属的Prefab已经发生更改. IsPersistent用于判定对象是否是被保存在硬盘中 DisplayDialog显示一个对话框,有OK,Cancel按钮 ...
- keil c51笔记
第一章 Keil C51开发系统基本知识 第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性. ...
- 深入理解JAVA I/O系列三:字符流详解
字符流为何存在 既然字节流提供了能够处理任何类型的输入/输出操作的功能,那为什么还要存在字符流呢?容我慢慢道来,字节流不能直接操作Unicode字符,因为一个字符有两个字节,字节流一次只能操作一个字节 ...
- Promise 记录
- 【第二周】PSP
日期 C类别 C内容 S开始时间 E结束时间 I间隔(单位:分钟) T净时间(单位:分钟) 9月8日 编程 结对编程 12:15 13:15 10 50 编程 结对编程 16:35 17:30 ...
- [转帖]脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么? http://www.52im.net/thread-1732-1-1.html 1.引言 本文接上篇<脑残式网 ...