Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:
1.查询k在区间内的排名
2.查询区间内排名为k的值
3.修改某一位值上的数值
4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)
5.查询k在区间内的后继(后继定义为大于x,且最小的数)

Input

第一行两个数 n,m 表示长度为n的有序序列和m个操作
第二行有n个数,表示有序序列
下面有m行,opt表示操作标号
若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名
若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数
若opt=3 则为操作3,之后有两个数pos,k 表示将pos位置的数修改为k
若opt=4 则为操作4,之后有三个数l,r,k 表示查询区间[l,r]内k的前驱
若opt=5 则为操作5,之后有三个数l,r,k 表示查询区间[l,r]内k的后继

Output

对于操作1,2,4,5各输出一行,表示查询结果

Sample Input

9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5

Sample Output

2
4
3
4
9

HINT

1.n和m的数据范围:n,m<=50000

2.序列中每个数的数据范围:[0,1e8]

3.虽然原题没有,但事实上5操作的k可能为负数

Solution

板子题也没啥好写的……找个好看点的板子比如我的抄抄吧

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#define N (3000000+100)
using namespace std; int Root[N],sz,Father[N],Son[N][];
int Cnt[N],Val[N],Size[N];
int n,m,a[N],Ans,maxn; inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct Splay_Tree
{
int Get(int x) { return Son[Father[x]][]==x; }
void Update(int x) { Size[x]=Size[Son[x][]]+Size[Son[x][]]+Cnt[x]; }
void New(int x) { ++sz; Size[sz]=; Val[sz]=x; Cnt[sz]=; }
void Clear(int x) { Size[x]=Father[x]=Son[x][]=Son[x][]=Cnt[x]=Val[x]=; }
int Pre(int Root) { int now=Son[Root][]; while (Son[now][]) now=Son[now][]; return now; }
int Next(int Root) { int now=Son[Root][]; while (Son[now][]) now=Son[now][]; return now; } void Rotate(int x)
{
int wh=Get(x),fa=Father[x],fafa=Father[fa];
if (fafa) Son[fafa][Son[fafa][]==fa]=x;
Father[fa]=x; Son[fa][wh]=Son[x][wh^];
if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
Father[x]=fafa; Son[x][wh^]=fa;
Update(fa); Update(x);
}
void Splay(int &Root,int x)
{
for (int fa;fa=Father[x];Rotate(x))
if (Father[fa])
Rotate(Get(fa)==Get(x)?fa:x);
Root=x;
}
int Findx(int &Root,int x)
{
int now=Root;
while ()
if (x<=Size[Son[now][]])
now=Son[now][];
else
{
if (x<=Cnt[now])
{
Splay(Root,now);
return now;
}
x-=Cnt[now];
now=Son[now][];
}
}
int Find(int &Root,int x)
{
int now=Root,ans=;
while ()
{
if (!now) return ans;
if (x<Val[now])
now=Son[now][];
else
{
ans+=Size[Son[now][]];
if (Val[now]==x)
{
Splay(Root,now);
return ans;
}
ans+=Cnt[now];
now=Son[now][];
}
}
}
void Insert(int &Root,int x)
{
if (!Root) { New(x); Root=sz; return; }
int now=Root,fa=;
while ()
{
if (x==Val[now]) { ++Cnt[now]; Update(now); Splay(Root,now); return; }
fa=now; now=Son[now][x>Val[now]];
if (now==){ New(x); Father[sz]=fa; Son[fa][x>Val[fa]]=sz; Splay(Root,sz); return; }
}
}
void Delete(int &Root,int x)
{
Find(Root,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 oldroot=Root,pre=Pre(Root);
Splay(Root,pre);
Son[Root][]=Son[oldroot][];
Father[Son[oldroot][]]=Root;
Clear(oldroot);
Update(Root);
}
}; struct Segt_Tree
{
Splay_Tree Splay[];
void Get_rank(int node,int l,int r,int l1,int r1,int k)
{
if (l>r1 || r<l1) return;
if (l1<=l && r<=r1)
{
Ans+=Splay[node].Find(Root[node],k);
return;
}
int mid=(l+r)>>;
Get_rank(node<<,l,mid,l1,r1,k);
Get_rank(node<<|,mid+,r,l1,r1,k);
}
void Update(int node,int l,int r,int x,int k)
{
Splay[node].Delete(Root[node],a[x]);
Splay[node].Insert(Root[node],k);
if (l==r) return;
int mid=(l+r)>>;
if (x<=mid) Update(node<<,l,mid,x,k);
else Update(node<<|,mid+,r,x,k);
}
void Pre(int node,int l,int r,int l1,int r1,int k)
{
if (l>r1 || r<l1) return;
if (l1<=l && r<=r1)
{
Splay[node].Insert(Root[node],k);
int pre=Splay[node].Pre(Root[node]);
Ans=max(Ans,Val[pre]);
Splay[node].Delete(Root[node],k);
return;
}
int mid=(l+r)>>;
Pre(node<<,l,mid,l1,r1,k);
Pre(node<<|,mid+,r,l1,r1,k);
}
void Next(int node,int l,int r,int l1,int r1,int k)
{
if (l>r1 || r<l1) return;
if (l1<=l && r<=r1)
{
Splay[node].Insert(Root[node],k);
int next=Splay[node].Next(Root[node]);
Ans=min(Ans,next==?0x7fffffff:Val[next]);
Splay[node].Delete(Root[node],k);
return;
}
int mid=(l+r)>>;
Next(node<<,l,mid,l1,r1,k);
Next(node<<|,mid+,r,l1,r1,k);
}
void Ins(int node,int l,int r,int x,int k)
{
Splay[node].Insert(Root[node],k);
if (l==r) return;
int mid=(l+r)>>;
if (x<=mid) Ins(node<<,l,mid,x,k);
else Ins(node<<|,mid+,r,x,k);
}
}T; int main()
{
n=read(),m=read();
for (int i=; i<=n; ++i)
a[i]=read(),maxn=max(maxn,a[i]),T.Ins(,,n,i,a[i]);
int opt,l,r,k,pos;
for (int i=; i<=m; ++i)
{
opt=read();
switch(opt)
{
case :
{
l=read(),r=read(),k=read();
Ans=;
T.Get_rank(,,n,l,r,k);
printf("%d\n",Ans+);
break;
}
case :
{
l=read(),r=read(),k=read();
int L=,R=maxn;
while (L<R)
{
int mid=(L+R)>>;
Ans=;
T.Get_rank(,,n,l,r,mid);
if (Ans<k) L=mid+;
else R=mid;
}
printf("%d\n",L-);
break;
}
case :
{
pos=read(),k=read();
T.Update(,,n,pos,k);
a[pos]=k;
maxn=max(maxn,k);
break;
}
case :
{
l=read(),r=read(),k=read();
Ans=;
T.Pre(,,n,l,r,k);
printf("%d\n",Ans);
break;
}
case :
{
l=read(),r=read(),k=read();
Ans=0x7fffffff;
T.Next(,,n,l,r,k);
printf("%d\n",Ans);
break;
}
}
}
}

BZOJ3196:二逼平衡树(线段树套Splay)的更多相关文章

  1. 【BZOJ 3196】二逼平衡树 线段树套splay 模板题

    我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...

  2. BZOJ3196二逼平衡树——线段树套平衡树(treap)

    此为平衡树系列最后一道:二逼平衡树您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询 ...

  3. bzoj3196 二逼平衡树——线段树套平衡树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 人生中第一棵树套树! 写了一个晚上,成功卡时 9000ms+ 过了! 很要注意数组的大 ...

  4. bzoj 3196 && luogu 3380 JoyOI 1730 二逼平衡树 (线段树套Treap)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 题面; 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Se ...

  5. BZOJ - 3196 Tyvj 1730 二逼平衡树 (线段树套treap)

    题目链接 区间线段树套treap,空间复杂度$O(nlogn)$,时间复杂度除了查询区间k大是$O(log^3n)$以外都是$O(log^2n)$的. (据说线段树套线段树.树状数组套线段树也能过?) ...

  6. [bzoj3196]Tyvj 1730 二逼平衡树——线段树套平衡树

    题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查 ...

  7. 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义 ...

  8. bzoj 3196二逼平衡树 线段树套平衡树

    比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细) //By BLADEVIL ...

  9. [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在 ...

随机推荐

  1. php mktime()函数

    PHP提供一个很好用的函数mktime().你只要按顺序传送给mktime()你希望表示的小时,分钟,秒数,月份,日期,及年份,mktime()就会返回该日期自1970年1月1日的总秒数.获取2000 ...

  2. table中列复选框全选,再选 效果

    <table class="table table-striped sortable table-bordered table-hover " id="zdnews ...

  3. golang学习之生成代码文档

    go doc 工具会从 Go 程序和包文件中提取顶级声明的首行注释以及每个对象的相关注释,并生成相关文档. 一般用法: go doc package 获取包的文档注释,例如:go doc fmt 会显 ...

  4. 一:Maven知识整理

    一:maven的好处 1.依赖管理:对jar包的统一管理 可以节省空间 2.项目一键构建: 编码 编译 测试(junit) 运行 打包 部署 一个 tomcat:run就能把项目运行起来 Maven能 ...

  5. Spring Cloud面试题

    引言 面试中面试官喜欢问组件的实现原理,尤其是常用技术,我们平时使用了SpringCloud还需要了解它的实现原理,这样不仅起到举一反三的作用,还能帮助轻松应对各种问题及有针对的进行扩展.以下是 课程 ...

  6. javascript图片预加载

    图片预加载是非常常见的一个功能,PC和移动端都会用到,尤其是在移动端,只要涉及到较多图片的加载都会用到该技术.下面是移动端用到的,引入了zepto. <!DOCTYPE html> < ...

  7. AngularJs动态添加元素和删除元素

    动态添加元素和删除元素 //通过$compile动态编译html var html="<div ng-click='test()'>我是后添加的</div>" ...

  8. iview中upload组件上传图片,跨域

    前提:先前开发了一个A项目,A项目中有一套上传图片的接口,现在开发B项目. B项目开发中用iview中的upload组件上传图片,用到了A项目中上传接口,爬坑经历 1.涉及到了跨域解决:后台配置一下文 ...

  9. iview中关于table组件内放入Input会失去焦点

    table里面的数据是一个数组,父组件传入的.子组件是截图的内容.当每个input框数据发生变化时,把数据传给父组件.在父组件做表单的提交. github内已经提到过这个问题(https://gith ...

  10. BottomBar之Android底部菜单

    BottomBar之Android底部菜单 前言:开源项目BottomBar,实现Android底部菜单(常用菜单,BottomBar实现动画(上下式)+消息菜单,BottomBar+ViewPage ...