On the application of left-leaning tree in Oi

Source: Internet
Author: User

Preface

can be stacked , a data structure that sounds very NB, in fact, more than the average heap of a combined operation.

When considering a general heap merge, when we merge we can only force the elements of one heap into another heap, so that the complexity will reach \ (\log (| a|) +\log (| b|) \), the limit data is clearly to be T-burst.

So we're thinking about using one of the most cost-effective stacks--the left-leaning tree --and its ideas and code are pretty simple and efficient.

Learn and reference from here

What is leftist Tree

The left side of the tree, as the name implies is like the left side of the tree, but such an abstract expression is certainly not in line with the people we learn oi the back of the board rigorous attitude.

We give some definitions:

    • Outer node: When and only if one of the Saozi right subtrees of this node is an empty node, note that the outer node is not a leaf node
    • Distance (or height?) ): For a node in the left-biased tree \ (x\), to its child nodes, the number of edges passing through its nearest outer node is called its distance, recorded as \ (dist_x\). In particular, the distance of the outer junction is 0, and the distance of the empty node (null) is \ ( -1\).

And then following this definition we can draw some properties:

    • Heap properties: For a non-leaf node in a left-biased tree, the nature of the heap should be satisfied. If it is a large heap, the value of any non-leaf node that is greater than the left and right child (if any) should be satisfied. i.e. \ (Val_x\ge val_{lson (x)},val_{rson (x)}\)
    • Left-biased: for any node in the left-leaning tree to satisfy its Zuozi (if any), the distance is greater than or equal to the right subtree (if any). i.e. \ (Dist_{lson (x)}\ge Dist_{rson (x)}\)
    • Transitivity? : Left-leaning tree any node of the son (if any) is a left- side Tree nonsense

By these properties, it is found that the left-leaning tree is a stacked ordered binary tree with left-biased properties .

At the same time, there is a last lemma that cannot be ignored: the distance of nodes in the left-leaning tree always satisfies \ (Dist_x=dist_{rsom (x)}+1\)

Proof: At the same time by the definition of distance and left-biased nature can be obtained.

The operator of leftist Tree

BB has so much in the nature of abstract things, it is time to talk about something really useful.

Merge

The basic operation of the heap (and also the necessary operations) is a quick completion of the merger, while merging is also the soul part of the left side of the tree, as long as the mastery of the merger, you can directly take the left side of the tree vigorously to the relevant .

The following assumes the Dagen case, we assume that the root of the two left-biased trees to be merged (note that a single node is also a left-biased tree ) is \ (x,y\)

We first maintain the nature of the heap so that \ (val_x>val_y\), that is, when the value is not satisfied \ (\operatorname{swap} (x, y) \)

Then we find that we are going to insert \ ( y\) into the subtree of \ (x\) , in other words, to merge \ (y\) and \ (x\) subtree.

So who does it merge with? Considering our hard-to-maintain left-biased nature, because the chain length on the right is less than or equal to the left, so in order to ensure that the complexity is definitely a direct and \ (dist\) Small Merge, so we merge \ (Rson (x) \) and \ (y\)

But after inserting the right sub-tree \ (dist\) may be greater than the left side of the tree , we must be not allowed, so we judge this situation, if there is a direct exchange Left and right subtree (note the direct exchange number can be).

So when does it end, of course, when \ (x\) or \ (y\) accesses than either is empty. So we can easily get the merged code (return the heap top number of the merged heap):

inline int merge(int x,int y){    if (!x||!y) return x+y; if (c[x]<c[y]) swap(x,y);    rc(x)=merge(rc(x),y); if (node[lc(x)].dis<node[rc(x)].dis) swap(lc(x),rc(x));    node[x].dis=node[rc(x)].dis+1; return x;}

Here is a picture of the Luogu found on the map can be more vivid to understand the following:

The complexity of the words similar to heuristic thought found is \ (O (\log) \) level.

Push

The essence of push is to merge a left-biased tree with only one node in a left-leaning tree, so it can be used directly with the merge.

Top

This directly returns the weight of the root node.

Pop

If you delete the root node, consider merging the two subtrees of the root node , and the code

inline void remove(int &x){    x=merge(lc(x),rc(x));}
Board Title: Luogu P3377 "template" Left biased tree (can be stacked)

This is when we maintain a small root pair, and then maintain a parent node information can be queried whether the two number in the same left-leaning tree.

CODE

#include <cstdio> #include <cctype>using namespace std;const int n=100005;struct leftist_tree{int Ch[2],val , DIS,FA;}    Node[n];int n,m,x,y,opt;inline char tc (void) {static char fl[100000],*a=fl,*b=fl; Return a==b&& (b= (A=FL) +fread (Fl,1,100000,stdin), a==b)? eof:*a++;}    inline void read (int &x) {x=0; char ch; while (!isdigit (CH=TC ())); while (x= (x<<3) + (x<<1) +ch-' 0 ', IsDigit (CH=TC ()));}    inline void Write (int x) {if (x>9) write (X/10); Putchar (x%10+ ' 0 ');} inline void swap (int &a,int &b) {int t=a; a=b; b=t;} inline int merge (int x,int y) {if (!x| |!    y) return x+y; if (node[x].val>node[y].val| | (Node[x].val==node[y].val&&x>y))    Swap (x, y); Node[x].ch[1]=merge (Node[x].ch[1],y);    Node[node[x].ch[1]].fa=x;    if (Node[node[x].ch[0]].dis<node[node[x].ch[1]].dis) swap (node[x].ch[0],node[x].ch[1]); node[x].dis=node[node[x].ch[1]].dis+1; return x;} inline void Remove (int x) {node[x].val=-1; Node[node[x].ch[0]].fa=node[Node[x].ch[1]].fa=0; Merge (Node[x].ch[0],node[x].ch[1]);} inline int getfather (int x) {while (NODE[X].FA) X=node[x].fa; return x;}    int main () {//freopen ("code.in", "R", stdin), Freopen ("Code.out", "w", stdout); register int i; Read (n); Read (m);    Node[0].dis=-1;    for (i=1;i<=n;++i) read (node[i].val);            while (m--) {read (opt); read (x); if (opt^2) {read (y); int fx=getfather (x), Fy=getfather (y);        if (~node[fx].val&&~node[fy].val&&fx!=fy) merge (Fx,fy); } else {int fx=getfather (x); ~node[fx].val)) puts ("-1");        else write (Node[fx].val), Putchar (' \ n '), remove (FX); }} return 0;}
Examples
    • Luogu P1552 [APIO2012] dispatch and pile up a good question. Consider each point as the leader to calculate the answer so the optional points are in this subtree, then consider the most people we are greedy to throw away the big, back when the merger of two heap can be. left-leaning tree processing.
    • Bzoj 1367: [baltic2004]sequence can pile up a good question, not a glance. Considering the case of not descending sequence , it is found that all the non- ascending sequences of the original sequence can be segmented, so it is necessary to take the median (the method of proving the absurdity). Then use the left-biased tree to count the median and support merging.
Postscript

Can and heap is between TG and provincial selection of the embarrassing content of the bar, in previous years Noip words are not seen.

Left-leaning tree although in the efficiency last in the pair heap, Fibonacci heap These immortal data structure, but its simple thought and code quantity are very conscience.

I'd like to know a little bit about it.

On the application of left-leaning tree in Oi

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.