Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Iterative Segment Tree (Range Maximum Query with Node Update)
Next article icon

Segment tree | Efficient implementation

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Let us consider the following problem to understand Segment Trees without recursion.
We have an array arr[0 . . . n-1]. We should be able to,  

  1. Find the sum of elements from index l to r where 0 <= l <= r <= n-1
  2. Change the value of a specified element of the array to a new value x. We need to do arr[i] = x where 0 <= i <= n-1. 

A simple solution is to run a loop from l to r and calculate the sum of elements in the given range. To update a value, simply do arr[i] = x. The first operation takes O(n) time and the second operation takes O(1) time.


Another solution is to create another array and store the sum from start to i at the ith index in this array. The sum of a given range can now be calculated in O(1) time, but the update operation takes O(n) time now. This works well if the number of query operations is large and there are very few updates.
What if the number of queries and updates are equal? Can we perform both the operations in O(log n) time once given the array? We can use a Segment Tree to do both operations in O(Logn) time. We have discussed the complete implementation of segment trees in our previous post. In this post, we will discuss the easier and yet efficient implementation of segment trees than in the previous post.
Consider the array and segment tree as shown below:  


You can see from the above image that the original array is at the bottom and is 0-indexed with 16 elements. The tree contains a total of 31 nodes where the leaf nodes or the elements of the original array start from node 16. So, we can easily construct a segment tree for this array using a 2*N sized array where N is the number of elements in the original array. The leaf nodes will start from index N in this array and will go up to index (2*N - 1). Therefore, the element at index i in the original array will be at index (i + N) in the segment tree array. Now to calculate the parents, we will start from the index (N - 1) and move upward. For index i , the left child will be at (2 * i) and the right child will be at (2*i + 1) index. So the values at nodes at (2 * i) and (2*i + 1) are combined at i-th node to construct the tree. 
As you can see in the above figure, we can query in this tree in an interval [L,R) with left index(L) included and right (R) excluded.
We will implement all of these multiplication and addition operations using bitwise operators.
Let us have a look at the complete implementation:  

C++
#include <bits/stdc++.h> using namespace std;  // limit for array size const int N = 100000;   int n; // array size  // Max size of tree int tree[2 * N];  // function to build the tree void build( int arr[])  {      // insert leaf nodes in tree     for (int i=0; i<n; i++)             tree[n+i] = arr[i];          // build the tree by calculating parents     for (int i = n - 1; i > 0; --i)              tree[i] = tree[i<<1] + tree[i<<1 | 1];     }  // function to update a tree node void updateTreeNode(int p, int value)  {      // set value at position p     tree[p+n] = value;     p = p+n;          // move upward and update parents     for (int i=p; i > 1; i >>= 1)         tree[i>>1] = tree[i] + tree[i^1]; }  // function to get sum on interval [l, r) int query(int l, int r)  {      int res = 0;          // loop to find the sum in the range     for (l += n, r += n; l < r; l >>= 1, r >>= 1)     {         if (l&1)              res += tree[l++];              if (r&1)              res += tree[--r];     }          return res; }  // driver program to test the above function  int main()  {     int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};      // n is global     n = sizeof(a)/sizeof(a[0]);          // build tree      build(a);          // print the sum in range(1,2) index-based     cout << query(1, 3)<<endl;          // modify element at 2nd index     updateTreeNode(2, 1);          // print the sum in range(1,2) index-based     cout << query(1, 3)<<endl;      return 0; } 
Java
import java.io.*;  public class GFG {          // limit for array size     static int N = 100000;           static int n; // array size          // Max size of tree     static int []tree = new int[2 * N];          // function to build the tree     static void build( int []arr)      {                   // insert leaf nodes in tree         for (int i = 0; i < n; i++)              tree[n + i] = arr[i];                  // build the tree by calculating         // parents         for (int i = n - 1; i > 0; --i)              tree[i] = tree[i << 1] +                       tree[i << 1 | 1];      }          // function to update a tree node     static void updateTreeNode(int p, int value)      {                   // set value at position p         tree[p + n] = value;         p = p + n;                  // move upward and update parents         for (int i = p; i > 1; i >>= 1)             tree[i >> 1] = tree[i] + tree[i^1];     }          // function to get sum on     // interval [l, r)     static int query(int l, int r)      {          int res = 0;                  // loop to find the sum in the range         for (l += n, r += n; l < r;                              l >>= 1, r >>= 1)         {             if ((l & 1) > 0)                  res += tree[l++];                      if ((r & 1) > 0)                  res += tree[--r];         }                  return res;     }          // driver program to test the     // above function      static public void main (String[] args)     {         int []a = {1, 2, 3, 4, 5, 6, 7, 8,                                 9, 10, 11, 12};              // n is global         n = a.length;                  // build tree          build(a);                  // print the sum in range(1,2)         // index-based         System.out.println(query(1, 3));                  // modify element at 2nd index         updateTreeNode(2, 1);                  // print the sum in range(1,2)         // index-based         System.out.println(query(1, 3));      } }  // This code is contributed by vt_m. 
Python
# Python3 Code Addition  # limit for array size  N = 100000;   # Max size of tree  tree = [0] * (2 * N);   # function to build the tree  def build(arr) :      # insert leaf nodes in tree      for i in range(n) :          tree[n + i] = arr[i];           # build the tree by calculating parents      for i in range(n - 1, 0, -1) :          tree[i] = tree[i << 1] + tree[i << 1 | 1];   # function to update a tree node  def updateTreeNode(p, value) :           # set value at position p      tree[p + n] = value;      p = p + n;           # move upward and update parents      i = p;          while i > 1 :                  tree[i >> 1] = tree[i] + tree[i ^ 1];          i >>= 1;   # function to get sum on interval [l, r)  def query(l, r) :       res = 0;           # loop to find the sum in the range      l += n;     r += n;          while l < r :              if (l & 1) :             res += tree[l];              l += 1              if (r & 1) :             r -= 1;             res += tree[r];                       l >>= 1;         r >>= 1          return res;   # Driver Code if __name__ == "__main__" :       a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];       # n is global      n = len(a);           # build tree      build(a);           # print the sum in range(1,2) index-based      print(query(1, 3));           # modify element at 2nd index      updateTreeNode(2, 1);           # print the sum in range(1,2) index-based      print(query(1, 3));       # This code is contributed by AnkitRai01 
C#
using System;  public class GFG {          // limit for array size     static int N = 100000;           static int n; // array size          // Max size of tree     static int []tree = new int[2 * N];          // function to build the tree     static void build( int []arr)      {                   // insert leaf nodes in tree         for (int i = 0; i < n; i++)              tree[n + i] = arr[i];                  // build the tree by calculating         // parents         for (int i = n - 1; i > 0; --i)              tree[i] = tree[i << 1] +                        tree[i << 1 | 1];      }          // function to update a tree node     static void updateTreeNode(int p, int value)      {          // set value at position p         tree[p + n] = value;         p = p + n;                  // move upward and update parents         for (int i = p; i > 1; i >>= 1)             tree[i >> 1] = tree[i] + tree[i^1];     }          // function to get sum on     // interval [l, r)     static int query(int l, int r)      {          int res = 0;                  // loop to find the sum in the range         for (l += n, r += n; l < r;                              l >>= 1, r >>= 1)         {             if ((l & 1) > 0)                  res += tree[l++];                      if ((r & 1) > 0)                  res += tree[--r];         }                  return res;     }          // driver program to test the     // above function      static public void Main ()     {         int []a = {1, 2, 3, 4, 5, 6, 7, 8,                             9, 10, 11, 12};              // n is global         n = a.Length;                  // build tree          build(a);                  // print the sum in range(1,2)         // index-based         Console.WriteLine(query(1, 3));                  // modify element at 2nd index         updateTreeNode(2, 1);                  // print the sum in range(1,2)         // index-based         Console.WriteLine(query(1, 3));      } }  // This code is contributed by vt_m. 
JavaScript
<script>     // limit for array size     let N = 100000;             let n; // array size            // Max size of tree     let tree = new Array(2 * N);     tree.fill(0);            // function to build the tree     function build(arr)      {                     // insert leaf nodes in tree         for (let i = 0; i < n; i++)              tree[n + i] = arr[i];                    // build the tree by calculating         // parents         for (let i = n - 1; i > 0; --i)              tree[i] = tree[i << 1] +                        tree[i << 1 | 1];      }            // function to update a tree node     function updateTreeNode(p, value)      {          // set value at position p         tree[p + n] = value;         p = p + n;                    // move upward and update parents         for (let i = p; i > 1; i >>= 1)             tree[i >> 1] = tree[i] + tree[i^1];     }            // function to get sum on     // interval [l, r)     function query(l, r)      {          let res = 0;                    // loop to find the sum in the range         for (l += n, r += n; l < r;                              l >>= 1, r >>= 1)         {             if ((l & 1) > 0)                  res += tree[l++];                        if ((r & 1) > 0)                  res += tree[--r];         }                    return res;     }          let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];            // n is global     n = a.length;      // build tree      build(a);      // print the sum in range(1,2)     // index-based     document.write(query(1, 3) + "</br>");      // modify element at 2nd index     updateTreeNode(2, 1);      // print the sum in range(1,2)     // index-based     document.write(query(1, 3));       </script> 

Output: 

5
3


Yes! That is all. The complete implementation of the segment tree includes the query and update functions in a lower number of lines of code than the previous recursive one. Let us now understand how each of the functions works: 
 

1. The picture makes it clear that the leaf nodes are stored at i+n, so we can clearly insert all leaf nodes directly.

2. The next step is to build the tree and it takes O(n) time. The parent always has its less index than its children, so we just process all the nodes in decreasing order, calculating the value of the parent node. If the code inside the build function to calculate parents seems confusing, then you can see this code. It is equivalent to that inside the build function. 

tree[i]=tree[2*i]+tree[2*i+1]


 

3. Updating a value at any position is also simple and the time taken will be proportional to the height of the tree. We only update values in the parents of the given node which is being changed. So to get the parent, we just go up to the parent node, which is p/2 or p>>1, for node p. p^1 turns (2*i) to (2*i + 1) and vice versa to get the second child of p.

4. Computing the sum also works in O(log(n)) time. If we work through an interval of [3,11), we need to calculate only for nodes 19,26,12, and 5 in that order.


The idea behind the query function is whether we should include an element in the sum or whether we should include its parent. Let's look at the image once again for proper understanding. Consider that L is the left border of an interval and R is the right border of the interval [L,R). It is clear from the image that if L is odd, then it means that it is the right child of its parent and our interval includes only L and not the parent. So we will simply include this node to sum and move to the parent of its next node by doing L = (L+1)/2. Now, if L is even, then it is the left child of its parent and the interval includes its parent also unless the right borders interfere. Similar conditions are applied to the right border also for faster computation. We will stop this iteration once the left and right borders meet.
The theoretical time complexities of both previous implementation and this implementation is the same, but practically, it is found to be much more efficient as there are no recursive calls. We simply iterate over the elements that we need. Also, this is very easy to implement.


Time Complexities:

  • Tree Construction: O( n )
  • Query in Range: O( Log n )
  • Updating an element: O( Log n ).

Auxiliary Space: O(4*N)
 

Related Topic: Segment Tree


Next Article
Iterative Segment Tree (Range Maximum Query with Node Update)

S

Striver
Improve
Article Tags :
  • Misc
  • Advanced Data Structure
  • DSA
  • Segment-Tree
Practice Tags :
  • Advanced Data Structure
  • Misc
  • Segment-Tree

Similar Reads

    Segment Tree
    Segment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
    3 min read
    Segment tree meaning in DSA
    A segment tree is a data structure used to effectively query and update ranges of array members. It's typically implemented as a binary tree, with each node representing a segment or range of array elements. Segment tree Characteristics of Segment Tree:A segment tree is a binary tree with a leaf nod
    2 min read
    Introduction to Segment Trees - Data Structure and Algorithm Tutorials
    A Segment Tree is used to store information about array intervals in its nodes.It allows efficient range queries over array intervals.Along with queries, it allows efficient updates of array items.For example, we can perform a range summation of an array between the range L to R in O(Log n) while al
    15+ min read
    Persistent Segment Tree | Set 1 (Introduction)
    Prerequisite : Segment Tree Persistency in Data Structure Segment Tree is itself a great data structure that comes into play in many cases. In this post we will introduce the concept of Persistency in this data structure. Persistency, simply means to retain the changes. But obviously, retaining the
    15+ min read
    Segment tree | Efficient implementation
    Let us consider the following problem to understand Segment Trees without recursion.We have an array arr[0 . . . n-1]. We should be able to, Find the sum of elements from index l to r where 0 <= l <= r <= n-1Change the value of a specified element of the array to a new value x. We need to d
    12 min read
    Iterative Segment Tree (Range Maximum Query with Node Update)
    Given an array arr[0 . . . n-1]. The task is to perform the following operation: Find the maximum of elements from index l to r where 0 <= l <= r <= n-1.Change value of a specified element of the array to a new value x. Given i and x, change A[i] to x, 0 <= i <= n-1. Examples: Input:
    14 min read
    Range Sum and Update in Array : Segment Tree using Stack
    Given an array arr[] of N integers. The task is to do the following operations: Add a value X to all the element from index A to B where 0 ? A ? B ? N-1.Find the sum of the element from index L to R where 0 ? L ? R ? N-1 before and after the update given to the array above.Example: Input: arr[] = {1
    15+ min read
    Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
    Prerequisites: Segment TreeGiven a number N which represents the size of the array initialized to 0 and Q queries to process where there are two types of queries: 1 P V: Put the value V at position P.2 L R: Output the sum of values from L to R. The task is to answer these queries. Constraints: 1 ? N
    15+ min read
    Applications, Advantages and Disadvantages of Segment Tree
    First, let us understand why we need it prior to landing on the introduction so as to get why this concept was introduced. Suppose we are given an array and we need to find out the subarray Purpose of Segment Trees: A segment tree is a data structure that deals with a range of queries over an array.
    4 min read

    Lazy Propagation

    Lazy Propagation in Segment Tree
    Segment tree is introduced in previous post with an example of range sum problem. We have used the same "Sum of given Range" problem to explain Lazy propagation   How does update work in Simple Segment Tree? In the previous post, update function was called to update only a single value in array. Ple
    15+ min read
    Lazy Propagation in Segment Tree | Set 2
    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
    15+ min read
    Flipping Sign Problem | Lazy Propagation Segment Tree
    Given an array of size N. There can be multiple queries of the following types. update(l, r) : On update, flip( multiply a[i] by -1) the value of a[i] where l <= i <= r . In simple terms, change the sign of a[i] for the given range.query(l, r): On query, print the sum of the array in given ran
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences