Queries to calculate sum of squares of ASCII values of characters of a substring with updates | Java 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class treeNode { int square_sum ; treeNode ( int square_sum ) { this . square_sum = square_sum ; } } ; static void buildTree ( char s [ ] , treeNode tree [ ] , int start , int end , int treenode ) { if ( start == end ) { tree [ treenode ] . square_sum = ( int ) Math . 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 ; } static 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 ; } static void updateTree ( char 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 = ( int ) Math . 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 ; } static void PerformQuery ( String S , String Q [ ] [ ] ) { int n = S . length ( ) ; treeNode tree [ ] = new treeNode [ ( 4 * n ) + 1 ] ; for ( int i = 0 ; i <= ( 4 * n ) ; i = i + 1 ) { tree [ i ] = new treeNode ( 0 ) ; } char s [ ] = S . toCharArray ( ) ; buildTree ( s , tree , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == " S " ) { int L = Integer . parseInt ( Q [ i ] [ 1 ] ) ; int R = Integer . parseInt ( Q [ i ] [ 2 ] ) ; System . out . println ( querySquareSum ( tree , 0 , n - 1 , 1 , L , R ) ) ; } else if ( Q [ i ] [ 0 ] == " U " ) { int I = Integer . parseInt ( Q [ i ] [ 1 ] ) ; updateTree ( s , tree , 0 , n - 1 , 1 , I , Q [ i ] [ 2 ] . charAt ( 0 ) ) ; } } } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; String Q [ ] [ ] = { { " S " , "0" , "2" } , { " S " , "1" , "2" } , { " U " , "1" , " a " } , { " S " , "0" , "2" } , { " S " , "4" , "5" } } ; PerformQuery ( S , Q ) ; } }