题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

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模板)的更多相关文章

  1. [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)

    解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...

  2. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

  3. bzoj3224 普通平衡树 splay模板

    题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...

  4. bzoj3224 普通平衡树(splay 模板)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 11427  Solved: 4878[Submit][St ...

  5. [luogu3369]普通平衡树(fhq-treap模板)

    解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  6. [luogu3369]普通平衡树(treap模板)

    解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  7. [bzoj3224]Tyvj 1728 普通平衡树——splay模板

    题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...

  8. P3369 【模板】普通平衡树 (splay 模板)

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...

  9. 【BZOJ3224】Tyvj 1728 普通平衡树 Splay

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  10. 文艺平衡树(splay模板)

    题干:splay模板,要求维护区间反转. splay是一种码量小于treap,但支持排名,前驱后继等treap可求的东西,也支持区间反转的平衡树. 但是有两个坏处: 1.splay常数远远大于trea ...

随机推荐

  1. HTML常用标签简介及快速入门

    此HTML常用标签简介编写的目的,是给一个经常使用网页编辑器的一个朋友提供一个快速熟悉和入门HTML的途径. 现在分享出来,给其他有类似需求的朋友,此处只列出了编辑文章时最常用和遇到的标签,完整标签页 ...

  2. Django入门--url路由基本配置

    URL(Uniform Resoure Locater)统一资源定位符,是对可以从互联网上得到资源位置和访问方法的一种简洁形式,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含 ...

  3. 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)

    第一步:添加springboot的配置文件 首先我先贴出我的配置 添加依赖包 <!-- spring boot devtools 依赖包. --> <dependency> & ...

  4. HDU 3698 Let the light guide us

    Let the light guide us Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. ...

  5. svn查看工程版本库的url地址

    打开cmd,cd到工程目录,使用svn的命令:svn info 完.

  6. rabbitMQ学习笔记(二) 简单的发送与接收消息 HelloWorld

    首先要下载rabbitmq的javaClient库,然后加入到项目中,下载地址为:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1. ...

  7. Android网络编程(十)Retrofit2后篇[注解]

    G相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley用法全解析 An ...

  8. rails 修改数据库之后注意修改controller

    rails 修改数据库之后注意修改controller 在view中进行修改之后,注意修改controller中的内容: 这样才可以进行参数的传递:

  9. Java多线程演示样例(模拟通话,sleep,join,yield,wait,notify,Semaphore)

    主线程等待子线程的多种方法 synchronized浅析 sleep 是静态方法,Thread.sleep(xx)谁调用谁睡眠. join 是合并方法.当前线程调用其它线程xx.join()则等到xx ...

  10. unity3d 中动画的帧事件

    代码事件监听 using UnityEngine; using System.Collections; public class BoxEventScript : MonoBehaviour { vo ...