bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 11427 Solved: 4878
[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
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
Source
#include <cstdio>
#define Maxn 1000000
using namespace std;
int f[Maxn];//father
int ch[Maxn][];//child ; 0 for left ; 1 for right
int key[Maxn];//key
int cnt[Maxn];//value
int siz[Maxn];//size of subtree
int sz,root;//size of tree and root
//clear the ndoe void clear(int x)
{
ch[x][]=ch[x][]=f[x]=cnt[x]=key[x]=siz[x]=;
} //rightson return 1;left son return 0
int getson(int x)
{
return ch[f[x]][]==x;
} //update the size
void update(int x)
{
siz[x]=cnt[x];
if (ch[x][]) siz[x]+=siz[ch[x][]];
if (ch[x][]) siz[x]+=siz[ch[x][]];
} //retation
int rotate(int x)
{
int fa=f[x],fafa=f[fa],k=getson(x);
ch[fa][k]=ch[x][k^];f[ch[fa][k]]=fa;
ch[x][k^]=fa;f[fa]=x;
f[x]=fafa;
if (fafa)
ch[fafa][ch[fafa][]==fa]=x;
update(fa);update(x);
} //rotate until x is the root
void splay(int x)
{
for (int fa;fa=f[x];rotate(x))
if (f[fa])
rotate(getson(x)==getson(fa) ? fa : x);
root=x;
} int pre()
{
int now=ch[root][];
while(ch[now][])
now=ch[now][];
return now;
} int nex()
{
int now=ch[root][];
while(ch[now][])
now=ch[now][];
return now;
} //find x's pos
int findpos(int v)
{
int now=root,ans=;
while()
{
if (v<key[now])
now=ch[now][];
else
{
ans+=ch[now][]?siz[ch[now][]]:;
if (v==key[now])
{
splay(now);
return ans+;
}
ans+=cnt[now];
now=ch[now][];
}
}
} //find pos's x
int findx(int x)
{
int now=root;
while()
{
if (ch[now][] && x<=siz[ch[now][]])
now=ch[now][];
else
{
int temp=(ch[now][]?siz[ch[now][]]:)+cnt[now];
if (x<=temp)
return key[now];
x-=temp;
now=ch[now][];
}
}
} //ceate a new splay node
void create(int v)
{
sz++;
ch[sz][]=ch[sz][]=f[sz]=;
key[sz]=v;
cnt[sz]=;
siz[sz]=;
//root=sz;
} //insert a node
void insert(int v)
{
if (!root)
create(v),root=sz;
else
{
int now=root,fa=;
while()
{
if (key[now]==v)
{
cnt[now]++;
update(now);update(fa);
splay(now);
break;
}
fa=now;
now=ch[fa][v>key[fa]];
if (!now)
{
create(v);
f[sz]=fa;
ch[fa][v>key[fa]]=sz;
update(fa);
splay(sz);
break;
}
}
}
} void del(int x)
{
int t=findpos(x);
if (cnt[root]>)
{
cnt[root]--;
update(root);
return;
}
//none
if (!ch[root][] && !ch[root][])
{
clear(root);
root=;
return;
}
//one
if (!ch[root][])
{
int temp=root;
root=ch[root][];
f[root]=;
clear(temp);
return;
}
else
if (!ch[root][])
{
int temp=root;
root=ch[root][];
f[root]=;
clear(temp);
return;
}
//two
int pre1=pre(),temp=root;
splay(pre1);
f[ch[temp][]]=root;
ch[root][]=ch[temp][];
clear(temp);
update(root);
} int main()
{
int n,opt,x;
scanf("%d",&n);
for (int i=;i<=n;++i)
{
scanf("%d%d",&opt,&x);
switch(opt)
{
case : insert(x); break;
case : del(x); break;
case : printf("%d\n",findpos(x)); break;
case : printf("%d\n",findx(x)); break;
case : insert(x); printf("%d\n",key[pre()]); del(x); break;
case : insert(x); printf("%d\n",key[nex()]); del(x); break;
}
}
}
#include<iostream>
#include<cstdio>
#include<cstring> #define N 1000000 using namespace std;
int f[N],ch[N][],key[N],cnt[N],siz[N],sz,root; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} inline void update(int x)
{
siz[x]=cnt[x];
if(ch[x][]) siz[x]+=siz[ch[x][]];
if(ch[x][]) siz[x]+=siz[ch[x][]];
} int pre()
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];return now;
} int nex()
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];return now;
} inline int getson(int x)
{
return ch[f[x]][]==x;
} inline void rorate(int x)
{
int fa=f[x],ffa=f[fa],k=getson(x);
ch[fa][k]=ch[x][k^];f[ch[fa][k]]=fa;
ch[x][k^]=fa;f[fa]=x;f[x]=ffa;
if(ffa)ch[ffa][ch[ffa][]==fa]=x;
update(fa);update(x);
} void splay(int x)
{
for(int fa;fa=f[x];rorate(x))
if(f[fa]) rorate(getson(x)==getson(fa)?fa:x);
root=x;
} int findpos(int x)
{
int now=root,ans=;
while()
{
if(x<key[now]) now=ch[now][];
else
{
ans+=ch[now][]?siz[ch[now][]]:;
if(x==key[now])
{
splay(now);return ans+;
}
ans+=cnt[now];now=ch[now][];
}
}
} int findx(int x)
{
int now=root;
while()
{
if(ch[now][]&&x<=siz[ch[now][]]) now=ch[now][];
else
{
int tmp=(ch[now][]?siz[ch[now][]]:)+cnt[now];
if(x<=tmp) return key[now];
x-=tmp;now=ch[now][];
}
}
} void clear(int x)
{
ch[x][]=ch[x][]=cnt[x]=siz[x]=key[x]=f[x]=;
} void creat(int x)
{
sz=sz+;key[sz]=x;cnt[sz]=siz[sz]=;
ch[sz][]=ch[sz][]=f[sz]=;
} void insert(int x)
{
if(!root) creat(x),root=sz;
else
{
int now=root,fa=;
while()
{
if(key[now]==x)
{
cnt[now]++;siz[now]++;splay(now);
break;
}
fa=now;now=ch[fa][x>key[fa]];
if(!now)
{
creat(x);f[sz]=fa;ch[fa][x>key[fa]]=sz;
splay(sz);break;
}
}
}
} void del(int x)
{
int t=findpos(x);
if(cnt[root]>)
{
cnt[root]--;siz[root]--;return;
}
if(!ch[root][]&&!ch[root][])
{
clear(root);root=;return;
}
if(!ch[root][])
{
int tmp=root;root=ch[root][];f[root]=;
clear(tmp);return;
}
if(!ch[root][])
{
int tmp=root;root=ch[root][];f[root]=;
clear(tmp);return;
}
int pre1=pre(),tmp=root;splay(pre1);
ch[root][]=ch[tmp][];f[ch[tmp][]]=root;
clear(tmp);update(root);
} int main()
{
int n,opt,x;
n=read();
for(int i=;i<=n;i++)
{
opt=read();x=read();
switch(opt)
{
case :insert(x);break;
case :del(x);break;
case :printf("%d\n",findpos(x));break;
case :printf("%d\n",findx(x));break;
case :insert(x);printf("%d\n",key[pre()]);del(x);break;
case :insert(x);printf("%d\n",key[nex()]);del(x);break;
}
}
}
我的马蜂
bzoj3224 普通平衡树(splay 模板)的更多相关文章
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- BZOJ3224:普通平衡树(Splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...
- P3369 【模板】普通平衡树 (splay 模板)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3224 题意概括 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
随机推荐
- 字符串系列——KMP模板整理
KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...
- Socket中BufferedReader.readLine()的阻塞特性导致的数据无法多次发送的问题
https://blog.csdn.net/shenpibaipao/article/details/70236657
- 每日命令:(6)rmdir
今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...
- Linux 服务器 U盘安装(避免U盘启动)以及拔除U盘后无法引导系统
一.U盘制作 首先下载两个文件: · rhel-server-6.3-i386-boot.iso 启动镜像 · rhel-server-6.3-i386-dvd. ...
- Linux命令学习(3): zcat 直接查看压缩文件
版权声明:本文为博主原创文章,未经允许不得转载. zcat 用途:在不解压文件的情况下,直接将文件内容输出到标准输出.(原压缩文件不做任何更改) 格式:zcat [-n] [-V] [FILE] 参数 ...
- 个人 NABCD
失物招领APP个人NABCD Need: 在朋友圈中,QQZone中我们长仓可以看到失物招领这方面的信息,又没有学校中专门使用的失物招领平台,所以根据这个信息,决定开发一款解决这方面问题的APP来满足 ...
- 版本优化-test
版本优化 标签(空格分隔): 测试 需求经手人太多,直接提bug,开发不乐意,跟Leader确认不靠谱,跟PM确认,不熟悉流程,跟第三方PM确认靠谱了,结果被开发三言两语,变成了不改bug 而改需求 ...
- python爬虫27 | 当Python遇到MongoDB的时候,存储av女优的数据变得如此顺滑爽~
上次 我们知道了怎么操作 MySQL 数据库 python爬虫26 | 把数据爬取下来之后就存储到你的MySQL数据库. MySQL 有些年头了 开源又成熟又牛逼 所以现在很多企业都在使用 MySQL ...
- 【18】AngularJS 包含
AngularJS 包含 在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中包含 HTML 文件 在 HTML 中,目前还不支持包含 HTML 文件的功能. 服 ...
- 【14】AngularJS 表单
AngularJS 表单 AngularJS 表单是输入控件的集合. HTML 控件 以下 HTML input 元素被称为 HTML 控件: input 元素 select 元素 button 元素 ...