Sum of product of all unordered pairs in given range with update queries | Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
def pairwiseProductSum ( a , l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( l - 1 , r , 1 ) : NEW_LINE INDENT for k in range ( j + 1 , r , 1 ) : NEW_LINE INDENT sum += ( a [ j ] * a [ k ] ) ; NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT def updateArray ( a , p , x ) : NEW_LINE INDENT a [ p - 1 ] = x NEW_LINE DEDENT def solveQueries ( a , n , Q , query ) : NEW_LINE INDENT for i in range ( Q ) : NEW_LINE INDENT if ( query [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 5 , 7 , 2 , 3 , 1 ] NEW_LINE N = len ( A ) NEW_LINE Q = 3 NEW_LINE query = [ [ 1 , 1 , 3 ] , [ 2 , 2 , 5 ] , [ 1 , 2 , 5 ] ] NEW_LINE solveQueries ( A , N , Q , query ) NEW_LINE DEDENT