Segment Tree Summary

Source: Internet
Author: User

Basic concept of line segment tree

Segment Tree/Interval tree is a very common data structure to manipulate interval data.
The line segment tree is a binary tree (but not necessarily a completely binary tree!). ), each of its nodes represents an interval, and the parent node represents the sum of the intervals represented by the left and right child nodes. In particular, the interval represented by the root node represents the sum of the intervals for all nodes, and each leaf node represents a single point (that is, an interval of length 1).

Segment tree structure

Each node in the tree represents an interval [a, b]. A, B is usually an integer. Each leaf node represents a unit interval (length of 1). For each non-leaf node the node represented by [A, b], whose left son represents a range of [A, (A+B)/2], the right son represents a range of [(A+B)/2+1,b] (Division to tail rounding).
such as the segment tree for the interval [1, 9]:

segment Tree and Interval

To solve the interval problem with the segment tree, we first need to correspond the interval to the node of the segment tree. This requires an interval decomposition on the online segment tree.
(1) If there is a node represented by the interval completely belongs to the decomposition interval, then the node is "Terminate" node, no longer continue to decompose downward;
(2) The intervals represented by all "terminating" nodes do not overlap, and together they are equal to the entire interval to be decomposed;
(3) interval decomposition, up to 2 "termination" nodes per layer, so the total number of "terminating" nodes is also the log (n) magnitude

Segment Tree Properties
  1. The
  2. segment tree is constructed with two points and the depth is  log 2 (b −a+ 1) (rounding up). In this way, the complexity of the operation can be log (n) magnitude
  3. When the update query operation is performed.
  4. The number of leaf nodes is the same as the interval length represented by the root node
  5. If the number of leaf nodes is N, the total number of nodes in the segment tree is 2*n-1. Because the node of the segment tree is either 0 degrees or 2 degrees, according to the nature of the binary tree.
  6. If the leaf node is n, in order to use a contiguous array to represent a segment tree, the size of the array should be 4N.
    Because, according to the nature of 3, the total number of nodes in the segment tree is 2*n-1, and because the segment tree is not a complete binary tree, so its lowest leaf layer is not close to the leftmost, so that the index number on the second-to-last level is close to 2N, according to 2*index+1 and 2*index+ The 2 way to access its left and right child nodes, which results in an entire array size offer of 4*n.
  7. In the segment tree, the number of "terminating" nodes obtained after any interval is decomposed is log (n) magnitude.
    Because there are up to two "terminating" nodes in each layer, there is a total log (n) layer so that the time complexity of updating the leaf nodes and interval decomposition on the segment tree is O (logn).
basic types of segment tree operations

(1) Single point update
Each time a point within the interval is updated, the update operation needs to be recursively to the leaf node. In the process of downward recursion, all nodes passing along the way are modified.
(2) Paragraph update
Simple explanation: The paragraph update needs to use the delay tag (or lazy tag), in short, every time the update does not update to the end, with the delay tag so that the update delay to the next time when you need to update or ask. The delay tag means that both the left and right sons of this interval need to be updated, but the current interval has been updated.
(3) Interval merging
Interval merging is the merging of the right and left sons of the current interval when the line tree is queried.
(4) Scan line

The general rule of problem solving in line tree
    1. The first thing to determine is an interval, if not the obvious interval, to see if it can be converted into an interval. For example poj_3321 's solution
    2. To figure out what information each node should hold (of course, the start and end nodes of the interval, and the left and right child node pointers are required)
    3. Update, to see if the lazy update, that is, do not update to the leaf node, which will lead to the efficiency of the update is reduced
    4. Build first, then insert data, then update/query
other

There are several summary of good blogs, easy to find, listed below:
Segment Tree Summary 1
Segment Tree Summary 2

Segment Tree Summary

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.