Queries to calculate sum of array elements consisting of odd number of divisors | C ++ program for the above approach ; Function to get the middle index from the given ranges ; Recursive function to find the sum of values in the given range of the array ; If segment of this node is a part of given range , then return the sum of the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps the given range ; Function to find the sum of elements in the range from index qs ( querystart ) to qe ( query end ) ; Invalid ranges ; Recursive function to construct the Segment Tree for array [ ss . . se ] . si is index of current node in tree st ; If there is one element in the array ; Recur for left and right subtrees and store the sum of values in this node ; Function to construct segment tree from the given array ; Allocate memory for the segment tree Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Fill the allocated memory st ; Return the constructed segment tree ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Traverse the array , arr [ ] ; Replace elements that are not perfect squares with 0 ; Build segment tree from the given array ; Iterate through all the queries ; Print sum of values in array from index l to r ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } int getSumUtil ( int * st , int ss , int se , int qs , int qe , int si ) { if ( qs <= ss && qe >= se ) return st [ si ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return getSumUtil ( st , ss , mid , qs , qe , 2 * si + 1 ) + getSumUtil ( st , mid + 1 , se , qs , qe , 2 * si + 2 ) ; } int getSum ( int * st , int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { cout << " Invalid ▁ Input " ; return -1 ; } return getSumUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; } int constructSTUtil ( int arr [ ] , int ss , int se , int * st , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return arr [ ss ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ; return st [ si ] ; } int * constructST ( int arr [ ] , int n ) { int x = ( int ) ( ceil ( log2 ( n ) ) ) ; int max_size = 2 * ( int ) pow ( 2 , x ) - 1 ; int * st = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } void OddDivisorsSum ( int n , int q , int arr [ ] , vector < pair < int , int > > Query ) { for ( int i = 0 ; i < n ; i ++ ) { int sq = sqrt ( arr [ i ] ) ; if ( sq * sq != arr [ i ] ) arr [ i ] = 0 ; } int * st = constructST ( arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = Query [ i ] . first ; int r = Query [ i ] . second ; cout << getSum ( st , n , l , r ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 4 , 5 , 6 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int Q = 3 ; vector < pair < int , int > > Query = { { 0 , 2 } , { 1 , 3 } , { 1 , 4 } } ; OddDivisorsSum ( N , Q , arr , Query ) ; return 0 ; }