bzoj3223 文艺平衡树 Splay & Treap
来源:互联网 发布:数据分析临界值 编辑:程序博客网 时间:2024/05/18 21:38
平衡树做区间翻转
节点维护siz域用于查找当前第k个位置上的数,翻转打标记。
对于splay,翻转[L,R]只要把第L-1个位置转到根,第R+1个位置转到root->R的位置,对root->R->L打翻转标记。
对于合并式treap,翻转[L,R]只要分裂出L到R位置的树打标记即可。
Code:
Splay:
#include<bits/stdc++.h>using namespace std;const int maxn = 100005;const int INF = 0x3f3f3f3f;struct node{ int key,siz; bool fg; node *ch[2],*fa;};node* root=NULL;int n,m;node *new_node(node *fa,int key){ node *t = new node; t->ch[0]=t->ch[1] = NULL; t->fa = fa; t->key = key; t->siz = 1; t->fg = 0; return t;}#define siz(t) (t == NULL ? 0 : t->siz)void pushup(node *t){ t->siz = siz(t->ch[0])+siz(t->ch[1]) + 1;}void pushdown(node *t){ if(!t->fg)return; t->fg=0; swap(t->ch[0],t->ch[1]); if(t->ch[0]) t->ch[0]->fg^=1; if(t->ch[1]) t->ch[1]->fg^=1;}node *build(node* fa,int L,int R){ if(L>R)return NULL; int mid=(L+R)>>1,key=mid-1; node *t=new_node(fa,key); t->ch[0]=build(t,L,mid-1); t->ch[1]=build(t,mid+1,R); pushup(t); return t;}void order(node *t){ if(t == NULL)return; pushdown(t); order(t->ch[0]); if(t->key >=1 && t->key <= n) printf("%d ",t->key); order(t->ch[1]);}void rotate(node *x,bool d){ node *y=x->fa; y->ch[!d]=x->ch[d]; if(x->ch[d]) x->ch[d]->fa=y; x->fa = y->fa; if(y->fa){ if(y == y->fa->ch[0]) y->fa->ch[0] = x; else y->fa->ch[1] = x; } x->ch[d] = y;y->fa=x; pushup(y);pushup(x);}void splay(node *x,node *tar){ while( x->fa != tar){ node *y=x->fa; if( x == y->ch[0]){ if(y->fa != tar && y == y->fa->ch[0]) rotate(y,1); rotate(x,1); }else{ if(y->fa != tar && y == y->fa->ch[1]) rotate(y,0); rotate(x,0); } } if( tar == NULL ) root=x;}node *find_kth(node *x,int kth){ pushdown(x); int siz=siz(x->ch[0]); if( siz+1 == kth ) return x; if( kth <= siz )return find_kth(x->ch[0],kth); return find_kth(x->ch[1],kth-siz-1);}void rev(int L,int R){ node *x=find_kth(root,L),*y=find_kth(root,R+2); splay(x,NULL); splay(y,root); y->ch[0]->fg^=1;}int main(){ freopen("input.txt","r",stdin); freopen("output1.txt","w",stdout); scanf("%d%d",&n,&m); root = build(NULL,1,n+2); for(int L,R,i=1;i<=m;i++){ scanf("%d%d",&L,&R); rev(L,R); } order(root);puts("");}
Treap:
#include<bits/stdc++.h>using namespace std;struct node{ int siz,key,pri; bool fg; node *L,*R;};node *root = NULL;int n,m;node* new_node(int key){ node *x = new node; x->L = x->R = NULL; x->key = key; x->pri = rand(); x->siz = 1; x->fg = 0; return x;}#define siz(x) (x == NULL ? 0 : x->siz)void pushup(node *x){ x->siz=siz(x->L)+siz(x->R)+1;}void pushdown(node *x){ if(!x->fg)return; x->fg=0; swap(x->L,x->R); if( x->L ) x->L->fg^=1; if( x->R ) x->R->fg^=1;}node *merge(node *x,node *y){ if( x == NULL ) return y; if( y == NULL ) return x; pushdown(x);pushdown(y); if( x->pri < y->pri ){ x->R = merge( x->R , y ); pushup(x); return x; }else{ y->L = merge( x , y->L ); pushup(y); return y; }}typedef pair<node*,node*>pii;pii split(node *x,int k){ if( x == NULL )return pii(NULL,NULL); pushdown(x); if( k <= siz(x->L) ){ pii tmp = split(x->L,k); x->L=tmp.second; pushup(x); return pii(tmp.first,x); } else { pii tmp = split(x->R,k-siz(x->L)-1); x->R=tmp.first; pushup(x); return pii(x,tmp.second); }}node *build(int L,int R){ if(L>R)return NULL; int mid=(L+R)>>1,key=mid-1; node *x = new_node(key); x->L = build(L,mid-1); x->R = build(mid+1,R); pushup(x); return x;}void order(node *x){ if( x == NULL ) return; pushdown(x); order(x->L); if(x->key >= 1 && x->key<=n)printf("%d ",x->key); order(x->R);}void rev(int L,int R){ pii tmp1=split(root,R+1); pii tmp2=split(tmp1.first,L); tmp2.second->fg^=1; root = NULL; root = merge(tmp2.first,tmp2.second); root = merge(root,tmp1.second);}int main(){ freopen("input.txt","r",stdin); freopen("output2.txt","w",stdout); srand(619); scanf("%d%d",&n,&m); root = build(1,n+2); for(int L,R,i=1;i<=m;i++){ scanf("%d%d",&L,&R); rev(L,R); } order(root);puts("");}
0 0
- bzoj3223 文艺平衡树 Splay & Treap
- bzoj3223 文艺平衡树 treap
- [BZOJ3223] 文艺平衡树 - splay
- [BZOJ3223]文艺平衡树 splay
- bzoj3223文艺平衡树splay
- 【bzoj3223】文艺平衡树【Splay】【呵呵】
- BZOJ3223 文艺平衡树(Splay)
- 【bzoj3223】Tyvj 1729 文艺平衡树 Splay
- 【BZOJ3223】文艺平衡树,Splay反转区间
- 【BZOJ3223】Tyvj 1729 文艺平衡树【Splay】
- splay 新模板 【bzoj3223】 文艺平衡树
- bzoj3223 Tyvj 1729 文艺平衡树 (splay)
- [BZOJ3223]文艺平衡树(平衡树splay)
- [BZOJ3223]文艺平衡树(平衡树splay)
- 【bzoj3223】文艺平衡树
- BZOJ3223文艺平衡树
- [bzoj3223]文艺平衡树
- bzoj3223 文艺平衡树
- 递归创建先序序列二叉树
- android intent 传递list或者对象
- 设计模式笔记:观察者模式
- Android M(6.0) 权限解决方案
- 欢迎使用CSDN-markdown编辑器
- bzoj3223 文艺平衡树 Splay & Treap
- nginx支持tcp代理mysql
- 自定义层叠布局StackLayout
- Java批量文件打包下载
- 以小米4手机为例换算px,dip,dpi等数值
- Adapter不调用getView()的可能
- 合理使用Android提供的Annotation来提高代码的质量
- MySql安装过程以及中文乱码解决办法
- Effective-OC 10.在既有类中使用关联对象存储自定义数据