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 ...
随机推荐
- Vue 区别
computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.
- js动态追加的元素如何触发事件
一般通过js或者jQuery动态添加的元素标签,通过该元素标签.class.id触发事件,是无效的.如下所示: <body> <input type="text" ...
- 5.29 @Value{name}无效时怎么办Could not resolve placeholder ‘name22’ in value “${name22}” 错误解决
springboot Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘name22’ in ...
- 易企CMS主要模板文件介绍
article.tpl 文章内容页模板 catalog.tpl 文章,产品目录页模板 category.tpl 分类页模板 comment.tpl 留言页模板 footer.tpl 页尾模板 head ...
- 连接(JOIN)运算
内连接--INNER JOIN 此处用商品表(product)和商店商品表(ShopProduct)测试,外键:product_id select sp.shop_id, sp.shop_name, ...
- jquery相关常用的工具函数
1.弹出提示框: function prompt(msg){ $("<div>" + msg + "</div>").css({ &qu ...
- day37-2元类,单例模式
目录 元类 造类的第一种形式 class做了什么事 控制元类产生的类 控制元类产生的对象 实例化类 加上元类后类的属性查找顺序 元类控制模版 单例模式 1. 使用类方法的特性 2. 使用装饰器 3. ...
- DATEADD()
定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的 ...
- HTTP 状态码 301 和 302 详解及区别——辛酸的探索之路
转自:http://blog.csdn.net/grandpang/article/details/47448395 一直对http状态码301和302的理解比较模糊,在遇到实际的问题和翻阅各种资料了 ...
- php第十一节课
增删改查 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...