Lazy Propagation in Segment Tree17 Mar 2025 | 9 min read In the last post, the segment tree was presented along with a range sum problem example. We explained lazy propagation using the same "Sum of specified Range" problem. ![]() How does the Simple Segment Tree update function?The update method was used in the last lesson to change just one value in an array. Due to the possibility that several segment tree nodes have a single array element in their ranges, it is important to be aware that a single value update in an array may result in multiple updates in the segment tree. This post's simple reasoning is shown below. 1) Begin at the segment tree's root. 2) Return if the array index that has to be modified is outside the current node's range. 3) Update the current node and repeat for children if not. The code from the previous post is shown here. C++ CodeThe application of the aforementioned strategy is seen below: What if a number of array indexes have updates?Add 10, for instance, to each element in the array with an index between 2 and 7. For every index from 2 to 7, the aforementioned update has to be called. By creating a function called updateRange() that updates nodes as necessary, we can avoid making numerous calls. C++ CodeThe application of the aforementioned strategy is seen below: Lazy Propagation is an optimization for speeding up range updates.We can delay some updates when there are multiple updates and updates are being performed on a range (avoiding recursive calls in update) and do such updates just as needed. Please keep in mind that a node in a segment tree stores or symbolises the outcome of a query for a variety of indexes. Additionally, if the update operation's range includes this node, all of its descendants must likewise be updated. Consider, for instance, the node with value 27 in the picture above. This node contains the sum of values at indexes ranging from 3 to 5. We must update both this node and all of its descendants if our update query covers the range 2 to 5. By storing this update information in distinct nodes referred to as lazy nodes or values, we use lazy propagation to update only the node with value 27 and delay updates to its descendants. We make an array called lazy [] to stand in for the lazy node. The size of lazy [] is the same as the array used to represent the segment tree in the code following, which is tree []. The goal is to set all of lazy [elements ]'s to 0. There are no pending changes on segment tree node I if lazy[i] has a value of 0. A non-zero value for lazy[i] indicates that before doing any queries on node I in the segment tree, this sum needs to be added to the node. Here is an updated updating technique. Has the Query Function changed as well?If a query is sent to a node that hasn't been updated yet, there can be issues because we adjusted update to postpone its activities. Therefore, we also need to alter the query method we used in the previous post, getSumUtil. The getSumUtil() now determines whether an update is pending before updating the node. Once it confirms that any pending updates have been made, it functions similarly to the previous getSumUtil (). The programmes below show how Lazy Propagation works. C++ CodeThe application of the aforementioned strategy is seen below: Output: Sum of values in given range = 15 Updated sum of values in given range = 45 Time Complexity: O(n) Auxiliary Space: O(MAX) Next TopicSegment Tree - Range Minimum Query |
Before understanding the types of Tree in Data Structure, first, let us understand what is Tree as a Data Structure. The tree can be defined as a non-linear data structure that stores data in the form of nodes and nodes are connected to each other with...
25 min read
Splay trees are the self-balancing or self-adjusted binary search trees. In other words, we can say that the splay trees are the variants of the binary search trees. The prerequisite for the splay trees that we should know about the binary search trees. As we already know,...
14 min read
Introduction Burning a binary tree beginning from a particular node is a fascinating issue in computer science, frequently experienced in algorithmic interviews and competitive programming. This task includes reproducing the spread of fire from a given node all through the binary tree and deciding the time it...
4 min read
A flattened binary tree is a changed version of the normal binary tree, as all the nodes present are rearranged to create a linear structure. All the nodes in the tree are organized so that when traversing the tree from left to right, we observe the...
6 min read
Introduction In this article, we dive into the methodologies and calculations utilized actually to handle this issue. In mechanical technology and improvement issues, boosting chocolates in a matrix utilizing two robots presents a charming test. This situation includes effectively exploring a network to gather as many chocolates as...
11 min read
Introduction A basic data structure in computer science, priority queues enable quick access to the element with the highest (or lowest) priority. Priority queues in C++ can be expanded to handle pairs, providing a flexible method of sorting according to the first or second element of the...
7 min read
The median concept is very important when it comes to data analysis and algorithm creation. It offers a reliable way to calculate central tendency and sheds light on the properties and distribution of a dataset. One interesting problem when working with a stream of integers is...
6 min read
This article will teach us to find the kth largest element in an unsorted array. There are different ways to find the solution to the given problem. The best possible practices are discussed below: Problem - Consider an unsorted array with N number of elements. A number...
26 min read
In 1962, GM Adelson-Velsky and EM Landis created the AVL Tree. To honors the people who created it, the tree is known as AVL. The definition of an AVL tree is a height-balanced binary search tree in which each node has a balance factor that is determined...
14 min read
Introduction One common algorithmic problem-solving challenge is to determine the longest subarray with particular properties. This article will focus on resolving a specific variation of this issue, which involves determining the longest subarray in which a single value exceeds a specified threshold, k. We'll utilize the programming...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India