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,终于做对 ...
随机推荐
- c# 删除文件,清理删除文件
c# 删除程序占用的文件,清理删除文件,彻底删除文件,解除文件占用 文件打开时,以共享读写模式打开 FileStream inputStream = new FileStream(name, File ...
- WCF 之部署(VS2010)
一. 环境vs2010,WCF应用程序,server 2008 第一步:WCF项目右键点击项目,选择生成部署包,如下图: 第二步:WCF项目上右键,选择:在windows资源管理器中打开文件夹,如下图 ...
- [linux] C语言Linux系统编程-捕获进程信号
typedef void( *sighandler_t)(int); 1.用typedef给类型起一个别名. 2.为函数指针类型定义别名, 3.函数指针(指向函数的指针) sighandler_t s ...
- 十二、异步工具Timer
一.简介 JDK提供一种异步线程工具Timer类,你可以利用这个类做延迟任务.周期性任务等. JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/ ...
- Java中返回值的详解
package com.company; //java中main()函数中调用其他方法的两种方式//1.实例化对象 public class returnDemo { public static vo ...
- 理解webpack4.splitChunks之其余要点
splitChunks除了之前文章提到的规则外,还有一些要点或是叫疑惑因为没有找到官方文档的明确说明,所以是通过我自己测试总结出来的,只代表我自己的测试结果,不一定正确. splitChunks.ca ...
- jsp页面乱码
- ThreeJs 选中物体事件
选中物体变红色demo: https://threejs.org/examples/#webgl_raycast_sprite <!DOCTYPE html> <html lang= ...
- css3怎么分清伪类和伪元素
伪类用于向某些选择器添加特殊的效果. 伪元素用于将特殊的效果添加到某些选择器. 伪类有::first-child ,:link:,vistited,:hover,:active,:focus,:lan ...
- Android 弹出框Dialog并缩放图片
java代码 Activity: // 调用dialog,参数:1:自身的activity,2:Bitmap bm读取好的图片 MyDialog dialog = new MyDialog(MyAct ...