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 equa ; 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
class treeNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . square_sum = x NEW_LINE DEDENT DEDENT def buildTree ( s , tree , start , end , treeNode ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT tree [ treeNode ] . square_sum = pow ( ord ( s [ start ] ) - ord ( ' a ' ) + 1 , 2 ) NEW_LINE return NEW_LINE DEDENT mid = start + ( ( end - start ) // 2 ) NEW_LINE buildTree ( s , tree , start , mid , 2 * treeNode ) NEW_LINE buildTree ( s , tree , mid + 1 , end , 1 + 2 * treeNode ) NEW_LINE tree [ treeNode ] . square_sum = ( tree [ ( 2 * treeNode ) ] . square_sum + tree [ ( 2 * treeNode ) + 1 ] . square_sum ) NEW_LINE DEDENT def querySquareSum ( tree , start , end , treeNode , l , r ) : NEW_LINE INDENT if ( ( l > end ) or ( r < start ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( ( l <= start ) and ( r >= end ) ) : NEW_LINE INDENT return tree [ treeNode ] . square_sum NEW_LINE DEDENT mid = start + ( ( end - start ) // 2 ) NEW_LINE X = querySquareSum ( tree , start , mid , 2 * treeNode , l , r ) NEW_LINE Y = + querySquareSum ( tree , mid + 1 , end , 1 + 2 * treeNode , l , r ) NEW_LINE return X + Y NEW_LINE DEDENT def updateTree ( s , tree , start , end , treeNode , idx , X ) : NEW_LINE INDENT if ( ( start == end ) and ( idx == start ) ) : NEW_LINE INDENT s [ idx ] = X NEW_LINE tree [ treeNode ] . square_sum = pow ( ord ( X ) - ord ( ' a ' ) + 1 , 2 ) NEW_LINE return NEW_LINE DEDENT mid = start + ( ( end - start ) // 2 ) NEW_LINE if ( idx <= mid ) : NEW_LINE INDENT updateTree ( s , tree , start , mid , ( 2 * treeNode ) , idx , X ) NEW_LINE DEDENT else : NEW_LINE INDENT updateTree ( s , tree , mid + 1 , end , ( 2 * treeNode ) + 1 , idx , X ) NEW_LINE DEDENT tree [ treeNode ] . square_sum = ( tree [ ( 2 * treeNode ) ] . square_sum + tree [ ( 2 * treeNode ) + 1 ] . square_sum ) NEW_LINE DEDENT def PerformQuery ( S , Q ) : NEW_LINE INDENT n = len ( S ) NEW_LINE tree = [ treeNode ( 0 ) for i in range ( ( 4 * n ) + 1 ) ] NEW_LINE for i in range ( 4 * n + 1 ) : NEW_LINE INDENT tree [ i ] . square_sum = 0 NEW_LINE DEDENT buildTree ( S , tree , 0 , n - 1 , 1 ) NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == " S " ) : NEW_LINE INDENT L = int ( Q [ i ] [ 1 ] ) NEW_LINE R = int ( Q [ i ] [ 2 ] ) NEW_LINE print ( querySquareSum ( tree , 0 , n - 1 , 1 , L , R ) ) NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == " U " ) : NEW_LINE INDENT I = int ( Q [ i ] [ 1 ] ) NEW_LINE updateTree ( S , tree , 0 , n - 1 , 1 , I , Q [ i ] [ 2 ] [ 0 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE Q = [ [ " S " , "0" , "2" ] , [ " S " , "1" , "2" ] , [ " U " , "1" , " a " ] , [ " S " , "0" , "2" ] , [ " S " , "4" , "5" ] ] NEW_LINE PerformQuery ( [ i for i in S ] , Q ) NEW_LINE DEDENT