Queries to calculate sum of squares of ASCII values of characters of a substring with updates |  ; 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
using System ; public class treeNode { public int square_sum ; public treeNode ( int square_sum ) { this . square_sum = square_sum ; } } ; public class GFG { 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 . GetLength ( 0 ) ; i ++ ) { if ( Q [ i , 0 ] == " S " ) { int L = Int32 . Parse ( Q [ i , 1 ] ) ; int R = Int32 . Parse ( Q [ i , 2 ] ) ; Console . WriteLine ( querySquareSum ( tree , 0 , n - 1 , 1 , L , R ) ) ; } else if ( Q [ i , 0 ] == " U " ) { int I = Int32 . Parse ( Q [ i , 1 ] ) ; updateTree ( s , tree , 0 , n - 1 , 1 , I , Q [ i , 2 ] [ 0 ] ) ; } } } static public void Main ( ) { string S = " geeksforgeeks " ; string [ , ] Q = { { " S " , "0" , "2" } , { " S " , "1" , "2" } , { " U " , "1" , " a " } , { " S " , "0" , "2" } , { " S " , "4" , "5" } } ; PerformQuery ( S , Q ) ; } }