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. java服务器访问其他服务器工具类编写

    java服务器访问其他服务器工具类编写适合各种消息推送及微服务交互 package com.xiruo.medbid.components; import com.xiruo.medbid.util. ...

  2. [转]Web API Introduction to OData Services using ASP.NET Web API

    本文转自:http://mahedee.net/tag/web-api/ What is OData? OData Stands for Open Data Protocol. It is a dat ...

  3. Apache2.4+PHP7.3 安装及整合教程[Windows]

    系统环境:Win10 64位 Apache版本:2.4.37 64位 PHP版本:7.3.1 64位 下载 安装的第一步肯定是下载了,可以直接到我的网盘(密码:18tp)下载,下载完成后将文件解压到你 ...

  4. 鼠标拖动改变DIV等网页元素的大小的最佳实践

    1.初次实现 1.1 html代码 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" la ...

  5. poj Corn Fields 状态压缩dp。

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5763   Accepted: 3052 Descr ...

  6. python 之 os._exit() sys.exit() 、exit()

    sys.exit 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 退出程序引发SystemExit异常,(这是唯一一个不会被认为是 ...

  7. thinkphp下mysql用用户名或者手机号登录

    $res=$user->where("login_id='{$username}' OR phone='{$username}'")->find(); $phone=I ...

  8. 在HTML代码中使用freemarker

    在HTML代码中使用freemarker 1.freemarker中显示某对象的属性使用${user.name}. 但如果name为null,freemarker就会报错.如果需要判断对象是否为空: ...

  9. ElementUI组件库常见方法及问题汇总(持续更新)

    本文主要介绍在使用ElementUI组件库的时候,常遇见的问题及使用到的方法,汇总记录便于查找. 1.表单 阻止表单的默认提交 <!-- @submit.native.prevent --> ...

  10. C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...