【模板】Splay

文艺平衡树

注意到本题是按位置比较大小,val 值不满足二分查找性质,所以不存在 GetRank(Rt,val) 函数。

#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define ll long long using namespace std; const int mx=5e5+5; //Task : 文艺平衡树 struct node{ node *ch[2]; node *fa; int key; //记录当前位置的数值 int siz; int lazy; //懒标记,记录是否交换左右儿子 }tree[mx]; node *Root,*NIL,*ncnt; int n,m; void Init() { NIL=&tree[0]; NIL->key=-INF; ncnt=&tree[1]; Root=NIL->ch[0]=NIL->ch[1]=NIL->fa=NIL; } void PushUp(node *rt) { rt->siz=rt->ch[0]->siz+rt->ch[1]->siz+1; } void PushDown(node *rt) { if(rt==NIL) return; if(!rt->lazy) return; swap(rt->ch[0],rt->ch[1]); rt->ch[0]->lazy^=1; rt->ch[1]->lazy^=1; rt->lazy=0; } void Rotate(node *x) { PushDown(x->fa); PushDown(x); //核心代码 //旋转之前先下传标记 node *y=x->fa; int d=(x==y->ch[0]); x->fa=y->fa; if(y->fa!=NIL) y->fa->ch[y->fa->ch[1]==y]=x; y->ch[!d]=x->ch[d]; if(x->ch[d]!=NIL) x->ch[d]->fa=y; x->ch[d]=y; y->fa=x; if(y==Root) Root=x; PushUp(y); PushUp(x); } node *NewNode(int val) { node *p=ncnt++; p->ch[0]=p->ch[1]=p->fa=NIL; p->key=val; p->siz=1; return p; } void Splay(node *x,node *rt) { node *y,*z; while(x->fa!=rt) { y=x->fa; z=y->fa; if(z==rt) { Rotate(x); } else { if((y==z->ch[0])^(x==y->ch[0])) Rotate(x); else Rotate(y); Rotate(x); } } } node *Select(node *rt,int k) { if(rt==NIL) return NIL; PushDown(rt); if(k<=rt->ch[0]->siz) return Select(rt->ch[0],k); else if(k<=rt->ch[0]->siz+1) return rt; else return Select(rt->ch[1],k-rt->ch[0]->siz-1); } void Build(node *&rt,node *fa,int l,int r) { if(l>r) return; int mid=(l+r)>>1; rt=NewNode(mid); rt->fa=fa; Build(rt->ch[0],rt,l,mid-1); Build(rt->ch[1],rt,mid+1,r); PushUp(rt); } void Output(node *rt) { if(rt==NIL) return; PushDown(rt); Output(rt->ch[0]); if(rt->key>=1&&rt->key<=n) printf("%d ",rt->key); Output(rt->ch[1]); } void Reverse(int l,int r) { //1. l-1 提到根节点 node *x=Select(Root,l),*y=Select(Root,r+2); // printf("YES"); Splay(x,NIL),Splay(y,Root); Root->ch[1]->ch[0]->lazy^=1; } int main() { scanf("%d%d",&n,&m); Init(); Build(Root,NIL,0,n+1); while(m--) { int l,r; scanf("%d%d",&l,&r); Reverse(l,r); } Output(Root); }

__EOF__

本文作者仰望星空的蚂蚁
本文链接https://www.cnblogs.com/cqbzly/p/17530317.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   仰望星空的蚂蚁  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示