Arts balanced tree (Splay)

The meaning of problems

A given sequence, m flip operations interval [l, r], such as 1,5,4,2 become 2,4,5,1

Seeking final sequence

n,m100000

answer

Weights ordinary splay maintenance if the maintenance sequence, then you can maintain sequence subscript that splay the preorder is the original sequence.

Extraction period interval [l, r] to put a spin root l-1, r + 1 is screwed onto the right son of the root, then this section in two sub-r + 1 of the left portion.

After extracted some sections of the structure is a tree, then flipped it marked section marked behalf of his son is about to be exchanged at the root of the tree.

In the next pass mark can find in, because all operations go through this step.

 

#include<bits/stdc++.h>
using namespace std;

const int maxn=100005;
const int oo=100000000;
int n,m,root,num,a[maxn];
struct point{
    int size,fa,v,tag,s[2];
}tr[maxn];

template<class T>inline void read(T &x){
    x=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
}

int build(int l,int r,int f){
    if(l>r) return 0;
    int now=++num,mid=(l+r)>>1;
    tr[now].fa=f;
    tr[now].v=a[mid];
    tr[now].size++;
    tr[now].s[0]=build(l,mid-1,now);
    tr[now].s[1]=build(mid+1,r,now);
    tr[now].size+=tr[tr[now].s[1]].size+tr[tr[now].s[0]].size;
    return now;
}


void pushdown(int x){
    if(!tr[x].tag) return ;
    tr[tr[x].s[0]].tag^=1;
    tr[tr[x].s[1]].tag^=1;
    swap(tr[x].s[0],tr[x].s[1]);
    tr[x].tag=0;
}

int find(int x){
    int now=root;
    while(1){
        pushdown(now);
        if(x<=tr[tr[now].s[0]].size) {now=tr[now].s[0]; continue;}
        x=x-(tr[tr[now].s[0]].size+1);
        if(!x) return now;
        now=tr[now].s[1];
    }
}

int indenti(int x){ return tr[tr[x].fa].s[1]==x;}

void connect(int x,int y,int d){
    tr[x].fa=y;
    tr[y].s[d]=x;
}

void update(int x){
    int ls=tr[x].s[0],rs=tr[x].s[1];
    tr[x].size=tr[ls].size+tr[rs].size+1;
}

void rotate(int x){
    int f=tr[x].fa,ff=tr[f].fa;
    int d1=indenti(x);
    int d2=indenti(f);
    int cs=tr[x].s[d1^1];
    connect(x,ff,d2);
    connect(f,x,d1^1);
    connect(cs,f,d1);
    update(f);
    update(x);
}

void splay(int x,int go){
    if(go==root) root=x;
    go=tr[go].fa;
    while(tr[x].fa!=go){
        int f=tr[x].fa;
        if(tr[f].fa==go) rotate(x);
        else if(indenti(x)==indenti(f)) {rotate(f);rotate(x);}
        else {rotate(x);rotate(x);}
    }
}

void cx(int x,intY) {
     int L = Find (X- . 1 ), R & lt Find = (Y + . 1 ); // find the interval [x, y] predecessor and successor 
    The splay (L, the root); 
    The splay (R & lt, TR [L]. S [ . 1 ]);
     int POS = TR [the root] .s [ . 1 ]; 
    POS = TR [POS] .s [ 0 ]; // find [x, y] whole pieces root subtree 
    tr [pos]. = ^ Tag . 1 ; 
} 

void Nice ( int X) { 
    pushdown (X); 
    IF (TR [X] .s [ 0 ]) Nice (TR [X] .s [ 0 ]);
     IF (TR [X]. ! && V TR = OO [X] .v = -! OO) the printf ( " % D ",tr[x].v);
    if(tr[x].s[1]) nice(tr[x].s[1]);
}

int main(){
    read(n);read(m);
    a[1]=-oo;a[n+2]=oo;
    for(int i=1;i<=n;i++) a[i+1]=i;
    root=build(1,n+2,0);
    for(int i=1;i<=m;i++){
        int x,y;read(x);read(y);
        cx(x+1,y+1 ); // Add the -OO 
    } 
    Nice (the root); 
}
View Code

 

Guess you like

Origin www.cnblogs.com/sto324/p/11297573.html