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. DIY了一下自己blog的UI

    当年才学前端时就想改自己blog的UI,然鹅当时没看见那个“申请JS权限”,一直以为blog不能随意DIY样式,只改了少许CSS.现在重新看看设置管理选项,简单修改了一下样式(注意:修改样式之前发邮件 ...

  2. CSS之APP开发比较实用的CSS属性

    简介:本人刚入前端没多久,在做APP的开发的时候,经常遇到一些奇怪的问题,本人经验少,会使用js来解决css上的问题,但,却不知道其实有些css已经帮我们解决了. 1,white-space: now ...

  3. 十二 Cent OS下 tomcat启动项目响应速度很慢

    在tomcat部署了web项目,每次启动项目都需要花费2-3分钟,甚至有的时候需要花费10分钟左右,实在是太慢了. 在网上查找解决方案,把 jdk/jre/lib/security/java.secu ...

  4. 基于bootstrap的图片轮播功能

    插入js及css支持: <link rel="stylesheet" href="css/bootstrap.min.css"/> <scri ...

  5. 中南oj 1216: 异或最大值 数据结构

    1216: 异或最大值 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 98  Solved: 29 [Submit][Status][Web Boar ...

  6. wampserevr安装redis和mongo扩展

    1.下载redis对应的扩展.dll文件(php_redis.dll)和php_igbinary.pdb文件以及php_mongo.dll文件(对应版本)慢慢试 2.把找好的对应好的版本放到 D:\p ...

  7. 车厢调度(train.cpp)

    车厢调度(train.cpp) [问题描述]        有一个火车站,铁路如图所示,每辆火车从A驶入,再从B方向驶出,同时它的车厢可以重新组合.假设从A方向驶来的火车有n节(n<=1000) ...

  8. js表单快速取值/赋值 快速生成下拉框

    1.表单取值/赋值公共方法 //表单序列化:文本框的name字段和数据源一致<form id="myForm" onsubmit="return false;&qu ...

  9. Spring中无法访问resources目录下页面或静态资源

    1.新建项目,在 resources 目录下创建 views 目录,在 views 目录下创建 index.html 页面,项目跑起来,浏览器访问页面,提示找不到页面之类的错误提示. 2.再尝试访问图 ...

  10. 微信小程序开发5-WXML

    1.HTML元素是构建网页的一种单位,是由HTML标签和HTML属性组成的,HTML元素也是网页中的一种基本单位.HTML与其他标记语言一样,HTML的关键,是标签(tag).HTML标签是HTML语 ...