VOOZH about

URL: https://www.geeksforgeeks.org/dsa/segment-tree-for-range-multiplication-and-sum-queries/

⇱ Segment Tree for Range Multiplication and Sum Queries - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Segment Tree for Range Multiplication and Sum Queries

Last Updated : 23 Jul, 2025

Given an array of size N filled with all 1s, the task is to answer Q queries, where the queries can be one of the two types:
Type 1 (1, L, R, X): Multiply all elements on the segment from L to R−1 by X, and
Type 2 (2, L, R): Find the sum on the segment from L to R−1.

Examples:

Input: N = 5, Q = 6, queries[] = {
{1, 0, 3, 3},
{2, 1, 2},
{1, 1, 4, 4},
{2, 1, 3},
{2, 1, 4},
{2, 3, 5}}
Output: 3 24 28 5
Explanation:

  • Initially the array is {1, 1, 1, 1, 1}
  • First query is to multiply segment from index 0 to 2 by 3, so after first query the array is {3, 3, 3, 1, 1}.
  • Second query is to print the sum of segment from index 1 to 1, so the sum = 3.
  • Third query is to multiply segment from index 1 to 3 by 4, so after third query is {3, 12, 12, 4, 1, 1}.
  • Fourth query is to print the sum of segment from index 1 to 2, so the sum = 12 + 12 = 24.
  • Fifth query is to print the sum of segment from index 1 to 3, so the sum = 12 + 12 + 4 = 28.
  • Sixth query is to print the sum of segment from index 3 to 4, so the sum = 4 + 1 = 5.

Input: N = 2, Q = 2, queries[] = {
{1, 0, 1, 1000000},
{2 0 2}}
Output: 1000001

Approach: To solve the problem, follow the below idea

The idea is to use a segment tree data structure with lazy propagation to efficiently process the queries. The segment tree is built such that each node stores the sum of its corresponding segment of the array. When an update query is performed, instead of immediately updating all the elements in the range, the update is postponed and stored in a separate array called lazy. The pending updates are applied only when they are needed, i.e., when a sum query is performed on a node or its descendants.

Step-by-step algorithm:

  • Declare arrays t and lazy for the segment tree and lazy propagation and initialize lazy values to 1.
  • Build the segment tree recursively using the build function.
  • Initialize lazy values to 1.
    • Implement the push function to apply pending updates.
    • Update values based on the lazy value.
    • If the node is not a leaf, update the lazy values for the left and right children.
    • Reset the lazy value.
    • Implement the update function to modify a range of the array and the segment tree.
    • Apply pending updates using push.
    • If the range is invalid, return.
    • If the range matches the segment, update the lazy value and apply the update immediately.
    • Otherwise, update the left and right children recursively and merge their values.
    • Implement the query function to retrieve information about a range of the array.
    • Apply pending updates using push.
    • If the range is invalid, return 0.
    • If the range matches the segment, return the value of the segment.
    • Otherwise, calculate the middle and return the sum of queries on the left and right children.

Below is the implementation of the algorithm:


Output
3
24
28
5

Time Complexity: O(NlogN + MlogN), where N is the array size and M is the number of operations.
Auxiliary Space: O(N) for the segment tree and lazy propagation arrays.

Comment