Implementation and application of line segment tree lazy markup

Line segment tree is a common data structure used to solve interval query problems, mainly used to support operations such as interval query and single-point modification. In addition, the line segment tree has some advanced usage, which can help us better solve some special problems.

Below we will introduce several advanced usages of line segment trees, and give corresponding sample codes.

  1. Interval modification

In the standard line segment tree, we can only modify the value of a certain position, and cannot modify all elements in a range. However, in some cases, we need to modify all elements in an interval, then we need to use the line segment tree modified by the interval.

The core idea of ​​the line segment tree for interval modification is "lazy marking". We can temporarily store the modification operations that need to be performed on the elements in the interval in each node, and then execute these operations together when we need to query the interval sum of the node.

Here is a simple sample code:

class SegmentTree:
    def __init__(self, data):
        self.size = len

Guess you like

Origin blog.csdn.net/qq_29669259/article/details/130258943