Line segment tree (scan line)

Source: Internet
Author: User

Here we use the HDU 1542 as an example, a classic scan line topic, calculate the rectangle and the.

First, the scan line, is an imaginary line, from left to right a vertical bar to scan the past.

Scan lines can be used to fill the polygon, specifically, see http://blog.csdn.net/orbit/article/details/7368996 write well.

Then there is the application of the scan line in this problem.

  

Calculating the area of the two rectangles is equivalent to calculating the area of the red, green, and blue three blocks.

So, from left to right scan line scan, line tree maintenance is the total length of the interval is covered, and then multiplied by the distance between two scanning lines, is a piece of area, and then update the segment tree, in order to push, get the total area.

But there are three places to be aware of this problem:

1, discretization, the coordinates of each edge is a floating-point number, to be discretized, remember to go heavy.

2, is the length of the interval coverage calculation problem, here I tangled old, first if the point to cover the interval 2,8 coverage is actually 1,5 and 6,10 coverage (if the segment tree is 1,5 and 6,10, then the middle of the 5 and 6 is not calculated, then the problem ...)

So it's not going to be covered by dots, so each point should be treated as a line between this point and the next point, so that you don't miss the middle one.

If it is a line segment, then if there are 10 points, then there are 9 line segments, so the line segment tree is 1 to 9 (instead of 10), each time the length of the calculation of the right to remember to add one.

3, is the line tree update problem, each add a line segment or minus a line segment, it is not easy to cover the good, but should record that the interval is covered several times, until the time to zero is no line in the coverage of the interval. In this case, each additional line segment is the interval +1 of the line segment, to a line segment is-1.

However, the pushdown is not good, because if the col value of a node is pushed to the following two, then if the next overwrite, the node's col becomes-1, it is not good to calculate the value of this node maintenance. So according to the god of Hangzhou TV, not pushdown, but in the calculation of pushup, if the node has been covered for more than 0 of the direct calculation (because the node represents the entire range is covered, directly to the right minus the left.) ), otherwise only recourse to the following two nodes. Also, the query only asks for the value of node 1, so there is no error.

Almost three of them, the level is limited, please understand.

Examples:

HDU 1542, click.

HDU 1828, click.

Line segment tree (scan line)

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.