BZOJ - 3196 Tyvj 1730 二逼平衡树 (线段树套treap)
区间线段树套treap,空间复杂度$O(nlogn)$,时间复杂度除了查询区间k大是$O(log^3n)$以外都是$O(log^2n)$的。
(据说线段树套线段树、树状数组套线段树也能过?)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e4+,inf=0x3f3f3f3f;
#define lson (u<<1)
#define rson (u<<1|1)
#define mid ((l+r)>>1)
int n,m,a[N],rt[N<<],ch[N*][],val[N*],siz[N*],rd[N*],tot;
void pu(int u) {siz[u]=siz[ch[u][]]+siz[ch[u][]]+;}
int newnode(int x) {int u=++tot; val[u]=x,siz[u]=,ch[u][]=ch[u][]=,rd[u]=rand(); return u;}
void rot(int& u,int f) {
int v=ch[u][f];
ch[u][f]=ch[v][f^],ch[v][f^]=u;
pu(u),pu(v),u=v;
}
void ins(int& u,int x) {
if(!u) {u=newnode(x); return;}
int f=x>val[u];
ins(ch[u][f],x);
if(rd[ch[u][f]]>rd[u])rot(u,f);
if(u)pu(u);
}
void del(int& u,int x) {
if(!u)return;
if(val[u]==x) {
if(!ch[u][]||!ch[u][])u=ch[u][]|ch[u][];
else {
int f=rd[ch[u][]]>rd[ch[u][]];
rot(u,f),del(ch[u][f^],x);
}
} else del(ch[u][x>val[u]],x);
if(u)pu(u);
}
int rnk(int u,int x) {
int ret=;
for(; u; u=ch[u][x>val[u]])if(x>val[u])ret+=siz[ch[u][]]+;
return ret+;
}
int kth(int u,int k) {
while(k!=siz[ch[u][]]+) {
if(k<siz[ch[u][]]+)u=ch[u][];
else k-=siz[ch[u][]]+,u=ch[u][];
}
return val[u];
}
int lb(int u,int x) {
int ret=;
for(; u; u=ch[u][x>val[u]])if(val[u]<x)ret=val[u];
return ret;
}
int ub(int u,int x) {
int ret=;
for(; u; u=ch[u][x>=val[u]])if(val[u]>x)ret=val[u];
return ret;
}
void upd(int p,int x,int u=,int l=,int r=n) {
del(rt[u],a[p]),ins(rt[u],x);
if(l==r)return;
p<=mid?upd(p,x,lson,l,mid):upd(p,x,rson,mid+,r);
}
void build(int u=,int l=,int r=n) {
for(int i=l; i<=r; ++i)ins(rt[u],a[i]);
if(l==r)return;
build(lson,l,mid),build(rson,mid+,r);
}
int qry1(int L,int R,int x,int u=,int l=,int r=n) {
if(l>=L&&r<=R) {return rnk(rt[u],x)-;}
if(l>R||r<L)return ;
return qry1(L,R,x,lson,l,mid)+qry1(L,R,x,rson,mid+,r);
}
int qry2(int L,int R,int k) {
int l=,r=inf;
while(l<r)qry1(L,R,mid+)>=k?r=mid:l=mid+;
return l;
}
int qry4(int L,int R,int x,int u=,int l=,int r=n) {
if(l>=L&&r<=R) {int t=lb(rt[u],x); return t?t:~inf;}
if(l>R||r<L)return ~inf;
return max(qry4(L,R,x,lson,l,mid),qry4(L,R,x,rson,mid+,r));
}
int qry5(int L,int R,int x,int u=,int l=,int r=n) {
if(l>=L&&r<=R) {int t=ub(rt[u],x); return t?t:inf;}
if(l>R||r<L)return inf;
return min(qry5(L,R,x,lson,l,mid),qry5(L,R,x,rson,mid+,r));
}
int main() {
srand(time());
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)scanf("%d",&a[i]);
build();
while(m--) {
int f,l,r,x;
scanf("%d",&f);
if(f==)scanf("%d%d%d",&l,&r,&x),printf("%d\n",qry1(l,r,x)+);
else if(f==)scanf("%d%d%d",&l,&r,&x),printf("%d\n",qry2(l,r,x));
else if(f==)scanf("%d%d",&l,&x),upd(l,x),a[l]=x;
else if(f==)scanf("%d%d%d",&l,&r,&x),printf("%d\n",qry4(l,r,x));
else if(f==)scanf("%d%d%d",&l,&r,&x),printf("%d\n",qry5(l,r,x));
}
return ;
}
BZOJ - 3196 Tyvj 1730 二逼平衡树 (线段树套treap)的更多相关文章
- 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义 ...
- [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在 ...
- bzoj 3196 && luogu 3380 JoyOI 1730 二逼平衡树 (线段树套Treap)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 题面; 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Se ...
- [bzoj3196]Tyvj 1730 二逼平衡树——线段树套平衡树
题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查 ...
- bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1807 Solved: 772[Submit][Stat ...
- bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ...
- BZOJ 3196: Tyvj 1730 二逼平衡树( 树套树 )
这道题做法应该很多吧.... 我用了线段树套treap.... -------------------------------------------------------------------- ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 树套树 线段树 treap
http://www.lydsy.com/JudgeOnline/problem.php?id=3196 http://hzwer.com/2734.html 线段树套treap,似乎splay也可以 ...
- BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay
传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...
随机推荐
- Java 基础总结(一)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. String,StringBuffer,StringBuilder 1) ...
- WPF MVVM模式下ComboBox级联效果 选择第一项
MVVM模式下做的省市区的级联效果.通过改变ComboBox执行命令改变市,区. 解决主要问题就是默认选中第一项 1.首先要定义一个属性,继承自INotifyPropertyChanged接口.我这里 ...
- UDP协议----简单的CS模型实现
UDP简单介绍 传输层主要应用的协议模型有两种,一种是TCP协议,另外一种则是UDP协议.TCP协议在网络通信中占主导地位,绝大多数的网络通信借助TCP协议完成数据传输.但UDP也是网络通信中不可或缺 ...
- JVM调优总结(二)
Java对象的大小 基本数据的类型的大小是固定的,这里就不多说了.对于非基本类型的Java对象,其大小就值得商榷. 在Java中,一个空Object对象的大小是8byte,这个大小只是保存堆中一个没有 ...
- 最牛技术 1秒启动Linux的窍门
1秒启动Linux可以实现吗?我们知道Linux系统开机并不算快,最少也需要11秒,但是,现在有一个技巧,可以1秒打开linux系统,到底是什么技术这么牛?请看下文详细介绍 尽可能快的启动系统,对于自 ...
- Flume-NG源码阅读之FileChannel
FileChannel是flume一个非常重要的channel组件,非常常用.这个channel非常复杂,涉及的文件更多涉及三个包:org.apache.flume.channel.file.org. ...
- JSP数据交互(一)
1.JSP内置对象 请求对象:request 输出对象:out 响应对象:response 应用程序对象:application 会话对象:session 页面上下文对象:pageContext 页面 ...
- LeetCode第[14]题(Java): Longest Common Prefix
题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...
- WPF DataGrid 分组
public ListCollectionView collection; collection = new ListCollectionView(obj.empData); collection.G ...
- Python 面向对象-------补充
Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...