![]() |
VOOZH | about |
Segment tree is the data structure that can be used for solving the range queries efficiently. In Segment tree, a single value update in array may cause multiple updates in Segment Tree as there may be many segment tree nodes that have a single array element in their ranges. Using Lazy Propagation, we can handle the updates in a much faster way. In this article, we will learn about the basics of Lazy Propagation along with its implementation in Python.
Segment Tree is a kind of binary tree which optimizes the operations of array segment processing. Each element in the tree is called a segment (or range) over the array. The root node will represent the whole array while each child will represent the half of the portion of their parent recursively.
While range trees primarily serve the purpose of performing range queries on the elements efficiently, some cases may require finding the sum of all elements in a given range or finding the maximum/minimum element in a range.
Segment trees can be mostly applied for range queries. Common operations include:
Range Sum Query: Finding the sum of elements within a given range.
Range Minimum Query: Searching for the smallest element in the given range.
Range Maximum Query: The maximum element of a given range.
Range Update: Updating all elements from the given range.
Lazy propagation is a method which helps to make the segment tree algorithms efficient and particularly on cases where the updates are frequent or the updates are for the entire segment. To address this, the recommendation is to hold the update of segments until they are needed and keep a “lazy” array to store pending updates.
The drawback of the segment trees is the updating of the system which is more difficult especially when the updates are frequent or to be applied to a big part of the segment. The conventional solution involves modification of all the nodes of the path from the main to the sub-root nodes, which leads to O(N) time complexity for each update.
Lazy Propagation helps achieve this by putting off updates until the right time. It keeps a lazy array along with the segment tree, that is used to store the updates that are not applied yet. By this method, we only update the nodes only when necessary when a query is asked. So, we are able to reduce the number of updates a lot.
Lazy propagation reduces the time complexity of range query operations. Without lazy propagation, performing updates and queries might require traversing the entire tree, resulting in O(N) complexity. . With lazy propagation, the time complexity of range queries can be reduced to O(logN) or even O(1) in some cases:
Below is the implementation of Lazy Propagation in Python:
Time Complexity: O(|Q| * log(N))
Auxiliary Space: O(N)
Segment Trees in frequency queries is a great technique for arrays to maintain and query intervals. It works to run queries related to the range such as sum or the extremes in some set range structure and binary tree. Nevertheless, the requirement for frequent and bulk updates makes its use quite difficult and is therefore considered less feasible while higher techniques are required.