Getting started with line tree

Source: Internet
Author: User

Line Segment tree(Interval Tree) is a tree structure that gets two parts of the interval. It reflects the problem solving methods of many grouping algorithms, including Merge Sorting.

 

It is a typical line segment tree that segments [1, 10] until a single vertex. Features of this tree
Yes:
1. Each layer is a division of the interval [a, B]. Note that L = B-

2. A total of log2l Layers
3. Given a vertex P, all intervals from the root to the leaf P contain vertex P, and other intervals do not contain vertex P.
4. Given an interval [L; R], you can break it down into the Union of 2 or more non-intersecting lines.
The fourth point is not very obvious. Figure 8.1 shows the decomposition method of [2, 8], and the deep gray node is
Interval. The light gray node is the node that passes through the recursive decomposition process. Nodes in the tree are described below for convenience.
The corresponding interval is the interval in the tree.

 

 

We can draw a conclusion from the 3rd and 4th points: to modify a vertex, we only need to modify the interval information in the log2 and l trees. to calculate a interval, we only need to accumulate the information of 2 log2 and l trees, in addition, the sum points for access are O (log L. Both operations are easy to implement.
Dynamic statistics I has an integer array a that contains n elements. Each time an element can be modified, you can also ask the sum of all elements in a certain range [L; R. How to design algorithms to minimize the time complexity of modification and inquiry operations?
If method 1 is done directly, the modification operation is O (1), but the Query Needs to be accumulated. The time complexity is O (r ¡L), and the worst case is O (n ).
Method 2: Create a line segment tree. The interval in each tree records the elements and values of the interval, when modifying an element, you only need to modify the elements and values of the intervals in the log2 l tree. During statistics, you only need to add the elements and values of the intervals in the 2log2 l tree. Both operations are O (log n) is much better than a method.
Dynamic statistical problem II has an integer array a that contains n elements. Each time a range [L; r] adds a number D (D can be negative) at the same time, or you can ask a number AI value. How to design algorithms to minimize the time complexity of modification and inquiry operations?

How can I quickly modify a range? Modifying the interval [A; B] In a tree will affect the entire subtree rooted in [A; B] and all its ancestors. Therefore, if we follow the line segment tree definition just now, it is impossible to quickly implement the interval modification operation.
The solution is to use the commonly used \ lazy operations in the data structure to only record the operations that need to be executed, rather than actually performing these operations. In other words, the element of the interval [L; R] In the tree needs to be added at the same time.
When adding D, we only record \ once had a command Add (L, R, D. The value of the record is called the Add Value of the interval in the tree. The element value of the leaf node is the sum of the add values of all its ancestors. According to the preceding conclusion, the modification commands for each interval can be decomposed into the modification commands for the interval in the 2log2 l tree, and the modification operations are overlapping, therefore, the time complexity of the modification operation is O (log n ). Query operation only
All the ancestors of the leaves need to be accumulated. It is also O (log n.
Dynamic statistics problem III there is an integer array a that contains n elements. Each time, a Range [L; the number in R] adds a number D (D can be negative) at the same time. You can also ask the sum of all elements in a range [L; R. How to design algorithms to minimize the time complexity of modification and inquiry operations?

Obviously, the first two problems are special cases of this problem, so this problem is more difficult than the first two problems. Note that the line segment tree in the previous problem can only provide the true elements and values of leaf nodes, because for the interval [L; R] in any tree, the interval modified by the instruction that affects it includes not only all its ancestors, but also all its descendants. Therefore, the values of all elements in [L; R] should be equal to the values of the add values of all [L; R] ancestors plus the values of the add values of all generations of [L; R.

However, there are many future generations, and the overhead of the accumulated time is too large. Here, we need to record an additional value sum_of_add by using the interval addition property of the Line Segment tree, indicating the sum of the values of the add values of all trees in the tree with the root of [L; R.
The modify operation splits the interval into the interval in the tree, modifies their add value, and modifies their father's sum_off_add value by the way. Since the sum point of access to the interval decomposition process is O (log L), the modification operation is O (log n.
The query operation splits the interval into the interval in the tree, counts the sum of the add values of all their ancestors, and adds them with the sum_off_add values of the interval itself in these trees.

 

This article is from the competition of algorithm art and informatics ---- Author: Liu rujia

Related Article

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.