VOOZH about

URL: https://www.geeksforgeeks.org/dsa/range-update-without-using-lazy-propagation-and-point-query-in-a-segment-tree/

⇱ Range Update without using Lazy Propagation and Point Query in a Segment Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Range Update without using Lazy Propagation and Point Query in a Segment Tree

Last Updated : 3 Oct, 2025

Given an array arr[] consisting of N 0s and a 2D array Q[][] consisting of queries of the following two types:

  • 1 L R X: Increment all the elements in the range [L, R] by X.
  • 2 X: Print elements at Xth index of the array.

Input: arr[] = { 0, 0, 0, 0, 0 }, Q[][] = { { 1, 0, 2, 100 }, { 2, 1 }, { 1, 2, 3, 200 }, { 2, 2 }, { 
4 } } 
Output: 100 300 0 
Explanation: 
Query1: Incrementing all the array elements in the range [0, 2] by 100 modifies arr[] to { 100, 100, 100, 0, 0 }. 
Query2: Print arr[1](=100) 
Query3: Incrementing all the array elements in the range [2, 3] by 200 modifies arr[] to { 100, 100, 300, 200, 0 }. 
Query4: Print arr[2](=300) 
Query5: Print arr[4](=0)

Input: arr[] = { 0, 0 }, Q[][] = { { 1, 0, 1, 100 }, { 2, 1 } } 
Output: 100

Approach: The idea is to use the concept of Segment Tree to perform queries of type 2 and the difference array to perform queries of type 1. Follow the steps below to solve the problem:

Below is the implementation of the above approach:


Output: 
Element at 1 is 100

 

Time Complexity: O(|Q| * log(N))
Auxiliary Space: O(N)

Comment