解题关键:由于需要根据平衡进行重建,所以不能进行去重,否则无法保证平衡性。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll; const double alpha=0.7;
const int N=1e5+;
int n; namespace ScapegoatTree{
struct node{
int l,r,v,sz,valid;
bool del;
}t[N<<];
int tot=,rt=;
#define ls(o) t[o].l
#define rs(o) t[o].r
#define pb push_back
int new_node(int x){++tot;t[tot].l=t[tot].r=;t[tot].v=x;t[tot].sz=t[tot].valid=;t[tot].del=;return tot;}
bool Bad(int o){
return (double)t[ls(o)].sz>alpha*t[o].sz||(double)t[rs(o)].sz>alpha*t[o].sz;
}
void Updata(int o){
t[o].sz=t[ls(o)].sz+t[rs(o)].sz+;
t[o].valid=t[ls(o)].valid+t[rs(o)].valid+!t[o].del;
}
void Dfs(int o,std::vector<int> &v){
if(!o) return;
Dfs(ls(o),v);
if(!t[o].del) v.pb(o);
Dfs(rs(o),v);
}
int Build(std::vector<int> &v,int l,int r){
if(l>r) return ;//原因是右边界不包含
int mid=(l+r)>>,o=v[mid];
ls(o)=Build(v,l,mid-);
rs(o)=Build(v,mid+,r);
Updata(o);
return o;
}
void ReBuild(int &o){
std::vector<int>v;
Dfs(o,v);
o=Build(v,,(int)v.size()-);
}
void Insert(int x,int &o){
if(!o){
o=new_node(x);
return ;
}
if(x>=t[o].v) Insert(x,rs(o));
else Insert(x,ls(o));
Updata(o);
if(Bad(o)) ReBuild(o);
return;
}
//del with rnk
void Delete(int o,int Rnk){
if(!t[o].del&&Rnk==t[ls(o)].valid+) {
t[o].del=;
--t[o].valid;
return;
}
if(Rnk<=t[ls(o)].valid+!t[o].del) Delete(ls(o),Rnk);
else Delete(rs(o),Rnk-t[ls(o)].valid-!t[o].del);
Updata(o);
}
int GetRank(int o,int x){
int ans=;
while(o){
if(t[o].v>=x) o=ls(o);
else{
ans+=t[ls(o)].valid+!t[o].del;
o=rs(o);
}
}
return ans;
}
int FindKth(int o,int x) {
while(o){
if(!t[o].del&&t[ls(o)].valid+==x) {return t[o].v;}
if(t[ls(o)].valid>=x) o=ls(o);
else {
x-=t[ls(o)].valid+!t[o].del;
o=rs(o);
}
}
}
int GetPred(int o,int x){
return FindKth(o,GetRank(o,x)-);
}
int GetSucc(int o,int x){
return FindKth(o,GetRank(o,x+));
}
}
using namespace ScapegoatTree; int main() {
scanf("%d",&n);
rt=;
while(n--) {
int op,x;
scanf("%d%d",&op,&x);
if(op==) Insert(x,rt);
if(op==) Delete(rt,GetRank(rt,x));
if(op==) printf("%d\n",GetRank(rt,x));
if(op==) printf("%d\n",FindKth(rt,x));
if(op==) printf("%d\n",GetPred(rt,x));
if(op==) printf("%d\n",GetSucc(rt,x));
}
return ;
}

[luogu3369]普通平衡树(替罪羊树模板)的更多相关文章

  1. [TYVJ1728/BZOJ3224]普通平衡树-替罪羊树

    Problem 普通平衡树 Solution 本题是裸的二叉平衡树.有很多种方法可以实现.这里打的是替罪羊树模板. 此题极其恶心. 前驱后继模块需要利用到rank模块来换一种思路求. 很多细节的地方容 ...

  2. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  3. 平衡树 替罪羊树(Scapegoat Tree)

    替罪羊树(Scapegoat Tree) 入门模板题 洛谷oj P3369 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入xx数 删除xx数(若有多个相同 ...

  4. bzoj2827: 千山鸟飞绝 平衡树 替罪羊树 蜜汁标记

    这道题首先可以看出坐标没有什么意义离散掉就好了. 然后你就会发现你要每次都更改坐标,而一旦更改受影响的是坐标里的所有数,要是一个一个的改,会不可描述. 所以换个视角,我们要找的是某只鸟所到每个坐标时遇 ...

  5. bzoj 3224: Tyvj 1728 普通平衡树 替罪羊树

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

  6. BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 22483  Solved: 10130[Submit][S ...

  7. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  8. 替罪羊树模板(BZOJ1056/1862)

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #defin ...

  9. [luogu3369] 普通平衡树(splay模板)

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

随机推荐

  1. Android数据库代码优化(1) - 从Google的数据库guide说起

    假如我们没有任何在Android上使用SQLite的经验,现在要开始在工作中用SQLite存储一些数据.OK, 我们去看google的官方培训文档吧,http://developer.android. ...

  2. HAWQ取代传统数仓实践(十八)——层次维度

    一.层次维度简介 大多数维度都具有一个或多个层次.例如,示例数据仓库中的日期维度就有一个四级层次:年.季度.月和日.这些级别用date_dim表里的列表示.日期维度是一个单路径层次,因为除了年-季度- ...

  3. Go语言开发中MongoDB数据库

    伴随着移动端的兴起,Nosql数据库以其分布式设计和高性能等特点得到了广泛的应该用,下面将介绍下Nosql中的mongoDB在Go语言中的应用,在开发前,有必要了解下基础知识 在开发前,导入开发需要用 ...

  4. HSRP/VRRP/GLBP

    当网络足够大的时候,网络规划师要考虑的技光是网络本身的性能问题,冗余技术也是必不可少的. 常见的冗余网关技术有• 热备份路由协议(HSRP).• 虚拟路由器冗余协议(VRRP)• 网关负载均衡协议(G ...

  5. PenMount Touch显示鼠标指针驱动安装

    /******************************************************************************* * PenMount Touch显示鼠 ...

  6. LeetCode Optimal Division

    原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...

  7. Weblogic配置SSl使用Https

    一 .可以开启自带的SSL连接 启动weblogic,进入左侧菜单,点击左侧的安全领域-->点击myrealm-->点击角色和策略-->点击服务器AdminServer 点击保存,w ...

  8. WARNING: at drivers/gpio/gpiolib.c:101 gpio_ensure_requested+0x5c/0x118()

    使用输入子系统实现的按键程序,每次按键后进入中断,就会报错如下: ---input_key_handler--- ------------[ cut here ]------------ WARNIN ...

  9. 关于.NET中的Session

    Asp.net 默认配置下,Session莫名丢失的原因及解决办法正常操作情况下Session会无故丢失.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成6 ...

  10. 在rac集群上开启OEM

    由于安装rac的时候没有开启oem,这里开启oem,方便管理 [oracle@rac01 ~]$ emca -config dbcontrol db -repos create -cluster ST ...