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] $ 内排 ...
随机推荐
- iOS 多线程安全 与可变数组
完全来自于iOS 多线程安全与可变字典 的学习 基本相同,举一反三 直接上样例代码 是我参照网上,根据当前业务需求改的. 其实好多人在这里喜欢用类别处理.我个人觉得用类别 极其容易和普通方法混淆,所以 ...
- gh-ost原理
gh-ost原理 一.三种模式架构图 1.连上从库,在主库上修改 这是gh-ost默认的工作模式,它会查看从库情况,找到集群的主库并且连接上去,对主库侵入最少,大体步骤是: 在主库上创建_xxx_gh ...
- APPIUM API整理(python)---其他辅助类
App运行类 1.current_activity current_activity(self): 用法: print(driver.current_activity()) Retrieves the ...
- Linux常用监控命令
A goal is a dream with a deadline. Much effort, much prosperity. 1.IOSTAT命令 此命令安装包为sysstat 可用yu ...
- Autofac register and resolve
Passing Parameters to Register When you register components you have the ability to provide a set of ...
- 关于C/C++中main函数参数的学习
因为面对对象作业(2018.5.21)的要求,去学习了C/C++中main函数参数的意义,以及一些简单的使用(从命令行指令的接受),不给予赘述.(仅为个人拙见,还望看官指正) 首先,带有参数的main ...
- All Classic Bluetooth profile for iPhone
iPhone BC profiles Profile Decription HFP1.6 1.通知客户端有电话拨入:2.免提功能:3.音频的输入输出机制. PBAP 1.下载通讯录:2.查找通讯录:3 ...
- 谷歌SEO老域名注册完全攻略
老域名有优势,不管在百度和谷歌都是一样的. 我们查看搜索结果就能发现,谷歌里面很多排前十的网站都是N年前的,一零年以后的算是比较新的,很多产品网站域名是零几年,甚至很多排名更好的域名是九几年的. 谷歌 ...
- 一个问题:C#引用类型传参,说出你的答案
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { People p = new Peo ...
- AIDL与Binder的区别
Binder是一个远程对象的基础类,核心部分是远程调用机制,这部分是由IBinder定义的. 它是对IBinder类的实现,其中IBinder类提供了这样一个类的标准的本地化实现方式. 大多数开发者不 ...