bzoj3223 文艺平衡树 Splay & Treap

平衡树做区间翻转

节点维护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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值