【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap
奉上sth神犇的模板:
- //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排名,查询区间某个值值前驱、后继。查询第k小值是log^3(n)的,其他都是log^2(n)的
- #include <cstdio>
- using namespace std;
- const int maxn=,inf=;
- int a[maxn],f[maxn],sum[maxn],num[maxn],n,m,c[],root[],left,right,tot,son[maxn][];
- int mmin(int x,int y)
- {
- if (x<y) return x;
- return y;
- }
- int mmax(int x,int y)
- {
- if (x>y) return x;
- return y;
- }
- void rotate(int x,int w)
- {
- int y=f[x];
- if (f[y]) if (y==son[f[y]][]) son[f[y]][]=x; else son[f[y]][]=x;
- f[x]=f[y];
- if (son[x][w]) f[son[x][w]]=y;
- son[y][-w]=son[x][w];
- f[y]=x;
- son[x][w]=y;
- sum[y]=sum[son[y][]]+sum[son[y][]]+num[y];
- }
- void splay(int r,int x,int w)
- {
- int y;
- while (f[x]!=w)
- {
- y=f[x];
- if (f[y]==w) if (x==son[y][]) rotate(x,); else rotate(x,);
- else if (y==son[f[y]][]) if (x==son[y][]) {rotate(y,); rotate(x,); } else {rotate(x,); rotate(x,);}
- else if (x==son[y][]) {rotate(x,); rotate(x,);} else {rotate(y,); rotate(x,);}
- }
- sum[x]=sum[son[x][]]+sum[son[x][]]+num[x];
- if (w==) root[r]=x;
- }
- void insert(int r,int value)
- {
- int x=root[r];
- if (x==)
- {
- a[++tot]=value;
- num[tot]=sum[tot]=;
- root[r]=tot;
- return;
- }
- while (a[x]!=value)
- if (a[x]>value) {if (son[x][]) x=son[x][]; else break;}
- else {if (son[x][]) x=son[x][]; else break;}
- if (a[x]==value) {num[x]++; splay(r,x,); return;}
- a[++tot]=value;
- f[tot]=x;
- num[tot]=sum[tot]=;
- if (value<a[x]) son[x][]=tot; else son[x][]=tot;
- splay(r,tot,);
- }
- void del(int r,int value)
- {
- int x=root[r];
- while (a[x]!=value)
- if (a[x]>value) {if (son[x][]) x=son[x][]; else break;}
- else {if (son[x][]) x=son[x][]; else break;}
- splay(r,x,);
- if (num[x]>) {num[x]--; return;}
- if (!son[x][]) {root[r]=son[x][]; f[son[x][]]=; return;}
- if (!son[x][]) {root[r]=son[x][]; f[son[x][]]=; return;}
- int y=son[x][];
- while (son[y][]) y=son[y][];
- splay(r,y,x);
- son[y][]=son[x][];
- f[y]=;//一定记得f[y]=0
- f[son[y][]]=y;
- sum[y]=sum[son[y][]]+sum[son[y][]]+num[y];
- root[r]=y;
- }
- void build(int now,int l,int r)
- {
- //printf("now=%d l=%d r=%d\n",now,l,r);
- int i;
- for (i=l;i<=r;i++) insert(now,c[i]);//对每个线段树区间都建立一棵平衡树
- //print(now);
- if (l==r) return;
- int mid=(l+r)>>;
- i=now<<;
- build(i,l,mid);
- build(i+,mid+,r);
- }
- int find(int r,int value)
- {
- int x=root[r];
- while (a[x]!=value)
- if (a[x]>value) {if (son[x][]) x=son[x][]; else break;}
- else {if (son[x][]) x=son[x][]; else break;}
- splay(r,x,);
- return x;
- }
- int rank(int r,int k)
- {
- int x=find(r,k);
- if (a[x]>=k) return (sum[son[x][]]);
- return (sum[son[x][]]+num[x]);
- }
- int getrank(int now,int l,int r,int k)
- {
- if (l>=left&&r<=right) return (rank(now,k));
- int mid=(l+r)>>,w=now<<,ret=;
- if (left<=mid) ret=getrank(w,l,mid,k);
- if (right>mid) ret+=getrank(w+,mid+,r,k);
- return ret;
- }
- void change(int now,int l,int r,int pos,int value)
- {
- del(now,c[pos]);
- insert(now,value);
- if (l==r) return;
- int mid=(l+r)>>,w=now<<;
- if (pos<=mid) change(w,l,mid,pos,value); else change(w+,mid+,r,pos,value);
- }
- int ppre(int r,int value)
- {
- int x=root[r];
- while (a[x]!=value)
- if (a[x]>value) {if (son[x][]) x=son[x][]; else break;}
- else {if (son[x][]) x=son[x][]; else break;}
- if (a[x]==value)
- {
- splay(r,x,);
- if (!son[x][]) return ;
- x=son[x][];
- while (son[x][]) x=son[x][];
- return x;
- }
- while (f[x]&&a[x]>value) x=f[x];
- if (a[x]<value) return x;
- return ;
- }
- int ssucc(int r,int value)
- {
- int x=root[r];
- while (a[x]!=value)
- if (a[x]>value) {if (son[x][]) x=son[x][]; else break;}
- else {if (son[x][]) x=son[x][]; else break;}
- if (a[x]==value)
- {
- splay(r,x,);
- if (!son[x][]) return ;
- x=son[x][];
- while (son[x][]) x=son[x][];
- return x;
- }
- while (f[x]&&a[x]<value) x=f[x];
- if (a[x]>value) return x;
- return ;
- }
- int pre(int now,int l,int r,int value)
- {
- if (l>=left&&r<=right)
- {
- int x=ppre(now,value);
- if (a[x]<value) return a[x];
- x=find(now,);
- return -;
- }
- int mid=(l+r)>>,w=now<<,ret=-;
- if (left<=mid) ret=mmax(ret,pre(w,l,mid,value));
- if (right>mid) ret=mmax(ret,pre(w+,mid+,r,value));
- return ret;
- }
- int succ(int now,int l,int r,int value)
- {
- if (l>=left&&r<=right)
- {
- int x=ssucc(now,value);
- if (a[x]>value) return (a[x]);
- return inf;
- }
- int mid=(l+r)>>,w=now<<,ret=inf;
- if (left<=mid) ret=mmin(ret,succ(w,l,mid,value));
- if (right>mid) ret=mmin(ret,succ(w+,mid+,r,value));
- return ret;
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- int i,kind,x,y,z,min,max,ans,mid;
- for (i=;i<=n;i++) scanf("%d",&c[i]);
- tot=;
- a[]=-,a[]=;
- build(,,n);
- for (i=;i<=m;i++)
- {
- scanf("%d%d%d",&kind,&x,&y);
- if (kind!=) scanf("%d",&z);
- if (kind==)
- {
- left=x,right=y;
- printf("%d\n",getrank(,,n,z)+);
- continue;
- }
- if (kind==)
- {
- left=x,right=y;
- min=,max=;
- while (min<=max)
- {
- mid=(min+max)>>;
- if (getrank(,,n,mid)<z) {ans=mid; min=mid+;} else max=mid-;
- }
- printf("%d\n",ans);
- continue;
- }
- if (kind==)
- {
- change(,,n,x,y);
- c[x]=y;
- continue;
- }
- if (kind==)
- {
- left=x,right=y;
- printf("%d\n",pre(,,n,z));
- continue;
- }
- left=x,right=y;
- printf("%d\n",succ(,,n,z));
- }
- return ;
- }
然后是我的AC code:
/*调了好久,一部分原因是做这道题时学长讲新课,没时间调这道题。今天终于调出来了,然后把三篇平衡树的博客一起写出来了。这个题的题解在上面sth神犇的模板里有,主要是那个二分k值是本题的特色。我这道题一直WA的原因是快速读入没有判断负号(╯‵□′)╯︵┻━┻*/
- #include<cstdio>
- #include<cctype>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- inline const int max(const int &a,const int &b){return a>b?a:b;}
- inline const int min(const int &a,const int &b){return a<b?a:b;}
- struct node{
- node();
- node *fa,*ch[];
- int sum,d;
- short pl(){return this==fa->ch[];}
- void count(){sum=ch[]->sum+ch[]->sum+;}
- }*null;
- node::node(){fa=ch[]=ch[]=null;sum=;}
- int data[],N,M;
- node *ROOT[<<];
- int getint(){char c;int fh=;while(!isdigit(c=getchar()))if (c=='-')fh=-;int a=c-'';while(isdigit(c=getchar()))a=a*+c-'';return a*fh;}
- namespace Splay{
- void Builda(){
- null=new node;
- *null=node();
- }
- void rotate(node *k,int rt){
- node *r=k->fa;
- if (k==null||r==null) return;
- int x=k->pl()^;
- r->ch[x^]=k->ch[x];
- r->ch[x^]->fa=r;
- if (r->fa==null) ROOT[rt]=k;
- else r->fa->ch[r->pl()]=k;
- k->fa=r->fa; r->fa=k;
- k->ch[x]=r;
- r->count(); k->count();
- }
- void splay(int rt,node *r,node *tar=null){
- for (;r->fa!=tar;rotate(r,rt))
- if (r->fa->fa!=tar)rotate(r->pl()==r->fa->pl()?r->fa:r,rt);
- }
- void insert(int rt,int x){
- node *r=ROOT[rt];
- if (ROOT[rt]==null){
- ROOT[rt]=new node;
- *ROOT[rt]=node();
- ROOT[rt]->d=x;
- ROOT[rt]->count();
- return;
- }
- while (){
- int c;
- if (x<r->d) c=;
- else c=;
- if (r->ch[c]==null){
- r->ch[c]=new node;
- *r->ch[c]=node();
- r->ch[c]->fa=r;
- r->ch[c]->d=x;
- splay(rt,r->ch[c]);
- return;
- }else r=r->ch[c];
- }
- }
- void build(int rt,int l,int r){
- ROOT[rt]=null;
- for(int i=l;i<=r;++i) insert(rt,data[i]);
- }
- node *kth(int rt,int x){
- node *r=ROOT[rt];
- while (r!=null){
- if (x<r->d) r=r->ch[];
- else if (x>r->d) r=r->ch[];
- else return r;
- }return r;
- }
- node *rightdown(node *r){
- while (r->ch[]!=null){
- r=r->ch[];
- }return r;
- }
- void deletee(int rt,int x){
- node *r=kth(rt,x);
- splay(rt,r);
- if ((r->ch[]==null)&&(r->ch[]==null)){
- ROOT[rt]=null;
- delete r;
- }else if (r->ch[]==null){
- r->ch[]->fa=null;
- ROOT[rt]=r->ch[];
- delete r;
- }else if (r->ch[]==null){
- r->ch[]->fa=null;
- ROOT[rt]=r->ch[];
- delete r;
- }else{
- splay(rt,rightdown(r->ch[]),ROOT[rt]);
- r->ch[]->ch[]=r->ch[];
- r->ch[]->fa=r->ch[];
- r->ch[]->fa=null;
- r->ch[]->count();
- ROOT[rt]=r->ch[];
- delete r;
- }
- }
- int predd(node *r,int x){
- if (r==null) return -;
- if (x<=r->d) return predd(r->ch[],x);
- else return max(r->d,predd(r->ch[],x));
- }
- int pross(node *r,int x){
- if (r==null) return 1E8+;
- if (x>=r->d) return pross(r->ch[],x);
- else return min(r->d,pross(r->ch[],x));
- }
- node *get1(node *r,int x){
- if (r==null) return null;
- if (x<r->d) return get1(r->ch[],x);
- if (x>r->d) return get1(r->ch[],x);
- node *rr=get1(r->ch[],x);
- if (rr!=null) return rr; else return r;
- }
- int quee1(int rt,int x){
- node *r=get1(ROOT[rt],x);
- if (r!=null){
- splay(rt,r);
- return r->ch[]->sum;
- }else{
- insert(rt,x);
- int anss=ROOT[rt]->ch[]->sum;
- deletee(rt,x);
- return anss;
- }
- }
- int quee2(int rt,int x){
- node *r=get1(ROOT[rt],x);
- if (r!=null){
- splay(rt,r);
- return r->ch[]->sum;
- }else{
- insert(rt,x);
- int anss=ROOT[rt]->ch[]->sum;
- deletee(rt,x);
- return anss;
- }
- }
- }
- void buildtree(int l,int r,int rt){
- Splay::build(rt,l,r);
- if (l==r) {Splay::build(rt,l,r);return;}
- int mid=(l+r)>>;
- buildtree(l,mid,rt<<);
- buildtree(mid+,r,rt<<|);
- }
- int que1(int L,int R,int k,int l,int r,int rt){
- if ((L<=l)&&(r<=R)) return Splay::quee1(rt,k);
- int mid=(l+r)>>,s=;
- if (L<=mid) s+=que1(L,R,k,l,mid,rt<<);
- if (R>mid) s+=que1(L,R,k,mid+,r,rt<<|);
- return s;
- }
- int que2(int L,int R,int k,int l,int r,int rt){
- if ((L<=l)&&(r<=R)) return Splay::quee2(rt,k);
- int mid=(l+r)>>,s=;
- if (L<=mid) s+=que2(L,R,k,l,mid,rt<<);
- if (R>mid) s+=que2(L,R,k,mid+,r,rt<<|);
- return s;
- }
- void que3(int pos,int k,int l,int r,int rt){
- if ((l<=pos)&&(pos<=r)){
- Splay::deletee(rt,data[pos]);
- Splay::insert(rt,k);
- }if (l==r) return; int mid=(l+r)>>;
- if (pos<=mid) que3(pos,k,l,mid,rt<<);
- if (pos>mid) que3(pos,k,mid+,r,rt<<|);
- }
- int que4(int L,int R,int k,int l,int r,int rt){
- if ((L<=l)&&(r<=R)) return Splay::predd(ROOT[rt],k);
- int mid=(l+r)>>,s=-;
- if (L<=mid) s=que4(L,R,k,l,mid,rt<<);
- if (R>mid) s=max(s,que4(L,R,k,mid+,r,rt<<|));
- return s;
- }
- int que5(int L,int R,int k,int l,int r,int rt){
- if ((L<=l)&&(r<=R)) return Splay::pross(ROOT[rt],k);
- int mid=(l+r)>>,s=1E8+;
- if (L<=mid) s=que5(L,R,k,l,mid,rt<<);
- if (R>mid) s=min(s,que5(L,R,k,mid+,r,rt<<|));
- return s;
- }
- int main(){
- Splay::Builda();
- N=getint();M=getint();
- for(int i=;i<=N;++i)data[i]=getint();
- buildtree(,N,);
- while (M){M--;
- int x=getint();
- switch (x){
- int l,r,k,pos,ans,left,right,mid;
- case :
- l=getint(),r=getint(),k=getint();
- printf("%d\n",que1(l,r,k,,N,)+);
- break;
- case :
- l=getint(),r=getint(),k=getint(),left=,right=1E8;
- while (left<=right){
- mid=(left+right)>>;
- if (que2(l,r,mid,,N,)+<=k) ans=mid,left=mid+;
- else right=mid-;
- }printf("%d\n",ans);
- break;
- case :
- pos=getint(),k=getint();
- que3(pos,k,,N,);
- data[pos]=k;
- break;
- case :
- l=getint(),r=getint(),k=getint();
- printf("%d\n",que4(l,r,k,,N,));
- break;
- case :
- l=getint(),r=getint(),k=getint();
- printf("%d\n",que5(l,r,k,,N,));
- break;
- }
- }
- return ;
- }
这样就可以了
【BZOJ 3196】二逼平衡树 线段树套splay 模板题的更多相关文章
- bzoj 3196二逼平衡树 线段树套平衡树
比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细) //By BLADEVIL ...
- BZOJ3196:二逼平衡树(线段树套Splay)
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 ...
- BZOJ - 3196 Tyvj 1730 二逼平衡树 (线段树套treap)
题目链接 区间线段树套treap,空间复杂度$O(nlogn)$,时间复杂度除了查询区间k大是$O(log^3n)$以外都是$O(log^2n)$的. (据说线段树套线段树.树状数组套线段树也能过?) ...
- BZOJ3196二逼平衡树——线段树套平衡树(treap)
此为平衡树系列最后一道:二逼平衡树您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询 ...
- bzoj3196 二逼平衡树——线段树套平衡树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 人生中第一棵树套树! 写了一个晚上,成功卡时 9000ms+ 过了! 很要注意数组的大 ...
- [bzoj3196]Tyvj 1730 二逼平衡树——线段树套平衡树
题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查 ...
- 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义 ...
- [bzoj3196][Tyvj 1730][二逼平衡树] (线段树套treap)
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在 ...
随机推荐
- 如何解决inline-block元素的空白间距
早上在博客中有人提了这样一个问题:“li元素inline-block横向排列,出现了未知间隙”,我相信大家在写页面的时候都遇到过这样的情况吧. 我一般遇到这情况都会把li浮动起来,这样就没有间隙.但是 ...
- 谈谈自己对java的学习看法
从明天起,开始整理java的基础知识,进行巩固学习. 今天呢,谈谈自己的一点想法.很多人不知道java怎么学,学什么,有的是直接在网上找一些视频来看,不懂的地方到处跑群里问,结果效果并不是太好,怎么办 ...
- AC日记——无线网络发射器选址 洛谷 P2038
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...
- 开坑,Unix环境高级编程,转行之路又得缓缓了
不要问我基础,我用了近6年的Linux系统,最早的版本可以追溯到Ubuntu 8.04,常用的命令 VIM基本上是没压力,遇到问题google 配置环境变量 网络环境也不在话下, C语法基本熟练,过去 ...
- f2fs源码解析(五) node管理结构梳理
node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...
- 第二章 时间控件(DateTime Picker)
这家伙太懒了,碰到问题才写博文,嘿嘿. 好了进入正题,二话不说,先放地址: 中文:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm ...
- 【WPF】WPF通过RelativeSource绑定父控件的属性
1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...
- 【Asp.Net】Asp.Net CommandName作用
数据绑定控件的模板中 CommandName 属性以下属性值会触发特定的事件: Cancel(取消) Delete(删除) Select(选择) Edit(编辑) Insert(插入) Update( ...
- C# 无边框窗体的最小化问题
WinForm在窗体风格设置成None时无法最小化的问题.添加以下代码即可实现最小化: protected override CreateParams CreateParams { get { con ...
- Oracle异常处理,动态游标
小例子,方便以后查阅. 包头需要声明: type C_CURSOR is ref cursor; procedure visitcount(in_date number, out_code out ...