bzoj 3224

时间:2023-03-08 20:59:43

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

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]

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 ;
}