[luogu3369] 普通平衡树(splay模板)
题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1.插入 xx 数
2.删除 xx 数(若有多个相同的数,因只删除一个)
3.查询 xx 数的排名(排名定义为比当前数小的数的个数 +1+1 。若有多个相同的数,因输出最小的排名)
4.查询排名为 xx 的数
5.求 xx 的前驱(前驱定义为小于 xx ,且最大的数)
6.求 xx 的后继(后继定义为大于 xx ,且最小的数)
输入输出格式
输入格式:
第一行为 nn ,表示操作的个数,下面 nn 行每行有两个数 optopt 和 xx , optopt 表示操作的序号( 1 \leq opt \leq 6 1≤opt≤6 )
输出格式:
对于操作 3,4,5,63,4,5,6 每行输出一个数,表示对应答案
输入输出样例
输入样例#1:
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
输出样例#1:
106465
84185
492737
说明

code:
//By Menteur_Hxy
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define M(a,b) memset(a,(b),sizeof(a))
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define C(i,a,b) for(register int i=(b);i>=(a);i--)
#define E(i,u) for(register int i=head[u];i;i=nex[i])
using namespace std;
inline LL rd() {
LL x=0,fla=1; char c=' ';
while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
return x*fla;
}
inline void out(LL x){
int a[25],wei=0;
if(x<0) putchar('-'),x=-x;
for(;x;x/=10) a[++wei]=x%10;
if(wei==0){ puts("0"); return;}
for(int j=wei;j>=1;--j) putchar('0'+a[j]);
putchar('\n');
}
/*--splay_begin--*/
#define check(x) (x==nd[fa[x]][1])
#define clear(x) key[x]=cnt[x]=siz[x]=fa[x]=nd[x][1]=nd[x][0]=0
#define update(x) siz[x]=siz[nd[x][1]]+siz[nd[x][0]]+cnt[x]
const int N=100005;
int key[N],cnt[N],siz[N],fa[N],nd[N][2];
int size,root;
inline int newnode(int x) {key[++size]=x,cnt[size]=siz[size]=1;return size;}
inline void rotate(int x) { int old=fa[x],oldf=fa[old];
bool wh=check(x);
fa[nd[x][wh^1]]=old; nd[old][wh]=nd[x][wh^1];
nd[x][wh^1]=old; fa[old]=x; fa[x]=oldf;
if(oldf) nd[oldf][nd[oldf][1]==old]=x;
update(old);update(x);
}
inline void splay(int x) {
for(int f=fa[x];f;rotate(x),f=fa[x])
if(fa[f]) rotate(check(f)==check(x)?f:x);
root=x;
}
inline void insert(int x) {
if(!root) {root=newnode(x);return ;}
int now=root,f=0;
while(1) {
if(key[now]==x) {cnt[now]++;update(now);update(f);splay(now);return ;}
f=now,now=nd[now][x>key[now]];
if(!now) {now=newnode(x);fa[now]=f;nd[f][x>key[f]]=now;update(f);splay(now);return ;}
}
}
inline int rk(int x) { int now=root,ans=0;
while(1) {
if(x<key[now]) now=nd[now][0];
else {
ans+=siz[nd[now][0]];
if(key[now]==x) return splay(now),ans+1;
ans+=cnt[now]; now=nd[now][1];
}
}
}
inline int kth(int x) { int now=root;
while(1) {
if(nd[now][0]&&x<=siz[nd[now][0]]) now=nd[now][0];
else {
x-=siz[nd[now][0]]+cnt[now];
if(x<=0) return now;
now=nd[now][1];
}
}
}
inline int prenxt(int now) { int wh=check(now);
while(nd[now][wh^1]) now=nd[now][wh^1];
return now;
}
inline void del(int x) {
rk(x);
if(cnt[root]>1) {cnt[root]--;update(root);return ;}
if(!nd[root][0]&&!nd[root][1]) {clear(root);root=0;return ;}
if(!nd[root][0]) {int old=root;root=nd[root][1];fa[root]=0;clear(old);return ;}
if(!nd[root][1]) {int old=root;root=nd[root][0];fa[root]=0;clear(old);return ;}
int old=root;
splay(prenxt(nd[root][0]));
nd[root][1]=nd[old][1];
fa[nd[old][1]]=root;
clear(old);
update(root);
return ;
}
/*--splay_end--*/
int main() {
int n=rd();
for(int i=1;i<=n;i++) { int opt=rd(),x=rd();
switch(opt) {
case 1:insert(x);break;
case 2:del(x);break;
case 3:out(rk(x));break;
case 4:out(key[kth(x)]);break;
case 5:insert(x);out(key[prenxt(nd[root][0])]);del(x);break;
case 6:insert(x);out(key[prenxt(nd[root][1])]);del(x);break;
}
}
return 0;
}
[luogu3369] 普通平衡树(splay模板)的更多相关文章
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
- [luogu3369]普通平衡树(fhq-treap模板)
解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...
- [luogu3369]普通平衡树(treap模板)
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- P3369 【模板】普通平衡树 (splay 模板)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- 文艺平衡树(splay模板)
题干:splay模板,要求维护区间反转. splay是一种码量小于treap,但支持排名,前驱后继等treap可求的东西,也支持区间反转的平衡树. 但是有两个坏处: 1.splay常数远远大于trea ...
随机推荐
- 使用shell脚本定时执行备份mysql数据库
使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...
- 配置sudo命令行为审计
1.检查是否安装 rpm -aq sudo rsyslog #检验是否安装此软件 ***如果没有需执行(yum install sudo rsyslog -y)安装*** 2.配置审计 echo &q ...
- seliux(类似防火墙,限制root用户)
注:如果在开发是不设置关闭,可能会出现很多不在预期内的效果 路径:/etc/selinux/config *修改(修改时拷贝对照) (拷贝):cp /etc/selinux/config /etc/s ...
- 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第五篇【AppiumLibrary校验函数介绍】
http://blog.csdn.net/deadgrape/article/details/50619050 以上连作者先跪一下方便面,在上一篇中,作者遗漏了两个常用的函数: 1.长按 Long P ...
- oracle定时器执行一遍就不执行或本就不执行
转:http://blog.csdn.net/qq_23311211/article/details/76283689 以sqlplus/ assysdba进入sql命令模式,使用sql:select ...
- CF49A Sleuth
CF49A Sleuth 题目描述 Vasya plays the sleuth with his friends. The rules of the game are as follows: tho ...
- maven下载的jar包可以查看源码
1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...
- [SQL]存储过程建表
create PROC [dbo].CreateUserTable ( @name NVARCHAR(60) ) AS DECLARE @a NVARCHAR(max) SET @a='create ...
- [HTML5] Text Alternatives
Most of times, we need 'alt' to the images, so it can tell the screen reader what is this image abou ...