Queries to update array elements in a range [ L , R ] to satisfy given conditions | Function to prthe array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] into arr [ ] ; Driver Code ; Given array ; Size of the array ; Stores count of query ; Function Call
def printArray ( arr , N ) : NEW_LINE INDENT print ( * arr ) NEW_LINE DEDENT def modifyArray ( arr , N , Q , cntQuery ) : NEW_LINE INDENT arr1 = [ 0 for i in range ( N + 2 ) ] NEW_LINE arr2 = [ 0 for i in range ( N + 2 ) ] NEW_LINE for i in range ( cntQuery ) : NEW_LINE INDENT L = Q [ i ] [ 0 ] + 1 NEW_LINE R = Q [ i ] [ 1 ] + 1 NEW_LINE arr1 [ L ] += 1 NEW_LINE arr1 [ R + 1 ] -= 1 NEW_LINE arr2 [ R + 1 ] -= R - L + 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr1 [ i ] += arr1 [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr [ i - 1 ] = arr2 [ i ] NEW_LINE DEDENT printArray ( arr , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ [ 1 , 3 ] , [ 0 , 1 ] ] NEW_LINE cntQuery = len ( Q ) NEW_LINE modifyArray ( arr , N , Q , cntQuery ) NEW_LINE DEDENT