Line Segment tree + discrete data (well written)

Source: Internet
Author: User

1. The line segment tree is a binary tree and must be a balanced binary tree, but not necessarily a Complete Binary Tree.

2. If the range [a, B] is set to mid = (a + B)/2, the left subtree is [a, mid]. the right subtree is [Mid + 1, B]. When a = B, the interval is the leaf of the Line Segment tree, and no further division is required.

3. Although the line segment tree is not a complete binary tree, it can be constructed and stored in the form of a Complete Binary Tree, but the last layer may contain "Empty leaves" between some leaves ", ignore this. Similarly, empty leaves are numbered sequentially. When we determine a = B when traversing a line segment tree, we think that the leaves will never be traversed.

4. The purpose of using a Complete Binary Tree to store a line segment tree is to improve the efficiency of inserting line segments and searching. Using the p * 2, p * 2 + 1 Index to retrieve the left and right subtree of P is much faster than the pointer.

5. The essence of a line segment tree is that you do not need to search any longer. Use the root information of the subtree as much as possible to obtain the information of the entire tree. If you have to search for leaves each time when inserting a line segment or retrieving feature values, it is easier to directly create a common tree.

 

However, this question is not ac because the root system of a [1, 1 QW] Line Segment tree is very large, tle and MLE are fixed. Therefore, discretization is required.

In layman's terms, discretization is the compression interval, which maps the original long interval to the new short interval, but the coverage relationship before and after the compression remains unchanged. For example:

There is a number axis from 1 to 10 (the length is 9). Given four intervals [2, 4] [3, 6] [8, 10] [6, 9], the overwrite relationship means that the latter overwrites the former, the color of each interval is 1 2 3 4 in sequence.

Now we extract the eight endpoints of the four intervals, 2 4 3 6 8 6 9

Delete the same endpoint. If the same endpoint is 6, the remaining two are 4 3 6 8 10 9.

Sort them in ascending order. The values are 2, 3, 4, 6, 8, 9, and 10.

Then create a ing

2 3 4 6 8 9 10

When there are too many other users

1 2 3 4 5 6 7

The new four intervals are [1, 3] [2, 4] [5, 7] [4, 6], and the overwrite relationship is not changed. The new number axis is 1 to 7, that is, the length of the original number axis is compressed from 9 to 6. Obviously, the line segment tree of [] is more space-saving than the line segment tree, the search speed is faster, but the results are consistent.

 

Note that the same endpoint must be removed before sorting. This reduces the number of elements involved in sorting and saves time.

Line Segment tree + discrete data (well written)

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.