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.每个数的数据范围:[-2e9,2e9]

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)的更多相关文章

  1. bzoj3224 普通平衡树(splay 模板)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 11427  Solved: 4878[Submit][St ...

  2. [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)

    解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...

  3. bzoj3224 普通平衡树 splay模板

    题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...

  4. 【BZOJ3224】Tyvj 1728 普通平衡树 Splay

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  5. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  6. 【转】 史上最详尽的平衡树(splay)讲解与模板(非指针版spaly)

    ORZ原创Clove学姐: 变量声明:f[i]表示i的父结点,ch[i][0]表示i的左儿子,ch[i][1]表示i的右儿子,key[i]表示i的关键字(即结点i代表的那个数字),cnt[i]表示i结 ...

  7. hiho #1329 : 平衡树·Splay

    #1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...

  8. Hihocoder 1329 平衡树·Splay(平衡树)

    Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...

  9. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

随机推荐

  1. 微信WeUI常见页面模板

    购物车模板 就是popup弹层(css样式+js),还有slider滑动操作,还有增减的js 代码: <!DOCTYPE html> <html lang="zh-CN&q ...

  2. as3中xml文件的加载和读取

    ---恢复内容开始--- as代码如下: xml如下: 总结: 用URLReuqest对象加载xml的url 创建一个URLLoader对象,将1中的URLRequest指定给他 给URLLoader ...

  3. 将字符串 “ hello word,你 好 世 界 ! ” 两端空格去掉并且将其中的其他所有空格替换成一个空格 输出结果为“hello word,你 好 世界”

    string str = " hello word,你 好 世 界 ! "; string msg = str.Trim(); //去掉首尾空格 //使用split分割字符串,st ...

  4. C# 条码生成类

    using System.Collections; using System.Text.RegularExpressions; namespace DotNet.Utilities { public ...

  5. 运行javac编译报错:仅当显式请求注释处理时才接受类名称“xxxxxx”

    发生原因:运行javac编译时没有加上扩展名.解决方法:加上.java扩展名重新编译即可,"xxxxxx.java".

  6. dubbo客户端源码分析(一)

    rpc框架有很多,公司自研.开源的thrift.dubbo.grpc等.我用过几个框架,了解了一下实现原理,客户端基本都是用代理实现,jdk动态代理.cglib等.最近一段时间想了解一下dubbo源码 ...

  7. MySQL之多表查询练习

    一.表格 表一  emp 表二 dept 表三 salgrade; 表四 年度利润表 二.习题 1. 查出至少有一个员工的部门.显示部门编号.部门名称.部门位置.部门人数. 2. 列出所有员工的姓名及 ...

  8. Web知识简易介绍及HTTP知识总结

    一.软件系统体系结构: 常见软件系统体系结构B/S.C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 缺点:软件更新是需要同时更新客户端和服务器端两端,比较麻烦 优点:安全 ...

  9. C++ STL:vector实现

    练习一发,主要是使用placement new在原始内存上创建对象.半路md面试电话来了,赶紧存档,看Java大法 #include <iostream> #include <cst ...

  10. Bzoj4044 Virus synthesis

    题意 你要用 \(ATGC\) 四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部 一开始已有串为空.求最 ...