[Knowledge Point] The splay of the balance tree

Source: Internet
Author: User

This blog post for the migration, written on July 18, 2015, does not represent my current views and views. Original address: http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html

1. Prefacethis thing really engaged me for a long time, of course, has not been to control it for a while, recently direct reference to the YML (@YMDragon) procedures, feeling is good, probably understand, is a bit of a logical relationship. Well, don't say much nonsense ...  2. ConceptThe full name of the balance tree is: Balanced binary search tree, the function is very powerful, the result is the code is very complex. First of all, we should all know what the binary search tree is, so what is the meaning of the preceding balance? According to the reading order {80,30,90,20,40,35,38}, we first construct a binary search tree,

Obviously, the tree is a bit deformed, and its root node is significantly too large for its left son. However, this is not only an uncomfortable question, but if we are going to do an inquiry or an operation on the tree, if the depth is too large, it will obviously affect the efficiency of time, and we can cite an extreme example--if we construct a chain, then the complexity of the time is conceivable. And the meaning of the balance appears, make this tree grow more like a tree! can improve efficiency when operating. There are many ways to write a balanced tree, such as: Splay,treap, red-black and AVL trees, and so on. Splay in comparison with the powerful, the code is large, today's focus on splay.   3, let ' s splaySplay's Chinese is stretching, it's not clear what it's about. There are three main types of operations in Splay, which are described below: 1.Zig Operation The target node x is the left (right) son of the root node, and the target node is rotated right (left) to the root node.

2.zig-zig Operation The target node x is not a child of the root node, and is the left (right) son of its parent node, whose parent is the left (right) son of its grandfather node, and the target node x and its parent node are successively right (left).

3.zig-zag Operation The target node x is not a child of the root node, and is the left (right) son of its parent node, whose parent node is the right (left) son of its grandfather node, then the target node x is left (left) and the parent of the node is the right (right) spin. In particular, rotation is the bottom-up recursive rotation until the target node is rotated to the root node.

4. Applicationthe balance tree has a common effect, which is the balance (nonsense) that was mentioned earlier. But today's topic is splay, before also said it is powerful, so strong where? At first glance, you may not even think of using splay. Now let's look at a divine question:

first of all, this is a NOI2005 of the original problem, Bzoj does not give the tree data range, but I want to simulate the time complexity and space complexity must be unbearable. I also thought of the chain, but feel a little trouble to achieve, then this problem to use is splay. Notice that in the six operations, all interval modification or interval query, we first set the current operation corresponding interval for [x, Y] (insert is an exception, can be considered y=x+1), first we rotate X to the root, and then in the rotation of Y to the root, think carefully, [X, Y] is currently in the tree distribution? Must be in the same subtrees tree. So this is good to deal with, the insertion can be similar to in this subtrees tree structure (equivalent to the beginning of the input link), the deletion of a China (the specific operation of emptying: The weight value is recorded as 0, as a marker; there may be a problem if too many deletions can cause too much wasted space resources, Here we can consider to open a queue, so that in the future when the new node can be used to directly use the space has been deleted, modify the weights and flips the operation is a bit similar to the line tree; ask for, in advance for each subtrees tree maintenance can be.  Code:----------------------------------------------------------------------------------------------------#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> #define MAXN 500005
#define INF 1<<30
using namespace std; struct Node
{
int SON[2],L,R,M,K,T1,T2,S,SUM,FA;
};
 Node TREE[MAXN];int M,N,ROOT,A[MAXN];
Char now[10];
Queue Q; void Plus1 (int now,int val)
{
if (!now) return;
TREE[NOW].K=VAL,TREE[NOW].T1=1,TREE[NOW].SUM=TREE[NOW].K*TREE[NOW].S;
if (tree[now].k>0) tree[now].l=tree[now].r=tree[now].m=tree[now].sum;
else TREE[NOW].L=TREE[NOW].R=TREE[NOW].M=TREE[NOW].K;
} void Plus2 (int now)
{
if (!now) return;
Swap (tree[now].son[0],tree[now].son[1]), tree[now].t2^=1;
Swap (TREE[NOW].L,TREE[NOW].R);
} void push (int now)
{
if (!now) return;
if (TREE[NOW].T1) Plus1 (TREE[NOW].SON[0],TREE[NOW].K), Plus1 (TREE[NOW].SON[1],TREE[NOW].K), tree[now].t1=0;
if (tree[now].t2) Plus2 (tree[now].son[0]), Plus2 (Tree[now].son[1]), tree[now].t2=0;
}&NBSP; void update (int now)
{
         if (!now) return;
        push (now);
        int lson=tree[now].son[0],rson=tree[now].son[1];
        tree[now].s=tree[lson].s+tree[rson].s+1;
        tree[now].sum=tree[lson].sum+tree[rson].sum+tree[now].k;
        tree[now].l=max (tree[lson].l,tree[lson].sum+tree[now].k+ Max (tree[rson].l,0));
        tree[now].r=max (tree[rson].r,tree[rson].sum+tree[now].k+ Max (tree[lson].r,0));  
        tree[now].m=max (Max (Tree[lson). M,TREE[RSON].M), Tree[now].k+max (tree[lson].r,0) +max (tree[rson].l,0));
} &NBSP; void rotate (int now)
{
         int fa=tree[now].fa,gfa=tree[fa].fa,q= (tree[fa].son[1]==now), son=tree[now].son[q^ 1];
        if (GFA) TREE[NOW].FA=GFA,TREE[GFA].SON[TREE[GFA].SON[1]==FA ]=now;
        else Tree[now].fa=0,root=now;
        tree[fa].son[q]=son; if (son) Tree[son].fa=fa;
        tree[fa].fa=now,tree[now].son[q^1]=fa;
        update (FA), update (now);
//  Let me translate this passage:
//  new variable: My father, my father's father, "I am my father's right son" judgment, my Left (right) son;
//  If I have a grandfather, then my father is my grandfather, My grandfather's son is me, left or right depends on my father is my grandfather's left or right son;
//  if I have no grandfather, then my father is not, I am your ancestor
//  My father's son is my son, if I have a son, Then my son's father is my father.
//  My father's father is me, my left (right) son is my father. } void splay (int now)
{
int k=0;
for (int x=now;x;x=tree[x].fa) {k++; a[k]=x;}
for (int i=k;i>=1;i--) push (A[i]);
while (TREE[NOW].FA) rotate (now);
} void build (int now,int l,int R)
{
int mid= (L+R) >>1;
if (L<mid) Tree[now].son[0]=q.front (), Q.pop (), Build (tree[now].son[0],l,mid-1), Tree[tree[now].son[0]].fa=now;
scanf ("%d", &TREE[NOW].K);
if (mid<r) Tree[now].son[1]=q.front (), Q.pop (), Build (Tree[now].son[1],mid+1,r), Tree[tree[now].son[1]].fa=now;
Update (now);
}        int rank (int q)
{
         int Now=root;
        while (now)
         {
                 Push (now);
                if (tree[ TREE[NOW].SON[0]].S+1==Q) return now;
                if (tree[ TREE[NOW].SON[0]].S>=Q) now=tree[now].son[0];
                else q-= TREE[TREE[NOW].SON[0]].S+1,NOW=TREE[NOW].SON[1];
        }
        return 0;
}  void Ins ()
{
int pos,tot,l,r,root; scanf ("%d%d", &pos,&tot);
L=rank (POS), R=rank (pos+1); Splay (L), splay (R);
Root=q.front (), Q.pop (), build (Root,1,tot);
if (l) tree[l].son[1]=root,tree[root].fa=l;
else tree[r].son[0]=root,tree[root].fa=r;
Update (L), update (R);
} void DFS (int now)
{
if (Tree[now].son[0]) DFS (tree[now].son[0]);
if (tree[now].son[1]) DFS (tree[now].son[1]);
Q.push (now), memset (tree+now,0,sizeof (Tree[now));
} void Del ()
{
int pos,tot,l,r; scanf ("%d%d", &pos,&tot);
L=rank (pos-1), R=rank (Pos+tot); Splay (L), splay (R);
if (l) DFS (tree[l].son[1]), tree[l].son[1]=0;
else DFS (tree[r].son[0]), tree[r].son[0]=0;
Update (L), update (R);
} void Makesame ()
{
int pos,tot,c,l,r; scanf ("%d%d%d", &pos,&tot,&c);
L=rank (pos-1), R=rank (Pos+tot); Splay (L), splay (R);
if (l) Plus1 (tree[l].son[1],c); else Plus1 (TREE[R].SON[0],C);
Update (L), update (R);
} void reverse ()
{
int pos,tot,l,r; scanf ("%d%d", &pos,&tot);
L=rank (pos-1), R=rank (Pos+tot); Splay (L), splay (R);
if (l) plus2 (tree[l].son[1]); else if (r) plus2 (Tree[r].son[0]); else Plus2 (root);
UpdatE (L), update (R);
} int Getsum ()
{
int pos,tot,l,r; scanf ("%d%d", &pos,&tot);
L=rank (pos-1), R=rank (Pos+tot); Splay (L), splay (R);
if (l) return tree[tree[l].son[1]].sum;else return tree[tree[r].son[0]].sum;
}        int Maxsum () {return tree[root].m;} void Init ()
{
Freopen ("1500.in", "R", stdin);
Freopen ("1500.out", "w", stdout);
scanf ("%d%d", &n,&m);
for (int i=1;i<=maxn;i++) Q.push (i);
Tree[0].l=tree[0].r=tree[0].m=-inf;
Root=q.front (), Q.pop (), build (Root,1,n);
} int main ()
{
Init ();
for (int i=1;i<=m;i++)
{
scanf ("%s", &now);
if (now[0]== ' I ') ins ();
else if (now[0]== ' D ') del ();
else if (now[2]== ' K ') makesame ();
else if (now[0]== ' R ') reverse ();
else if (now[0]== ' G ') printf ("%d\n", Getsum ());
else printf ("%d\n", Maxsum ());
}
return 0;
}---------------------------------------------------------------------------------------------------

[Knowledge Point] The splay of the balance tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.