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

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

1.n的数据范围:n<=100000
2.每个数的数据范围:[-1e7,1e7]

Source

平衡树

Splay模板题,膜了YveH爷的模板,不过代码好长,常数还很大。。。好歹是打粗来了

 #include <cstdio>
using namespace std;
struct SplayNode
{
SplayNode *fa,*ch[];
SplayNode();
int data,num,size;
int chr() {return this==fa->ch[];}
void updata() { size=ch[]->size+ch[]->size+num;}
}*null;
SplayNode::SplayNode() {fa=ch[]=ch[]=null; size=; num=;}
int n;
namespace Splay
{
SplayNode *Root;
void MakeTree()
{
null=new SplayNode;
*null=SplayNode();
Root=null;
}
void rotate(SplayNode *x)
{
SplayNode *r=x->fa;
if (r==null || x==null) return;
int t=x->chr();
r->ch[t]=x->ch[t^];
r->ch[t]->fa=r;
if (r->fa==null) Root=x;
else r->fa->ch[r->chr()]=x;
x->fa=r->fa;
x->ch[t^]=r;
r->fa=x;
r->updata();
x->updata();
}
void splay(SplayNode *x,SplayNode *y)
{
for (;x->fa!=y;rotate(x))
if (x->fa->fa!=y)
if (x->chr()==x->fa->chr()) rotate(x->fa);
else rotate(x);
}
void insert(int v)
{
SplayNode *r=Root;
if (Root==null)
{
Root=new SplayNode;
Root->data=v;
Root->updata();
return;
}
if (Root->data==v)
{
Root->num++;
Root->updata();
return;
}
while (r->ch[r->data<v]!=null)
{
r=r->ch[r->data<v];
if (r->data==v)
{
r->num++;
splay(r,null);
return;
}
}
r->ch[r->data<v]=new SplayNode;
r->ch[r->data<v]->data=v;
r->ch[r->data<v]->fa=r;
splay(r->ch[r->data<v],null);
}
SplayNode *Kth(int k)
{
SplayNode *r=Root;
while (r!=null)
{
if (k<=r->ch[]->size) r=r->ch[];
else if (k>=r->ch[]->size+ && k<=r->ch[]->size+r->num) return r;
else
{
k=k-r->ch[]->size-r->num;
r=r->ch[];
}
}
return r;
}
SplayNode *find(int v)
{
SplayNode *r=Root;
while (r!=null)
{
if (r->data==v)
{
splay(r,null);
return r;
}
r=r->ch[r->data<v];
}
return null;
}
SplayNode *pre()
{
SplayNode *r=Root->ch[];
if (r==null) return null;
while (r->ch[]!=null) r=r->ch[];
return r;
}
SplayNode *suc()
{
SplayNode *r=Root->ch[];
if (r==null) return null;
while (r->ch[]!=null) r=r->ch[];
return r;
}
void del(int v)
{
find(v);
SplayNode *q=pre();
SplayNode *p=suc();
if (q==null && p==null)
if (Root->num==) Root=null;
else Root->num--,Root->updata();
if (q==null)
{
splay(p,null);
if (Root->ch[]->num==) Root->ch[]=null,Root->updata();
else Root->ch[]->num--,splay(Root->ch[],null);
return;
}
if (p==null)
{
splay(q,null);
if (Root->ch[]->num==) Root->ch[]=null,Root->updata();
else Root->ch[]->num--,splay(Root->ch[],null);
return;
}
splay(q,null);
splay(p,q);
if (p->ch[]->num==) p->ch[]=null,p->updata();
else p->ch[]->num--,splay(p->ch[],null);
return;
}
}
void solve()
{
int temp,x;
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%d%d",&temp,&x);
if (temp==) Splay::insert(x);
if (temp==) Splay::del(x);
if (temp==) printf("%d\n",Splay::find(x)->ch[]->size+);
if (temp==) printf("%d\n",Splay::Kth(x)->data);
if (temp==)
{
Splay::insert(x);
printf("%d\n",Splay::pre()->data);
Splay::del(x);
}
if (temp==)
{
Splay::insert(x);
printf("%d\n",Splay::suc()->data);
Splay::del(x);
}
}
}
int main()
{
Splay::MakeTree();
solve();
return ;
}

【BZOJ3224】Tyvj 1728 普通平衡树 Splay的更多相关文章

  1. bzoj3224: Tyvj 1728 普通平衡树(splay)

    3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: #include<cstdio> #include<cstring> ...

  2. [bzoj3224]Tyvj 1728 普通平衡树——splay模板

    题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...

  3. bzoj3224: Tyvj 1728 普通平衡树(平衡树)

    bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...

  4. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  5. bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5354  Solved: 2196[Submit][Sta ...

  6. BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3224 题意概括 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. ...

  7. 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会

    平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...

  8. bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)

    (其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...

  9. 【Splay】bzoj3224 Tyvj 1728 普通平衡树

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> us ...

随机推荐

  1. Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片

    设置 背景颜色和背景图片 首先设置autoFillBackground属性为真然后定义一个QPalette对象设置QPalette对象的背景属性(颜色或图片)最后设置QWidget对象的Palette ...

  2. QQ互联OAuth

    /** * QQ互联 oauth * @author dyllen * */ class Oauth { //取Authorization Code Url const PC_CODE_URL = ' ...

  3. hdu 4741 2013杭州赛区网络赛 dfs ***

    起点忘记录了,一直wa 代码写的很整齐,看着很爽 #include<cstdio> #include<iostream> #include<algorithm> # ...

  4. 背景虚化 Google Camera App Nokia Refocus HTC One M8 的 Duo景深相机

    背景虚化是单反中一种比较常见的拍照形式,参看 http://www.techbang.com/posts/%2017842 https://refocus.nokia.com/

  5. Codeforces Round #370 (Div. 2) D. Memory and Scores DP

    D. Memory and Scores   Memory and his friend Lexa are competing to get higher score in one popular c ...

  6. MapKit地图划线

    只要用于获取用户位置都要取得用户授权 #import "ViewController.h" #import <MapKit/MapKit.h> @interface V ...

  7. c#基础系列(转)

    转:http://www.cnblogs.com/landeanfen/p/4953025.html C#基础系列——一场风花雪月的邂逅:接口和抽象类 前言:最近一个认识的朋友准备转行做编程,看他自己 ...

  8. hdu1166 线段树

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

  9. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

  10. css整理-04 基本视觉格式化

    基本框 假定每一个元素都会生成一个火多个矩形框,为元素框 元素框中心有一个内容区,周围有内边距,边距和外边距 内容的背景会应用到内边距,外边距是透明的,可以看到父元素的背景 内边距不能是负值,外边距可 ...