bzoj3224: Tyvj 1728 普通平衡树(splay)
3224: Tyvj 1728 普通平衡树
题目:传送门
题解:
啦啦啦啦又来敲个模版水经验啦~
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
int d,c,n,f,son[];
}tr[];int len,root;
void add(int d,int f)
{
len++;tr[len].d=d;tr[len].n=tr[len].c=;
tr[len].son[]=tr[len].son[]=;tr[len].f=f;
if(d<tr[f].d)tr[f].son[]=len;
else tr[f].son[]=len;
}
void update(int x)
{
int lc=tr[x].son[],rc=tr[x].son[];
tr[x].c=tr[lc].c+tr[rc].c+tr[x].n;
}
int findip(int d)
{
int x=root;
while(tr[x].d!=d)
{
if(d<tr[x].d)
{
if(tr[x].son[]==)break;
else x=tr[x].son[];
}
else
{
if(tr[x].son[]==)break;
else x=tr[x].son[];
}
}
return x;
}
void rotate(int x,int w)
{
int f=tr[x].f,ff=tr[f].f;
int r,R;
r=tr[x].son[w],R=f;
tr[R].son[-w]=r;
if(r!=)tr[r].f=R; r=x,R=ff;
if(tr[R].son[]==f)tr[R].son[]=r;
else tr[R].son[]=r;
tr[r].f=R; r=f,R=x;
tr[R].son[w]=r;
tr[r].f=R; update(f);update(x);
}
void splay(int x,int rt)
{
while(tr[x].f!=rt)
{
int f=tr[x].f,ff=tr[f].f;
if(ff==rt)
{
if(tr[f].son[]==x)rotate(x,);
else rotate(x,);
}
else
{
if(tr[ff].son[]==f && tr[f].son[]==x)rotate(f,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(f,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(x,),rotate(x,);
else if(tr[ff].son[]==f && tr[f].son[]==x)rotate(x,),rotate(x,);
}
}
if(rt==)root=x;
}
void ins(int d)
{
if(root==)
{
add(d,);root=len;
return ;
}
int x=findip(d);
if(tr[x].d==d)
{
tr[x].n++;
update(x);
splay(x,);
}
else
{
add(d,x);
update(x);
splay(len,);
}
}
void del(int d)
{
int x=findip(d);if(tr[x].d!=d)return ;
splay(x,);
if(tr[x].n>){tr[x].n--;update(x);return ;}
if(tr[x].son[]== && tr[x].son[]==){root=;len=;}
else if(tr[x].son[]!= && tr[x].son[]==){root=tr[x].son[];tr[root].f=;}
else if(tr[x].son[]== && tr[x].son[]!=){root=tr[x].son[];tr[root].f=;}
else
{
int p=tr[x].son[];
while(tr[p].son[]!=)p=tr[p].son[];
splay(p,x); int r=tr[x].son[],R=p;
tr[R].son[]=r;
tr[r].f=R; root=p;tr[root].f=;
update(root);
}
}
void findpaiming(int d)
{
int x=findip(d);splay(x,);int lc=tr[x].son[];
printf("%d\n",tr[lc].c+);
}
void findshuzi(int k)
{
if(tr[root].c<k){printf("-1\n");return ;}
int x=root;
while()
{
int lc=tr[x].son[],rc=tr[x].son[];
if(k<=tr[lc].c)x=lc;
else if(k>tr[lc].c+tr[x].n)k-=tr[lc].c+tr[x].n,x=rc;
else break;
}
printf("%d\n",tr[x].d);
}
void findqianqu(int d)
{
int x=findip(d);splay(x,);
if(d<=tr[x].d && tr[x].son[]!=)
{
x=tr[x].son[];
while(tr[x].son[]!=)x=tr[x].son[];
}
if(d<=tr[x].d)x=;
printf("%d\n",tr[x].d);
}
void findhouji(int d)
{
int x=findip(d);splay(x,);
if(d>=tr[x].d && tr[x].son[]!=)
{
x=tr[x].son[];
while(tr[x].son[]!=)x=tr[x].son[];
}
if(d>=tr[x].d)x=;
printf("%d\n",tr[x].d);
}
/*
void dell(int l,int r)
{
int lc=findqianqu(tr[l].d),rc=findhouji(tr[r].d);
spaly(lc,0);spaly(rc,lc);
tr[rc].son[0]=0;len-=tr[tr[rc].lc].c;
update(rc);update(lc);
}
*/
int main()
{
int n;scanf("%d",&n);root=;len=;
for(int i=;i<=n;i++)
{
int opt,d;scanf("%d%d",&opt,&d);
if(opt==)ins(d);
else if(opt==)del(d);
else if(opt==)findpaiming(d);
else if(opt==)findshuzi(d);
else if(opt==)findqianqu(d);
else if(opt==)findhouji(d);
}
return ;
}
bzoj3224: Tyvj 1728 普通平衡树(splay)的更多相关文章
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- 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 普通平衡树(名次树+处理相同)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5354 Solved: 2196[Submit][Sta ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3224 题意概括 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. ...
- 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会
平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...
- bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)
(其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...
- 【Splay】bzoj3224 Tyvj 1728 普通平衡树
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> us ...
随机推荐
- js---通过代码学习
1:本例演示 getElementsByTagName 方法. 2:本例演示 getElementsByTagName 方法 3:注意:
- 【python】random
1.random 和其他语言一样,返回大于等于0.小于1的浮点数 2.uniform(a,b) 返回大于等于a.小于等于b的浮点数 3.randint(a,b) 这个很好理解,返回一个介于a和b之间的 ...
- Windows下使用Caffe-Resnet
参考文章: 编译历程参考:CNN:Windows下编译使用Caffe和Caffe2 caffe的VS版本源代码直接保留了sample里面的shell命令,当然这些shell命令在Windows平台下是 ...
- <aop:aspectj-autoproxy />
通过配置织入@Aspectj切面 虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作. 通过aop命名空间的<aop:aspectj-a ...
- JSP状态管理_1_Cookie
http协议的无状态性:当浏览器发送请求飞服务器时,服务器相应客户端请求:但当同一个浏览器再次发送请求给浏览器时,服务器并不知道它就是刚才那个客户端. 保存用户状态的两大机制:Session,Cook ...
- jq 跨域请求
//使用getJSON <script type="text/javascript"> $(function () { $("#btn2").cli ...
- Python字符串格式化--formate()的应用
1.简单运用字符串类型格式化采用format()方法,基本使用格式是:转自 <模板字符串>.format(<逗号分隔的参数>) 调用format()方法后会返回一个新的字符串, ...
- revit二次开发之族的类型参数与实例参数的转换
1背景小伙伴在做revit二次开发的时候,可能需要在族环境中将族的类型参数与实例参数相互转换. 2思路1.使用族管理器FamilyManager,参见注释12.首先获取需要转换的参数(单个与批量),参 ...
- Oracle经验积累
Oracle 常用Funtion ----行转列 开始---- select u_id, wmsys.wm_concat(goods) goods_sum from shopping group by ...
- 深度学习应用在推荐系统的论文-----A Novel Deep Learning-Based Collaborative Filtering Model for Recommendation System
1.题目:一种新的基于深度学习的协同过滤推荐系统 2.摘要: 以协同过滤(CF)为基础的模型主要获取用户和项目的交互或者相关性.然而,现有的基于CF的方法只能掌握单一类型的关系,如RBM,它只能获取用 ...