Queries to calculate sum of squares of ASCII values of characters of a substring with updates | C ++ implementation of the above approach ; Structure of a node of a Segment Tree ; Function to construct the Segment Tree ; If start and end are equal ; Assign squares of positions of the characters ; Stores the mid value of the range [ start , end ] ; Recursive call to left subtree ; Recursive call to right subtree ; Update the current node ; Function to perform the queries of type 2 ; No overlap ; If l <= start and r >= end ; Return the value of treeNode ; Calculate middle of the range [ start , end ] ; Function call to left subtree ; Function call to right subtree ; Return the sum of X and Y ; Function to perform update queries on a Segment Tree ; If start is equal to end and idx is equal to start ; Base Case ; Calculate middle of the range [ start , end ] ; If idx <= mid ; Function call to left subtree ; Otherwise ; Function call to the right subtree ; Update the current node ; Function to perform the given queries ; Stores the segment tree ; Traverse the segment tree ; Assign 0 to each node ; Builds segment tree ; Traverse the query array Q [ ] [ ] ; If query is of type S ; Stores the left boundary ; Stores the right boundary ; Prints the sum of squares of the alphabetic positions of the characters ; Otherwise ; Stores the index of the character to be updated ; Update the segment tree ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct treeNode { int square_sum ; } ; void buildTree ( string s , treeNode * tree , int start , int end , int treeNode ) { if ( start == end ) { tree [ treeNode ] . square_sum = pow ( s [ start ] - ' a ' + 1 , 2 ) ; return ; } int mid = start + ( ( end - start ) / 2 ) ; buildTree ( s , tree , start , mid , 2 * treeNode ) ; buildTree ( s , tree , mid + 1 , end , 1 + 2 * treeNode ) ; tree [ treeNode ] . square_sum = tree [ ( 2 * treeNode ) ] . square_sum + tree [ ( 2 * treeNode ) + 1 ] . square_sum ; } int querySquareSum ( treeNode * tree , int start , int end , int treeNode , int l , int r ) { if ( ( l > end ) || ( r < start ) ) { return 0 ; } if ( ( l <= start ) && ( r >= end ) ) { return tree [ treeNode ] . square_sum ; } int mid = start + ( ( end - start ) / 2 ) ; int X = querySquareSum ( tree , start , mid , 2 * treeNode , l , r ) ; int Y = + querySquareSum ( tree , mid + 1 , end , 1 + 2 * treeNode , l , r ) ; return X + Y ; } void updateTree ( string s , treeNode * tree , int start , int end , int treeNode , int idx , char X ) { if ( ( start == end ) && ( idx == start ) ) { s [ idx ] = X ; tree [ treeNode ] . square_sum = pow ( X - ' a ' + 1 , 2 ) ; return ; } int mid = start + ( ( end - start ) / 2 ) ; if ( idx <= mid ) { updateTree ( s , tree , start , mid , ( 2 * treeNode ) , idx , X ) ; } else { updateTree ( s , tree , mid + 1 , end , ( 2 * treeNode ) + 1 , idx , X ) ; } tree [ treeNode ] . square_sum = tree [ ( 2 * treeNode ) ] . square_sum + tree [ ( 2 * treeNode ) + 1 ] . square_sum ; } void PerformQuery ( string S , vector < vector < string > > Q ) { int n = S . size ( ) ; treeNode * tree = new treeNode [ ( 4 * n ) + 1 ] ; for ( int i = 0 ; i <= ( 4 * n ) ; i = i + 1 ) { tree [ i ] . square_sum = 0 ; } buildTree ( S , tree , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q [ i ] [ 0 ] == " S " ) { int L = stoi ( Q [ i ] [ 1 ] ) ; int R = stoi ( Q [ i ] [ 2 ] ) ; cout << querySquareSum ( tree , 0 , n - 1 , 1 , L , R ) << endl ; } else if ( Q [ i ] [ 0 ] == " U " ) { int I = stoi ( Q [ i ] [ 1 ] ) ; updateTree ( S , tree , 0 , n - 1 , 1 , I , Q [ i ] [ 2 ] [ 0 ] ) ; } } } int main ( ) { string S = " geeksforgeeks " ; vector < vector < string > > Q = { { " S " , "0" , "2" } , { " S " , "1" , "2" } , { " U " , "1" , " a " } , { " S " , "0" , "2" } , { " S " , "4" , "5" } } ; PerformQuery ( S , Q ) ; }