Lazy Propagation in Segment Tree | Set 2
Last Updated :
18 Jan, 2024
Given an array arr[] of size N. There are two types of operations:
- Update(l, r, x) : Increment the a[i] (l <= i <= r) with value x.
- Query(l, r) : Find the maximum value in the array in a range l to r (both are included).
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Update(0, 3, 4)
Query(1, 4)
Output: 8
After applying the update operation
in the given range with given value array becomes {5, 6, 7, 8, 5}.
Then the maximum value in the range 1 to 4 is 8.
Input: arr[] = {1, 2, 3, 4, 5}
Update(0, 0, 10)
Query(0, 4)
Output: 11
Approach: A detailed explanation about the lazy propagation in the segment tree is explained previously. The only thing that needed to change in the question is to return a maximum value between two child nodes when the parent node query is called. See the code for better understanding.
Below is the implementation of the above approach: