The data structure of the reading notes of the ACM/ICPC algorithm training course (line-Tree detailed)

Source: Internet
Author: User

Still continues the first reading note, this is based on the "ACM/ICPC Algorithm training Tutorial" on the line tree of the summary and modification (this book in the segment tree here error very much), but overall this book about the specific algorithm of the explanation and case are good.

Segment Tree Introduction    This is a binary search tree, similar to the interval tree, is a kind of tree-shaped data structure describing line segments, is also a acmer must learn a data structure, mainly used to query the processing and storage of a part of the query, The optimization of time is also more obvious, and the time complexity of optimization is O (logn). In addition, the segment tree can be extended to point tree ,zwk line segment Tree and so on, similar to the tree-like array and so on.

For example: To add the array s[] from the elements on the [i,j] segment to B, then we usually need to traverse each element (S[i],s[i+1]...s[j]) and +b, at which point the operand is (j-i+1), but if we are in some cases only concerned with the sum in [I,j] segment, At this point we simply +b* (j-i+1) on the basis of sum sum in the [I,j] segment, so that the operand needs only one time.

Furthermore, if you want to know the sum in the [I,j] segment and directly output the sums previously stored, this is much better than traversing (j-i+1) elements each time you query, so the reference tree structure can introduce a structure that represents the data on a line segment.

Using the array simulation tree , you can visually express the segment tree as the right image:

  Specific implementation and corresponding improvements code:

  Defined

The definition of each node can be temporary as follows:

struct node{    int  L, R;     int value; }TREE[MAXN];

The above is a straightforward representation, but for segment trees that need to update values frequently , this definition makes time optimization of the segment tree an advantage.

Because if each element in each [I,j] segment is +b, as a piece of data, we can +b* (j-i+1), but what about the data on the sub-tree of this section, it's a stupid thing to go all the way through and update the data in all the sub-nodes. This makes the line tree less efficient.

We introduce an incremental add(initially 0) on the definition of the node, so that every time the data is updated, after the node and its subtree are all updated, the +b on the incremental addof the node, so that each time the query or update to its child nodes , it is bound to traverse to the node, at which point it is queried if add is 0, if not 0, the value of add is passed down, and value on the subtree node is updated. (Updating when needed is a good algorithm optimization)

So we can improve the definition of the node above, and the final definition is as follows:

1 /* Tree */ 2 struct node{3     int L, R; 4     int value; 5     int add; 6 }TREE[MAXN];

  

  Build

So how do we build a line tree, we use the tree-like structure of the idea, and constantly get two points to the left son and right son. The value of the original node is added by the value of the left and right son.

Specific as follows:

1 /*extend the line tree from the X-node*/2 voidBuildintXintLintR)3 {4TREE[X].L =l;5TREE[X].R =R;6     if(L = =R) {7Tree[x].value =Source[l];8         return;9     }Ten     intMid = (L + r)/2; OneBuild (X *2, L, mid); ABuild (X *2+1, Mid +1, R); -Tree[x].value = tree[2* X].value + tree[2* x +1].value; -Tree[x].add =0; the}

  Update

The code in the book has been modified and improved here.

So in order to update the data on a piece of data, we have introduced the add increment representation above, as follows:

1 /*Update-Adds m to the [L,r] segment*/2 voidUpdateintXintLintRintm)3 {4     //Update5Tree[x].value + = m* (r-l +1);6     //hit!7     if(TREE[X].L = = L && TREE[X].R = =R) {8Tree[x].add + =m;9         return;Ten     } One     //Add-transfer A     if(tree[x].add) { -tree[2* X].add + =Tree[x].add; -tree[2* X].value + = tree[x].add* (tree[2* x].r-tree[2* X].L +1); thetree[2* x +1].add + =Tree[x].add; -tree[2* x +1].value + = tree[x].add* (tree[2* x +1].r-tree[2* x +1].L +1); -Tree[x].add =0; -     } +     //Continue-search -     intMid = (TREE[X].L + TREE[X].R)/2; +     if(R <= Mid)//[L,r] on the right side of mid AUpdate2*x, L, R, M); at     Else if(L >= Mid)//[L,r] on the left of mid -Update2* x +1, L, R, M); -     Else{//[L,r] across mid -Update2*x, L, Mid, m); -Update2* x +1, Mid +1, R, m); -     } in}

  Inquire

That is, querying data on a segment value

1 //Final Query Value2 intAns =0;3 /*Enquiry*/4 voidQueryintXintLintR)5 {6     //hit!7     if(TREE[X].L = = L && TREE[X].R = =R)8     {9Ans + =Tree[x].value;Ten         return; One     } A     //Add-transfer -     if(tree[x].add) { -tree[2* X].add + =Tree[x].add; thetree[2* X].value + = tree[x].add* (tree[2* x].r-tree[2* X].L +1); -tree[2* x +1].add + =Tree[x].add; -tree[2* x +1].value + = tree[x].add* (tree[2* x +1].r-tree[2* x +1].L +1); -Tree[x].add =0; +     } -     //Continue-search +     intMid = (TREE[X].L + TREE[X].R)/2; A     if(R <= Mid)//[L,r] on the left of mid atQuery2*x, L, R); -     Else if(L >= Mid)//[L,r] on the right side of mid -Query2* x +1, L, R); -     Else{//[L,r] across mid -Query2*x, L, mid); -Query2* x +1, Mid +1, R); in     } -}

Also for a source array Source[max], the segment tree tends to require slightly larger space, about 4*max.

ACM/ICPC algorithm Training tutorial data structure of reading notes (line tree details)

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.