Multiplication on Array : Range update query in O ( 1 ) | Creates a mul [ ] array for A [ ] and returns it after filling initial values . ; Does range update ; Prints updated Array ; Driver code ; ; Array to be updated ; Create and fill mul and div Array
def initialize ( mul , div , size ) : NEW_LINE INDENT for i in range ( 1 , size ) : NEW_LINE INDENT mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; NEW_LINE DEDENT DEDENT def update ( l , r , x , mul , div ) : NEW_LINE INDENT mul [ l ] *= x ; NEW_LINE div [ r + 1 ] *= x ; NEW_LINE DEDENT def printArray ( ar , mul , div , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT ar [ i ] = ar [ i ] * mul [ i ] ; NEW_LINE print ( int ( ar [ i ] ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 10 , 5 , 20 , 40 ] ; NEW_LINE n = len ( ar ) ; NEW_LINE mul = [ 0 ] * ( n + 1 ) ; NEW_LINE div = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT mul [ i ] = div [ i ] = 1 ; NEW_LINE DEDENT update ( 0 , 1 , 10 , mul , div ) ; NEW_LINE update ( 1 , 3 , 20 , mul , div ) ; NEW_LINE update ( 2 , 2 , 2 , mul , div ) ; NEW_LINE initialize ( mul , div , n + 1 ) ; NEW_LINE printArray ( ar , mul , div , n ) ; NEW_LINE DEDENT