Segment Tree in Segment Tree
Common Applications
In algorithm competitions, we sometimes need to maintain multi-dimensional information. In such cases, we often require nested data structures like segment trees within segment trees to record information.
Implementation Principle
Consider how to implement single-point modification and range query on a two-dimensional plane using a nested segment tree. We consider the outer segment tree, where the subtrees of the bottom-level nodes from \(1\) to \(n\) represent the segment trees for rows \(1\) to \(n\), respectively. The parent nodes corresponding to these bottom-level nodes then represent the region covered by the subtrees of their two child nodes.
Properties
Space Complexity
Typically, we cannot build a sub-segment tree for every node of the outer segment tree due to excessive space requirements. Nested segment trees generally adopt a dynamic node creation strategy. A single modification involves \(\log{n}\) nodes of the outer segment tree, and for each such node, it involves \(\log{n}\) nodes of its subtree. Thus, the maximum space required for a single modification is \(\log^2{n}\).
Time Complexity
For a query operation, we perform \(\log{n}\) operations on the outer segment tree, each of which involves \(\log{n}\) operations on an inner segment tree. Therefore, the time complexity is \(\log^2{n}\). Modification operations have the same complexity as query operations, also \(\log^2{n}\).
Classic Example
Flowers on Moshang sorts the first dimension and then uses a nested segment tree to maintain the second and third dimensions.
Sample Code
Second dimension query
1 2 3 4 5 6 7 8 | |
Second dimension modification
1 2 3 4 5 6 7 8 9 10 | |
Third dimension query
1 2 3 4 5 6 7 8 | |
Third dimension modification
1 2 3 4 5 6 7 8 | |
Related Algorithms
When dealing with multi-dimensional information problems, if the problem does not require being strictly online, we can also consider divide-and-conquer algorithms such as CDQ Divide and Conquer or Parallel Binary Search to avoid using advanced data structures and reduce implementation difficulty.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用