BZOJ3224:普通平衡树(Splay)
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
Solution
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN (100000+10)
using namespace std;
int Father[MAXN];
int Son[MAXN][];
int Key[MAXN];//结点代表的数字
int Cnt[MAXN];//结点代表数字出现次数
int Size[MAXN];//子树大小(含自身)
int Root,sz,n,opt,x; void Clear(int x){Father[x]=Son[x][]=Son[x][]=Key[x]=Cnt[x]=Size[x]=;}
void New(int x){Size[++sz]=;Key[sz]=x;Cnt[sz]=;}
void Update(int x){Size[x]=Cnt[x]+Size[Son[x][]]+Size[Son[x][]];}
int Get(int x){return x==Son[Father[x]][];}
int Pre(){int now=Son[Root][]; while (Son[now][]) now=Son[now][];return now;}
int Next(){ int now=Son[Root][];while (Son[now][]) now=Son[now][];return now;} void Rotate(int x)
{
int wh=Get(x);
int fa=Father[x],fafa=Father[fa];
Son[fa][wh]=Son[x][wh^];
Father[fa]=x;
if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
Son[x][wh^]=fa;
Father[x]=fafa;
if (fafa) Son[fafa][Son[fafa][]==fa]=x;
Update(fa);
Update(x);
} void Splay(int x)
{
for (int fa; fa=Father[x]; Rotate(x))
if (Father[fa])
Rotate(Get(fa)==Get(x)?fa:x);
Root=x;
} int Find(int x)
{
int ans=,now=Root;
while ()
if (x<Key[now])
now=Son[now][];
else
{
ans+=Size[Son[now][]];
if (x==Key[now])
{
Splay(now);
return ans+;
}
ans+=Cnt[now];
now=Son[now][];
}
} int Findx(int x)
{
int now=Root;
while ()
if (x<=Size[Son[now][]])
now=Son[now][];
else
{
x-=Size[Son[now][]];
if (x<=Cnt[now])
{
Splay(now);
return Key[now];
}
x-=Cnt[now];
now=Son[now][];
}
} void Insert(int x)
{
if (Root==)
{
New(x);
Root=sz;
return;
}
int now=Root,fa=;
while ()
{
if (x==Key[now])
{
Cnt[now]++;
Update(now);
Splay(now);
return;
}
fa=now,now=Son[now][Key[now]<x];
if (now==)
{
New(x);
Father[sz]=fa;
Son[fa][x>Key[fa]]=sz;
// Update(fa);
Splay(sz);
return;
}
}
} void Delete(int x)
{
Find(x);
if (Cnt[Root]>)
{
--Cnt[Root];
Update(Root);
return;
}
if (!Son[Root][] && !Son[Root][])
{
Clear(Root);
Root=;
return;
}
if (!Son[Root][])
{
Root=Son[Root][];
Clear(Father[Root]);
Father[Root]=;
return;
}
if (!Son[Root][])
{
Root=Son[Root][];
Clear(Father[Root]);
Father[Root]=;
return;
}
int old=Root;
Splay(Pre());
Son[Root][]=Son[old][];
Father[Son[old][]]=Root;
Clear(old);
Update(Root);
} int main()
{
scanf("%d",&n);
for (int i=; i<=n; ++i)
{
scanf("%d%d",&opt,&x);
if (opt==) Insert(x);
if (opt==) Delete(x);
if (opt==) printf("%d\n",Find(x));
if (opt==) printf("%d\n",Findx(x));
if (opt==) Insert(x),printf("%d\n",Key[Pre()]),Delete(x);
if (opt==) Insert(x),printf("%d\n",Key[Next()]),Delete(x);
}
}
BZOJ3224:普通平衡树(Splay)的更多相关文章
- bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
- [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,终于做对 ...
随机推荐
- guava快速入门(一)
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support] ...
- WPF的MediaElement指定Source无法播放问题解决
最近学wpf,在使用 MediaElement 指定 Source 进行视频播放时,在源码界面可以正常显示,但运行时控件显示空白. 源码界面如下图:(可正常显示) 运行后如下图所示:(控件位置显示空白 ...
- c# 旋转图片 无GDI+一般性错误
using (System.Drawing.Bitmap backgroudImg = System.Drawing.Bitmap.FromFile(DoubleClickPicInfo.FileNa ...
- Docker学习之Docker容器基本使用
Docker学习之Docker容器基本使用 新建容器并启动 命令格式:docker run --options repository:tag 后台运行 命令格式:-d 已存在的容器相关操作 启动:do ...
- 一、Java多线程基础
一.简介 1.操作系统 在早起的裸机时代,计算机非常地昂贵,而且也没有操作系统的概念,计算机从头到尾只能执行一个程序.如果程序在执行一个耗时的操作,那么在这个过程中,计算机就有大量的资源闲置在那里,这 ...
- Bash on windows从14.0升级到ubuntu16.04
升级参考:https://www.zhihu.com/question/49411626 解决中文乱码问题参考:http://www.lofter.com/tag/ubuntu%E5%AD%90%E7 ...
- JavaWeb学习总结(一):基本概念
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- Android之NDK环境配置+JNI开发+so文件编译
前言 这边Android作为日常记录,虽然破坏了文章队形~ 最近人工智能挺火的,也稍微了解了一些库,比如关于视觉库openCV.要在安卓下调用这些C/C++库,需要用到JNI开发,在此把过程分享一 ...
- ajax回调中执行window.open被拦截的解决办法
From:https://blog.csdn.net/winy_lm/article/details/60959751 ajax async设为false即同步调用 //去支付function got ...
- xfs参数简介
age_buffer_centisecs age_buffer_centisecs:(Min: 100 Default: 1500 Max: 720000) 多长时间设置为脏数据 xfsbufd_ ...