BZOJ3224:普通平衡树(Splay)
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
Solution
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN (100000+10)
using namespace std;
int Father[MAXN];
int Son[MAXN][];
int Key[MAXN];//结点代表的数字
int Cnt[MAXN];//结点代表数字出现次数
int Size[MAXN];//子树大小(含自身)
int Root,sz,n,opt,x; void Clear(int x){Father[x]=Son[x][]=Son[x][]=Key[x]=Cnt[x]=Size[x]=;}
void New(int x){Size[++sz]=;Key[sz]=x;Cnt[sz]=;}
void Update(int x){Size[x]=Cnt[x]+Size[Son[x][]]+Size[Son[x][]];}
int Get(int x){return x==Son[Father[x]][];}
int Pre(){int now=Son[Root][]; while (Son[now][]) now=Son[now][];return now;}
int Next(){ int now=Son[Root][];while (Son[now][]) now=Son[now][];return now;} void Rotate(int x)
{
int wh=Get(x);
int fa=Father[x],fafa=Father[fa];
Son[fa][wh]=Son[x][wh^];
Father[fa]=x;
if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
Son[x][wh^]=fa;
Father[x]=fafa;
if (fafa) Son[fafa][Son[fafa][]==fa]=x;
Update(fa);
Update(x);
} void Splay(int x)
{
for (int fa; fa=Father[x]; Rotate(x))
if (Father[fa])
Rotate(Get(fa)==Get(x)?fa:x);
Root=x;
} int Find(int x)
{
int ans=,now=Root;
while ()
if (x<Key[now])
now=Son[now][];
else
{
ans+=Size[Son[now][]];
if (x==Key[now])
{
Splay(now);
return ans+;
}
ans+=Cnt[now];
now=Son[now][];
}
} int Findx(int x)
{
int now=Root;
while ()
if (x<=Size[Son[now][]])
now=Son[now][];
else
{
x-=Size[Son[now][]];
if (x<=Cnt[now])
{
Splay(now);
return Key[now];
}
x-=Cnt[now];
now=Son[now][];
}
} void Insert(int x)
{
if (Root==)
{
New(x);
Root=sz;
return;
}
int now=Root,fa=;
while ()
{
if (x==Key[now])
{
Cnt[now]++;
Update(now);
Splay(now);
return;
}
fa=now,now=Son[now][Key[now]<x];
if (now==)
{
New(x);
Father[sz]=fa;
Son[fa][x>Key[fa]]=sz;
// Update(fa);
Splay(sz);
return;
}
}
} void Delete(int x)
{
Find(x);
if (Cnt[Root]>)
{
--Cnt[Root];
Update(Root);
return;
}
if (!Son[Root][] && !Son[Root][])
{
Clear(Root);
Root=;
return;
}
if (!Son[Root][])
{
Root=Son[Root][];
Clear(Father[Root]);
Father[Root]=;
return;
}
if (!Son[Root][])
{
Root=Son[Root][];
Clear(Father[Root]);
Father[Root]=;
return;
}
int old=Root;
Splay(Pre());
Son[Root][]=Son[old][];
Father[Son[old][]]=Root;
Clear(old);
Update(Root);
} int main()
{
scanf("%d",&n);
for (int i=; i<=n; ++i)
{
scanf("%d%d",&opt,&x);
if (opt==) Insert(x);
if (opt==) Delete(x);
if (opt==) printf("%d\n",Find(x));
if (opt==) printf("%d\n",Findx(x));
if (opt==) Insert(x),printf("%d\n",Key[Pre()]),Delete(x);
if (opt==) Insert(x),printf("%d\n",Key[Next()]),Delete(x);
}
}
BZOJ3224:普通平衡树(Splay)的更多相关文章
- bzoj3224 普通平衡树(splay 模板)
		
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11427 Solved: 4878[Submit][St ...
 - [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
		
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
 - bzoj3224  普通平衡树              splay模板
		
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
 - 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
		
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
 - BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
		
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
 - 【转】 史上最详尽的平衡树(splay)讲解与模板(非指针版spaly)
		
ORZ原创Clove学姐: 变量声明:f[i]表示i的父结点,ch[i][0]表示i的左儿子,ch[i][1]表示i的右儿子,key[i]表示i的关键字(即结点i代表的那个数字),cnt[i]表示i结 ...
 - hiho  #1329 : 平衡树·Splay
		
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
 - Hihocoder 1329 平衡树·Splay(平衡树)
		
Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...
 - 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
		
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
 
随机推荐
- jenkins学习之centos6.9下安装
			
以下为centos6.9下测试安装: docker下安装jenkins: 更新yum源: yum -y update 安装docker: yum -y install docker-io 启动dock ...
 - Java基础教程(6)--数组
			
1.基本概念 数组中的每一项称为元素,每个元素都通过数字索引(也可以称为下标)访问,编号从0开始.例如,第4个元素的索引为3.下面的程序创建了一个int类型的数组,把一些值放入数组中并将每个值打印 ...
 - 4、构造方法、this、super
			
构造方法 构造方法引入 * A:构造方法的引入 在开发中经常需要在创建对象的同时明确对象的属性值,比如员工入职公司就要明确他的姓名.年龄等属性信息. 那么,创建对象就要明确属性值,那怎么解决呢?也就是 ...
 - ssm项目快速搭建(注解)-依赖
			
父层jar包版本控制,管理配置 <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</ ...
 - String 简单使用
			
package com.direct.str; public class TestObject { /** * @param args */ /* * 1.object类是根类,里面定义的==和equ ...
 - vscode 实用的插件
			
REST-Client api接口测试插件 在项目中新建以.http后缀名的文件即可. 右键使用 可以生产多种语言的请求代码块.可以说是非常舒服了. 使用代码块功能生成了node环境的http请求. ...
 - 解决API中无法使用session问题
			
处理API无法使用session的方法,贴图: 1调用如下图 2.需要在Global.asax文件中配置一些东西 protected void Application_PostAuthorizeReq ...
 - C++ Knowledge series 1
			
Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...
 - jquery-animate()动画
			
一.animate()语法 $(“选择器”).animate({CSS样式},时间,运动方式,回调函数); 参数说明: 参数1:CSS属性名,属性值,JSON格式{"属性名":&q ...
 - JS的排序算法
			
排序是最基本的算法(本文排序为升序Ascending),常见的有以下几种: 1.冒泡排序 Bubble Sort 2.选择排序 Selection Sort 3.插入排序 Insertion Sort ...