splay literary balanced tree

Template title transfer

The range is simple flip operation, marking just fine. Code is easy to understand

#include<bits/stdc++.h>
#define INF 2100000001
#define N 100003
using namespace std;
int read()
{
    int x=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10 + S- ' 0 ' ; S = getchar ();}
     return X * F; 
} 
int the root = 0 , ndnum = 0 ;
 int FA [N], CH [N] [ . 4 ], Key [N], Tag [N], SIZ [N], Data [N];
 int build ( int F, int L, int R & lt) // and the segment tree is build there are some differences 
{
     IF (L> R & lt) return  0 ; // is the null node 
    int MID = (L + R & lt) >> . 1 ;
     int now ++ = ndnum; 
    Key [now]=data[mid],fa[now]=f,tag[now]=0;//tag标记 
    ch[now][0]=build(now,l,mid-1);
    ch[now][1]=build(now,mid+1,r);
    siz[now]=siz[ch[now][0]]+siz[ch[now][1]]+1;
    return now;
}
int get(int x)
{
    return ch[fa[x]][1]==x;
}
void update(int x)
{
    siz[x]=siz[ch[x][1]]+siz[ch[x][0]]+1;
}
void pushdown(int x)
{
    if(x&&tag[x])
    {
        tag[ch[x][0]]^=1;
        tag[ch[x][1]]^=1;
        swap(ch[x][1],ch[x][0]);
        tag[x]=0;
    }
}
void rotate(int x)
{
    int old=fa[x],oldf=fa[old];
    int which=get(x);
    ch[old][which]=ch[x][which^1];
    fa[ch[old][which]]=old;
    fa[old]=x;ch[x][which^1]=old;
    fa[x]=oldf;
    if(oldf) ch[oldf][ch[oldf][1]==old]=x;
    update(old);update(x);
}
void splay(int x,int tar)
{
    for(int f;(f=fa[x])!=tar;rotate(x))
      if(fa[fa[x]]!=tar) rotate((get(x)==get(f))?f:x);
    if(!tar) root=x;
}
int rank(int x)
{
    int now=root;
    while(1)
    {
        pushdown(now);
        if(x<=siz[ch[now][0]]) now=ch[now][0];
        else
        {
            x-=siz[ch[now][0]]+1;
            if(!x)return now;
            now=ch[now][1];
        }
    }
}
void turn(int l,int r)
{
    int x=rank(l);
    int y=rank(r+2);
    
    splay(x,0);splay(y,x);
    pushdown(root);
    tag[ch[ch[root][1]][0]]^=1;
}
void dfs(int x)
{
    pushdown(x);
    if(ch[x][0])dfs(ch[x][0]);
    if(key[x]!=-INF&&key[x]!=INF) printf("%d ",key[x]);
    if(ch[x][1]) DFS (CH [X] [ . 1 ]); 
} 
int main () 
{ 
    int n-= Read (), m = Read ();
     for ( int I = . 1 ; I <= n-; I ++) Data [ + I . 1 ] = I; 
    Data [ . 1 ] = - INF; Data [n-+ 2 ] = INF; // reverses at the interval [l ~ r], we can consider the use of the properties Splay,
     // the l-1 flip to the root node, then flip to children r + 1 for convenience the sub-root node, then the node number n is assigned to nodes plus two -INF and INF before node No. 1
     // as a virtual point, both to meet the binary search the nature of the tree, but also allows us not inverted GG ~ 1 when n- 
    the root Build = ( 0 , 1 , + n- 2 );
     for ( int I = 1;i<=m;++i)
    {
        int x=read(),y=read();
        turn(x,y);
    }
    dfs(root);
}
View Code

By the way the mouth is about the rollover next pass mark, if the mark is also directed to the other, to consider the impact (see specific topics, for example, also keep the maximum prefix and suffix maximum, then not only about his son to be exchanged, and the largest prefix The maximum extension should be exchanged).

Then we will find that this question is the corresponding index value, but there are some questions subscript is not a range, like a key deposit, but the value of sentinel node may have to change it (depending on the topic).

 

Guess you like

Origin www.cnblogs.com/yyys-/p/11240425.html