【Splay】bzoj3224 Tyvj 1728 普通平衡树
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- #define maxn 1000000
- #define INF 2147483647
- int n,fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn];
- void Maintain(int x)
- {
- siz[x]=siz[c[x][0]]+siz[c[x][1]]+cnt[x];
- }
- void NewNode(int &x,int Fa,int key)
- {
- x=++tot;
- fa[x]=Fa;
- c[x][0]=c[x][1]=0;
- val[x]=key;
- siz[x]=cnt[x]=1;
- }
- void Rotate(int x,bool flag)
- {
- int y=fa[x];
- c[y][!flag]=c[x][flag];
- fa[c[x][flag]]=y;
- if(fa[y]){
- c[fa[y]][c[fa[y]][1]==y]=x;
- }
- fa[x]=fa[y];
- c[x][flag]=y;
- fa[y]=x;
- Maintain(y);
- Maintain(x);
- }
- void Splay(int x,int goal)
- {
- if(!x){
- return;
- }
- int y;
- while((y=fa[x])!=goal){
- if(fa[y]==goal){
- Rotate(x,c[y][0]==x);
- }
- else{
- if((c[y][0]==x)==(c[fa[y]][0]==y)){
- Rotate(y,c[fa[y]][0]==y);
- }
- else{
- Rotate(x,c[y][0]==x);
- y=fa[x];
- }
- Rotate(x,c[y][0]==x);
- }
- }
- Maintain(x);
- if(!goal){
- root=x;
- }
- }
- int Find(int key,int x=root)
- {
- while(c[x][val[x]<key]){
- if(val[x]==key){
- return x;
- }
- x=c[x][val[x]<key];
- }
- return x;
- }
- void Insert(int key)
- {
- if(!root){
- NewNode(root,0,key);
- return;
- }
- int x=Find(key);
- if(val[x]==key){
- ++cnt[x];
- Splay(x,0);
- return;
- }
- NewNode(c[x][val[x]<key],x,key);
- Splay(c[x][val[x]<key],0);
- }
- int Findmax(int x=root)
- {
- while(c[x][1]){
- x=c[x][1];
- }
- return x;
- }
- int Findmin(int x=root)
- {
- while(c[x][0]){
- x=c[x][0];
- }
- return x;
- }
- void Delete(int key)
- {
- int x=Find(key);
- Splay(x,0);
- if(val[x]==key){
- --cnt[x];
- if(!cnt[x]){
- if(!c[x][0]&&!c[x][1]){
- root=0;
- }
- else if(!c[x][0]||!c[x][1]){
- fa[c[x][c[x][0]==0]]=0;
- root=c[x][c[x][0]==0];
- }
- else{
- int y=Findmax(c[x][0]);
- Splay(y,root);
- c[y][1]=c[root][1];
- fa[c[root][1]]=y;
- root=y;
- fa[root]=0;
- Maintain(root);
- }
- }
- else{
- Maintain(x);
- }
- }
- }
- int Rank(int key)
- {
- int x=Find(key);
- Splay(x,0);
- return siz[c[x][0]]+1;
- }
- int Kth(int K,int x=root)
- {
- while(1){
- int Siz0=siz[c[x][0]];
- if(K<=Siz0){
- x=c[x][0];
- }
- else if(K<=Siz0+cnt[x]){
- return val[x];
- }
- else{
- K-=(Siz0+cnt[x]);
- x=c[x][1];
- }
- }
- }
- int GetPre(int key)
- {
- int x=Find(key);
- if(val[x]==key){
- Splay(x,0);
- return val[Findmax(c[x][0])];
- }
- while(val[x]>key&&fa[x]){
- x=fa[x];
- }
- return val[x];
- }
- int GetNex(int key)
- {
- int x=Find(key);
- if(val[x]==key){
- Splay(x,0);
- return val[Findmin(c[x][1])];
- }
- while(val[x]<key&&fa[x]){
- x=fa[x];
- }
- return val[x];
- }
- int main(){
- int op, x;
- scanf("%d",&n);
- for(int i=1;i<=n;i++){
- scanf("%d%d",&op,&x);
- if(op==1){
- Insert(x);
- }
- else if(op==2){
- Delete(x);
- }
- else if(op==3){
- printf("%d\n",Rank(x));
- }
- else if(op==4){
- printf("%d\n",Kth(x));
- }
- else if(op==5){
- printf("%d\n",GetPre(x));
- }
- else{
- printf("%d\n",GetNex(x));
- }
- }
- return 0;
- }
【Splay】bzoj3224 Tyvj 1728 普通平衡树的更多相关文章
- bzoj3224: Tyvj 1728 普通平衡树(平衡树)
bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- bzoj3224: Tyvj 1728 普通平衡树(splay)
3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: #include<cstdio> #include<cstring> ...
- bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5354 Solved: 2196[Submit][Sta ...
- 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会
平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...
- bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)
(其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- 【权值分块】bzoj3224 Tyvj 1728 普通平衡树
权值分块和权值线段树的思想一致,离散化之后可以代替平衡树的部分功能. 部分操作的时间复杂度: 插入 删除 全局排名 全局K大 前驱 后继 全局最值 按值域删除元素 O(1) O(1) O(sqrt(n ...
- 【权值线段树】bzoj3224 Tyvj 1728 普通平衡树
一个板子. #include<cstdio> #include<algorithm> using namespace std; #define N 100001 struct ...
随机推荐
- C++学习之路(三):volatile关键字
volatile是c++中的一个关键字.用volatile修饰的变量,具有三个性质:易变性 (一)易变性: 由于编译器对代码执行的优化,两条赋值语句,下一条语句可能会直接从上一条语句使用的寄存器中取得 ...
- map,set的底层实现:红黑树[多图,手机慎入]
最近天下有一种颇不太平的感觉,各地的乱刀砍人,到处是贪官服法.京东准备上市了,阿里最近也提交申请了,猎豹也逆袭了,据说猎豹移动在国际市场上表现甚是抢眼.只有屌丝还在写着代码.花开花又谢,花谢花又开,为 ...
- fbx sdk
autodesk fbx review autodesk fbx review http://www.greenxf.com/soft/169025.html autodesk fbx review( ...
- selenium.webdriver.common.keys 模块中常用的变量
表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...
- 【bzoj4562】HAOI2016食物链
记忆化搜索水过去了…… QwQ #include<bits/stdc++.h> #define N 400010 typedef long long ll; using namespace ...
- 升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode” Xcode 插件安装
升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode” Xcode 插件安装 A: 升级10.11后Xcode 左上角模拟器选择菜单不在了 ...
- apache加入chkconfig
#First Step: cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd #Second Step: vim /etc/init.d/htt ...
- python安装基础
. python安装 //先查看是否存在python的包,如果没有,那可以用yum或去python的官网安装 [root@localhost ~]# rpm -qa|grep python pytho ...
- yum安装的Apache的各种配置文件的位置
//配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...
- Context-Aware Network Embedding for Relation Modeling
Context-Aware Network Embedding for Relation Modeling 论文:http://www.aclweb.org/anthology/P17-1158 创新 ...