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