Array range queries over range queries | Function to execute type 1 query ; incrementing the array by 1 for type 1 queries ; Function to execute type 2 query ; If the query is of type 1 function call to type 1 query ; If the query is of type 2 recursive call to type 2 query ; Input size of array amd number of queries ; Build query matrix ; Perform queries ; printing the result
def type1 ( arr , start , limit ) : NEW_LINE INDENT for i in range ( start , limit + 1 ) : NEW_LINE INDENT arr [ i ] += 1 NEW_LINE DEDENT DEDENT def type2 ( arr , query , start , limit ) : NEW_LINE INDENT for i in range ( start , limit + 1 ) : NEW_LINE INDENT if ( query [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT elif ( query [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT DEDENT n = 5 NEW_LINE m = 5 NEW_LINE arr = [ 0 for i in range ( n + 1 ) ] NEW_LINE temp = [ 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 ] NEW_LINE query = [ [ 0 for i in range ( 3 ) ] for j in range ( 6 ) ] NEW_LINE j = 0 NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT query [ i ] [ 0 ] = temp [ j ] NEW_LINE j += 1 NEW_LINE query [ i ] [ 1 ] = temp [ j ] NEW_LINE j += 1 NEW_LINE query [ i ] [ 2 ] = temp [ j ] NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( query [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT elif ( query [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT