这是一个treap裸题,还可以用splay,替罪羊树,线段树等等写

treap是树和堆结合,可以方便的在O(log(n))期望时间内进行以下操作,因此treap又叫做名次树

  1. 插入x数

  2. 删除x数(若有多个相同的数,因只删除一个)

  3. 查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)

  4. 查询排名为x的数

  5. 求x的前驱(前驱定义为小于x,且最大的数)

  6. 求x的后继(后继定义为大于x,且最小的数)
#include<bits/stdc++.h>
#include<ext/rope>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std;
using namespace __gnu_cxx; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct Node{
Node* ch[];
int r;//排名
int v;//权值
int s;//节点数
int flag;
Node()
{
ch[]=ch[]=NULL;
r=rand();v=;
s=flag=;
}
};
Node *root;
void maintain(Node* &o)//更新当前结点子树的节点数
{
o->s=o->flag;
if(o->ch[]!=NULL)o->s+=o->ch[]->s;//左子树不为空就加上数量
if(o->ch[]!=NULL)o->s+=o->ch[]->s;//右子树不为空就加上数量
}
void Rotate(Node* &o,int d)//d=0左旋,d=1右旋
{
Node* k=o->ch[d^];
o->ch[d^]=k->ch[d];
k->ch[d]=o;
maintain(o);maintain(k);
o=k;
}
void Insert(Node* &o,int d)
{
if(o==NULL)//子树为空直接加上去
{
o=new Node;
o->v=d;
return ;
}
else if(o->v==d)//可能会相等
{
o->flag++;
maintain(o);
return ;
}
int d1=d>o->v ? :;//判断搜索哪一颗子树
Insert(o->ch[d1],d);
if(o->ch[d1]->r > o->r)Rotate(o,d1^);
else maintain(o);
}
void Remove(Node* &o,int d)
{
if(o==NULL)return ;
if(o->v==d)
{
if(o->flag>)o->flag--;//该节点有多个数
else
{
if(o->ch[]==NULL&&o->ch[]==NULL)o=NULL;
else if(o->ch[]!=NULL&&o->ch[]!=NULL)
{
if(o->ch[]->r > o->ch[]->r)Rotate(o,),Remove(o->ch[],d);
else Rotate(o,),Remove(o->ch[],d);
}
else //有一个节点不为空,就拿另一个补上
{
if(o->ch[]==NULL)o=o->ch[];
else o=o->ch[];
}
}
if(o!=NULL)maintain(o);
}
else //根据d的大小找左右子树
{
if(d < o->v)Remove(o->ch[],d);
else if(d > o->v)Remove(o->ch[],d);
if(o!=NULL)maintain(o);
}
}
int kth(Node* o,int k)//查询排名为k的节点
{
if(k<||k > o->s||o==NULL)return ;//不合法的情况
int ss=;//左子树的节点
if(o->ch[]!=NULL)ss=o->ch[]->s;
if(k>=ss+&&k<=o->flag+ss)return o->v;
if(ss>=k)return kth(o->ch[],k);//左边够大了,从左边找
else return kth(o->ch[],k-ss-o->flag);
}
int Rank(Node* o,int k)//查询k的排名
{
if(o==NULL)return ;
int ss=;
if(o->ch[]!=NULL)ss=o->ch[]->s;
if(o->v==k)return ss+;
if(o->v > k)return Rank(o->ch[],k);//向左搜
else return ss+o->flag+Rank(o->ch[],k);//加上左子树的节点
}
void qq(Node* o,int k,int &ans)
{
if(o==NULL)return ;
if(o->v < k)
{
ans=max(ans,o->v);
if(o->ch[]!=NULL)qq(o->ch[],k,ans);
}
else
{
if(o->ch[]!=NULL)qq(o->ch[],k,ans);
}
}
void hj(Node* o,int k,int &ans)
{
if(o==NULL)return ;
if(o->v > k)
{
ans=min(ans,o->v);
if(o->ch[]!=NULL)hj(o->ch[],k,ans);
}
else
{
if(o->ch[]!=NULL)hj(o->ch[],k,ans);
}
}
int main()
{
srand(time(NULL));
int n;
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
if(a==)Insert(root,b);
else if(a==)Remove(root,b);
else if(a==)printf("%d\n",Rank(root,b));
else if(a==)printf("%d\n",kth(root,b));
else if(a==)
{
int ans=;qq(root,b,ans);
printf("%d\n",ans);
}
else
{
int ans=1e9;hj(root,b,ans);
printf("%d\n",ans);
}
}
return ;
}
/******************* ********************/

洛谷p3369 treap的更多相关文章

  1. 洛谷P3369 【模板】普通平衡树(Treap/SBT)

    洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...

  2. 【洛谷P3369】【模板】普通平衡树题解

    [洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...

  3. 洛谷P3369普通平衡树(Treap)

    题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...

  4. 在洛谷3369 Treap模板题 中发现的Splay详解

    本题的Splay写法(无指针Splay超详细) 前言 首先来讲...终于调出来了55555...调了整整3天..... 看到大部分大佬都是用指针来实现的Splay.小的只是按照Splay的核心思想和原 ...

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

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

  6. 洛谷 P3369 【模板】普通平衡树 (Treap)

    题目链接:P3369 [模板]普通平衡树 题意 构造一种数据结构满足给出的 6 种操作. 思路 平衡树 平衡树的模板题. 先学习了一下 Treap. Treap 在插入结点时给该结点随机生成一个额外的 ...

  7. AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369

    [模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...

  8. 洛谷P3369 【模板】普通平衡树(FHQ Treap)

    题面 传送门 题解 写了一下\(FHQ\ Treap\) //minamoto #include<bits/stdc++.h> #define R register #define inl ...

  9. [洛谷P3369] 普通平衡树 Treap & Splay

    这个就是存一下板子...... 题目传送门 Treap的实现应该是比较正经的. 插入删除前驱后继排名什么的都是平衡树的基本操作. #include<cstdio> #include< ...

随机推荐

  1. 机器学习10—K-均值聚类学习笔记

    机器学习实战之K-Means算法 test10.py #-*- coding:utf-8 import sys sys.path.append("kMeans.py") impor ...

  2. Auto Layout之创建布局约束

    上篇文章给大家介绍了AutoLayout 的由来及OC中关于AutoLayout 的类.这篇文章将向大家介绍,在iOS 编程中怎样使用Auto Layout 构建布局约束. 创建布局约束 创建布局约束 ...

  3. mybatis介绍安装

    MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简单 ...

  4. VS里用Sos扩展调试CLR总结

    暂时不用Windbg,用VS来调试CLR. 1) Enable unmanaged debugging 2) Load sos.dll into vs. !DumpHead -type SimpleC ...

  5. Unity导出AssetBundle到指定路径

    using System.Collections; using UnityEngine; using UnityEditor; using System.IO; /// <summary> ...

  6. iOS将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法

    demo下载地址 http://pan.baidu.com/s/1pLcpKpl 1.Unity导出工程时设置bundle id要与项目一致 2.修改bit code为NO 3.删除Main.stor ...

  7. 解决QT:forward declaration of &#39;struct Ui::xxx&#39;;invalid use of incomplete struct &quot;Ui::Widget&quot; 等莫名奇异错误

    今天在进行QT Widget的UI设计时,改了下Widget的对象名,然后在多次成功编译执行后,执行清理,又一次构建,就出现了好多莫名奇异的错误: widget.h:12: 错误:forward de ...

  8. JUNIT单元测试时统计代码的覆盖率工具eclemma安装

    EclEmma是Eclipse里的一个插件,安装简单,覆盖率显示直观.安装EclEmma.打开Eclipse,点击Help → Software Updates → Find and Install, ...

  9. winphone 中的指针和手势事件

    注意  有关这些主题的最新概述,请参阅指针事件和手势事件主题. 小心  W3C 指针事件规范自从在 Internet Explorer 10 中实施以来,已经经历了多次修订.此外,截止到 Intern ...

  10. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 错误解决办法

    This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log ...