Queries to calculate sum by alternating signs of array elements in a given range | Function to build the segment tree ; If current node is a leaf node of the segment tree ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree ; Update on L segment tree ; Update on R segment tree ; Find the sum from L subtree and R subtree ; Function to update elements at index pos by val in the segment tree ; If current node is a leaf node ; If current index is even ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree elements into L and R subtree ; If element lies in L subtree ; Update tree [ index ] ; Function to find the sum of array elements in the range [ L , R ] ; If start and end not lies in the range [ L , R ] ; If start and end comleately lies in the range [ L , R ] ; Stores sum from left subtree ; Stores sum from right subtree ; Driver code
def build ( tree , arr , start , end , index ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT if ( start % 2 == 0 ) : NEW_LINE INDENT tree [ index ] = arr [ start ] NEW_LINE DEDENT else : NEW_LINE INDENT tree [ index ] = - arr [ start ] NEW_LINE DEDENT return NEW_LINE DEDENT mid = start + ( end - start ) // 2 NEW_LINE build ( tree , arr , start , mid , 2 * index + 1 ) NEW_LINE build ( tree , arr , mid + 1 , end , 2 * index + 2 ) NEW_LINE tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] NEW_LINE DEDENT def update ( tree , index , start , end , pos , val ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT if ( start % 2 == 0 ) : NEW_LINE INDENT tree [ index ] = val NEW_LINE DEDENT else : NEW_LINE INDENT tree [ index ] = - val NEW_LINE DEDENT return NEW_LINE DEDENT mid = start + ( end - start ) // 2 NEW_LINE if ( mid >= pos ) : NEW_LINE INDENT update ( tree , 2 * index + 1 , start , mid , pos , val ) NEW_LINE DEDENT else : NEW_LINE INDENT update ( tree , 2 * index + 2 , mid + 1 , end , pos , val ) NEW_LINE DEDENT tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] NEW_LINE DEDENT def FindSum ( tree , start , end , L , R , index ) : 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 [ index ] NEW_LINE DEDENT mid = start + ( end - start ) // 2 NEW_LINE X = FindSum ( tree , start , mid , L , R , 2 * index + 1 ) NEW_LINE Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 ) NEW_LINE return X + Y NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE tree = [ 0 for i in range ( 4 * N + 5 ) ] NEW_LINE build ( tree , arr , 0 , N - 1 , 0 ) NEW_LINE Q = [ [ 2 , 0 , 3 ] , [ 1 , 1 , 5 ] , [ 2 , 1 , 2 ] ] NEW_LINE cntQuey = 3 NEW_LINE for i in range ( cntQuey ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT update ( tree , 0 , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( Q [ i ] [ 1 ] % 2 == 0 ) : NEW_LINE INDENT print ( FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT