BZOJ3224:普通平衡树(splay练习)
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
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 Input10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output106465
84185
492737
Hint
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct Splay{
int ch[maxn][],fa[maxn],num[maxn],sz[maxn],key[maxn],rt,cnt;
int get(int x) { return ch[fa[x]][]==x;}
Splay(){ rt=cnt=; }
void update(int Now)
{
if(!Now) return ;
sz[Now]=num[Now];
if(ch[Now][]) sz[Now]+=sz[ch[Now][]];
if(ch[Now][]) sz[Now]+=sz[ch[Now][]];
}
void rotate(int x)
{
int old=fa[x],fold=fa[old],opt=(ch[old][]==x);
fa[ch[x][opt^]]=old; ch[old][opt]=ch[x][opt^];
ch[x][opt^]=old; fa[old]=x; fa[x]=fold;
if(fold) ch[fold][ch[fold][]==old]=x;
else rt=x;
update(old); update(x);
}
void splay(int x,int y)
{
for(int f;(f=fa[x])!=y;rotate(x)){
if(fa[f]!=y)
rotate(get(x)==get(f)?f:x);
}
if(!y) rt=x;
}
void insert(int x)
{
if(!rt){
rt=++cnt; key[cnt]=x; sz[cnt]=num[cnt]=; return ;
}
int Now=rt,f=;
while(true){
if(key[Now]==x){
num[Now]++; update(Now); update(f); splay(Now,); return;
}
f=Now; Now=ch[Now][key[Now]<x];
if(!Now) {
fa[++cnt]=f; ch[f][key[f]<x]=cnt; num[cnt]=sz[cnt]=;
key[cnt]=x; update(f); splay(cnt,); return ;
}
}
}
void del(int x)
{
int whatever=find(x);
if(num[rt]>){num[rt]--; update(rt); return;}
if(!ch[rt][]&&!ch[rt][]) { rt=; return;}
if(!ch[rt][]){
int oldroot=rt; rt=ch[rt][]; fa[rt]=; return;
}
else if (!ch[rt][]){
int oldroot=rt; rt=ch[rt][]; fa[rt]=; return;
}
int leftbig=pre(),oldroot=rt;
splay(leftbig,);
ch[rt][]=ch[oldroot][];
fa[ch[oldroot][]]=rt;
update(rt);
}
int find(int x)
{
int Now=rt,res=;
while(true){
if(x<key[Now]) Now=ch[Now][];
else {
res+=ch[Now][]?sz[ch[Now][]]:;
if(key[Now]==x) {
splay(Now,);return res+;
}
res+=num[Now]; Now=ch[Now][];
}
}
}
int findx(int x)
{
int Now=rt;
while(true){
if(ch[Now][]&&sz[ch[Now][]]>=x) Now=ch[Now][];
else {
if(ch[Now][]) x-=sz[ch[Now][]];
if(num[Now]>=x) return key[Now];
x-=num[Now];
Now=ch[Now][];
}
}
}
int pre()
{
int Now=ch[rt][];
while(ch[Now][]) Now=ch[Now][];
return Now;
}
int nxt()
{
int Now=ch[rt][];
while(ch[Now][]) Now=ch[Now][];
return Now;
}
}S;
int main()
{
int N,opt,x;
scanf("%d",&N);
while(N--){
scanf("%d%d",&opt,&x);
switch(opt){
case : S.insert(x); break;
case : S.del(x); break;
case : printf("%d\n",S.find(x)); break;
case : printf("%d\n",S.findx(x)); break;
case : S.insert(x); printf("%d\n",S.key[S.pre()]); S.del(x);break;
case : S.insert(x); printf("%d\n",S.key[S.nxt()]); S.del(x);break;
}
}
return ;
}
BZOJ3224:普通平衡树(splay练习)的更多相关文章
- bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
- BZOJ3224:普通平衡树(Splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
- 【转】 史上最详尽的平衡树(splay)讲解与模板(非指针版spaly)
ORZ原创Clove学姐: 变量声明:f[i]表示i的父结点,ch[i][0]表示i的左儿子,ch[i][1]表示i的右儿子,key[i]表示i的关键字(即结点i代表的那个数字),cnt[i]表示i结 ...
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
- Hihocoder 1329 平衡树·Splay(平衡树)
Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
随机推荐
- python019 Python3 File(文件) 方法
file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...
- XV6环境搭建及注意事项
Ubuntu16.04SLT 64位 工具链 sudo apt-get install gcc-multilib libsdl1.2-dev, libtool-bin, libglib2.0-dev, ...
- 【三分+精度问题】G. Toxophily
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/G [题意] 已知人的坐标在(0,0),靶的位置在(x,y),人以速度v射箭并且射 ...
- HDU 2352 Verdis Quo
罗马数字转化为十进制的值 题目非常的长 提取有效信息 并且介绍很多规则 但是事实上有用的信息就是如何加 什么时候减 当当前字母小于下一个字母时 减去当前字母的值 #include <iostre ...
- CF788E:New task
n个数字中,每个数有数字A和属性B,每次操作将某个点x的属性B改变为0或1,求满足这样要求的子序列的个数: 下标a<b<c<d<e,而Aa<=Ab=Ac=Ad>=A ...
- PatentTips - Solid State Disk (SSD) Device
BACKGROUND OF THE INVENTION A SSD apparatus is a large-capacity data storage device using a nonvolat ...
- CodeForces 592C The Big Race
公倍数之间的情况都是一样的,有循环节. 注意min(a,b)>t的情况和最后一段的处理.C++写可能爆longlong,直接Java搞吧...... import java.io.Buffere ...
- 分布式RPC框架性能大比拼
https://github.com/grpc/grpc http://colobu.com/2016/09/05/benchmarks-of-popular-rpc-frameworks/ http ...
- HDU1087 Super Jumping! Jumping! Jumping!(LIS)
题目意思: http://acm.hdu.edu.cn/showproblem.php? pid=1087 此题的意思求最长上升子序列的和. 题目分析: 在求最长上升子序列的时候,不在保存最长的个数, ...
- CentOS 更改Apache默认网站目录
http://www.osyunwei.com/archives/789.html引言:Apache默认的网站目录是在/var/www/html, 现在要把网站目录更改到/home/wwwroot/w ...