On line segment Tree merging

Source: Internet
Author: User
We can certainly use DSU on tree to solve some statistical problems with Subtrees. However, once a query with modifications or even historical versions is added, we have to resort to other algorithms, this section describes how to merge line segments. Line Segment Tree merging is generally used for statistics on Subtrees. The common practice is to open a dynamic open-point line segment tree for each node of the tree, and then count the subtree information, merge all son information, count the answer, and continue to go up; there are also many examples, such as [usaco 17jan] Promotion counting promoter count, [noip2016 day1] Every day love to run and so on, the implementation is also very easy to understand, this is for the daily love running type (weight line segment tree)\ (Merge \)Code:\ (Code \):
Void Merge (Int & X, int y) {If (! X |! Y) {x = x + y; return;} sum [x] + = sum [y]; merge (LS [X], ls [y]); merge (RS [X], RS [y]);}
If you want to change to maintain a previous version, you can change it as follows:
Int merge (int x, int y) {If (! X |! Y) {return X + Y; return;} int P = ++ tot; LS [p] = merge (LS [X], ls [y]); RS [p] = Merge (RS [X], RS [y]); sum [p] = sum [ls [p] + sum [RS [p] Return P ;}
Easy to write and understand

On line segment Tree merging

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.