Lazy Propagation in Segment Tree in Java13 May 2025 | 9 min read The topic lazy propagation in segment tree in Java is the continuation of the topic segment tree in Java. Readers are advised to go through the segment tree topic first. Lazy propagation in a segment tree means postponing the updates of some values and updating them only when it is required. Update OperationLet's recall the update operation of a segment tree.
![]() Scenario for Lazy PropagationLet's discuss a scenario where one should use the lazy propagation technique. Suppose the task is to add the value 6 to each of the elements of the input array from the index 1 to 4. To do the task, one has to call the update() method for each element from index 1 to 4. These multiple calls lead to more time consumption. It is where the lazy propagation technique kicks in to make the update process fast. Note that a node of the segment tree contains the result of a query for a range of indices. And if the range of this node lies within the range of the update operation, then all the descendants of that node have to be updated. For example, if we consider the node with the value 35 in the above diagram, then the node contains the sum of values at the indices from 3 to 5 (see the above diagram). If the query of an update is for the range 2 to 5, then we have to update that node and all the descendants of that node. Using the lazy propagation, we need to update the node with value 35 and defer the updates of its descendants by containing this information of update in the separate nodes known as lazy nodes or the values. We create an array lazy[] that represents the lazy nodes. The size of the array lazy[] is the same as the array that does the representation of the segment tree, which is t[] in the following code. The approach is to initialize all the elements of the array lazy[] as 0. A value of 0 in the array lazy[j] shows that there are no updates pending on node j in the segment tree. Any other value (let's say the value is v) of lazy[j] means that tree before making any query to the node, v amount must be added to the node j in the segment tree. Steps Involved in Updating a Node Using Lazy Propagation in Segment TreeImplementation of Lazy PropagationObserve the following program. FileName: LazySegTree.java Output: The sum of the values in the given range is: 33 The sum of the values, after updation, in the given range is: 61 Next TopicFinal-arrays-in-java |
The java.nio.FloatBuffer Class has a flip() function. To flip this buffer, use the FloatBuffer Class. The buffer will be truncated to the current location and then the position will be adjusted to zero as a result of flipping this buffer. Any marks that may have...
3 min read
? In this section, we will learn the different approaches to find the length of an integer in Java. The length of an integer means the total number of digits present in that integer. We can find the length of integer by using the following approaches: Using while loop Using...
5 min read
What is FCFS scheduling algorithm? First Come, First Served (FCFS) is a non-primitive CPU scheduling algorithm. It schedules processes in the order in which they arrive in the ready queue. Processes are executed one by one until completion. What is arrival time? Arrival time is the time at which...
6 min read
Java is a versatile programming language, known for its rich library of classes and methods that empower developers to create complex and interactive graphical user interfaces (GUIs). When it comes to creating GUI components in Java, the setBounds() method plays a vital role. In this section,...
4 min read
In computer science, LinkedLists are a common data structure that are frequently used to store and manage collections of data. A LinkedList is made up of nodes, each of which has a value and a connection to the node after it in the list. There are...
8 min read
In Java, package plays an important role in enting naming conflicts, controlling access, and making searching and usage of classes, enumeration, interfaces, and annotation easier. In order to group classes, interfaces, and sub-packages that are related to each other, we use packages. By using packages: It is very...
3 min read
A positive integer array with an equal amount of digits for each integer is given. The number of distinct digits that occur in the same location in two integers is known as the digit difference between them. The total of the digit differences between each pair of...
7 min read
In the world of Java programming, interfaces play a vital role in defining contracts and establishing communication between classes. Typically, an interface is used to declare a set of methods that implementing classes must adhere to. However, Java also allows the creation of interfaces without any...
4 min read
In this section, we will learn what is a repdigit numbers and also create Java programs to check if the given number is a repdigit number or not. The repdigit number program is frequently asked in Java coding interviews and academics. Repdigit Number Repdigit is short for repeated...
2 min read
Java, a versatile and widely used programming language, provides a robust foundation for building various data structures and classes. In this section, we will delve into a custom class named Cint that represents a comparable integer, offering additional functionality for comparison operations. Java Cint Class The Java Cint...
5 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