【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 ...
随机推荐
- HDU 1234 开门人和关门人 (模拟)
题目链接 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input ...
- [bzoj1002]轮状病毒-矩阵树定理
Brief Description 求外圈有\(n\)个点的, 形态如图所示的无向图的生成树个数. Algorithm Design \[f(n) = (3*f(n-1)-f(n-2)+2)\] Co ...
- C# IEqualityComparer 使用方法 Linq Distinct使用方法
创建 IEqualityComparer的接口类必须实现Equals和GetHashCode方法 public class TipComparer : IEqualityComparer<Tip ...
- textarea输入框实时统计输入字符数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python switch-case语句的实现 -- 字典模拟实现
static void print_asru_status(int status, char *label) { char *msg = NULL; switch (status) { : msg = ...
- idea 调试远程tomcat
# ----- Execute The Requested Command ----------------------------------------- JAVA_OPTS="-age ...
- php伪协议
文件包含函数:include.require.include_once.require_once.highlight_file .show_source .readfile .file_get_con ...
- aptitude约等于apt-get的工具
如题,与之不同的是其会将依赖的程序也给删除. https://baike.baidu.com/item/aptitude/6849487?fr=aladdin 以下是一些常用 aptitude命令,仅 ...
- C基础 常用设计模式粗解
引言 面向对象, 设计模式是现代软件开发基石. C的面向过程已经很简洁, 但不代表C就没有面向对象.(libuv框架中C面向对象用的很多) 因为思想是互通的.全当熟悉一下那些常用的设计模式.先假定有一 ...
- JSP(3) - 9个JSP内置对象 - 小易Java笔记
1.9个JSP内置对象 内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse config Servle ...