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. Firebird Case-Insensitive Searching 大小写不敏感查找

    Firebird 默认是大小写敏感,在检索的时候. 要想不敏感检索,两种方法: 1.where upper(name) = upper(:flt_name) 2.检索时指定字符集collation,例 ...

  2. [转]oracle中查看用户权限

    本文转自:http://www.cnblogs.com/QDuck/archive/2010/08/11/1797225.html 1.查看所有用户:   select * from dba_user ...

  3. ASP.NET MVC4 新手入门教程之七 ---7.向电影模式和表中添加新字段

    在这一节中,您将使用实体框架代码第一次迁移,迁移到模型类的一些变化,所以该更改应用于数据库. 默认情况下,当您使用实体框架代码优先将自动创建一个数据库,像你那样早些时候在本教程中,代码第一次添加一个表 ...

  4. 用c+libcurl+PCRE写爬虫1--编译libcurl

    打算用c语言和libcurl库在windows下实现一些爬虫操作. 一.编译libcurl 1.编译zlib 1)下载zlib http://sourceforge.net/projects/libp ...

  5. 2018-12-25 课堂笔记&面试题

    面试题一.Java中,八大基本数据类型有哪些?答:数值型:整型(byte.short.int.long)浮点型(float.double)非数值型:布尔类型(boolean)字符型(char).注意: ...

  6. shell编程之while死循环

    原文 在linux下编程的程序猿都知道shell脚本,就算你不怎么熟悉,也应该听过的吧!那在shell脚本中的死循环该怎么写呢? 对于熟悉C语言的猿人们来说,最简单的死循环应该这样写: ------- ...

  7. 二、NAT(地址转换模式)

    刚刚我们说到,如果你的网络ip资源紧缺,但是你又希望你的虚拟机能够联网,这时候NAT模式是最好的选择.NAT模式借助虚拟NAT设备和虚拟DHCP服务器,使得虚拟机可以联网.其网络结构如下图所示: NA ...

  8. PAT 1048. Find Coins

    two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include &l ...

  9. 第9章 CSS3中的变形与动画(下)

    Keyframes介绍 Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以"@keyframes"开头,后面紧跟着是动画名称加上一对花括号" ...

  10. 盒模型的属性丶display显示丶浮动

    一丶盒模型的属性(重要) 1.padding padding是标准文档流,父子之间调整位置 <!DOCTYPE html> <html> <head> <me ...