【bzoj3223】文艺平衡树

#include<bits/stdc++.h>
#define N 300005
#define rat 4
using namespace std;
struct Node{
    int size,val,rev;
    Node *lc,*rc;
    Node(int s,int v,Node *a,Node *b):size(s),val(v),lc(a),rc(b),rev(0){}
    Node(){}
}*nul,*rt,*st[N],t[N];
int n,m,l,r,cnt,a[N];
inline void pushup(Node *o){
    if(o->lc->size)o->size=o->lc->size+o->rc->size;
}
inline void pushdown(Node *o){
    if(o->rev){
        o->lc->rev^=1;o->rc->rev^=1;o->rev^=1;
        swap(o->lc,o->rc);
    }
}
inline Node* newnode(int s,int v,Node *a,Node *b){
    return &(*st[cnt++]=Node(s,v,a,b));
}
Node * build(int l,int r){
    if(l==r)return newnode(1,a[l],nul,nul);
    int mid=(l+r)>>1;
    return newnode(r-l+1,a[r],build(l,mid),build(mid+1,r));
}
Node* merge(Node *a,Node *b){
    pushdown(a);pushdown(b);
    if(a->size>b->size*rat)return a->rc=merge(a->rc,b),pushup(a),a;
    if(b->size>a->size*rat)return b->lc=merge(a,b->lc),pushup(b),b;
    return newnode(a->size+b->size,b->val,a,b);
}
void split(int x,Node *o){
    pushdown(o);
    if(x>o->lc->size){
        split(x-o->lc->size,o->rc);o->lc=merge(o->lc,o->rc->lc);
        st[--cnt]=o->rc;o->rc=o->rc->rc;
    }
    else if(x<o->lc->size){
        split(x,o->lc);o->rc=merge(o->lc->rc,o->rc);
        st[--cnt]=o->lc;o->lc=o->lc->lc;
    }
}
void dfs(Node *o){
    pushdown(o);
    if(o->size==1){if(o->val)printf("%d ",o->val);}
    else{dfs(o->lc);dfs(o->rc);}
}
inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}
int main(){
    n=read();m=read();
    for(int i=1;i<=n;i++)a[i]=i;
    for(int i=0;i<=N;i++)st[i]=&t[i];
    nul=new Node(0,0,0,0);
    rt=build(0,n+1);
    while(m--){
        l=read(),r=read();l++;r++;
        split(r,rt);split(l-1,rt->lc);
        rt->lc->rc->rev^=1;
    }
    dfs(rt);
    return 0;
}

 

zzq wc-ctsc-apio-NOI Au;yql精通多项式;zyz精通女装;由乃精通数据结构;孔老师是毒奶大师;我没有学上:我们都有光明的前途。
posted @ 2017-06-11 08:34  zcysky  阅读(271)  评论(0编辑  收藏  举报