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
function type1 ( arr , start , limit ) { for ( let i = start ; i <= limit ; i ++ ) arr [ i ] ++ ; } function type2 ( arr , query , start , limit ) { for ( let i = start ; i <= limit ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } let n = 5 , m = 5 ; let arr = new Array ( n + 1 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } let temp = [ 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 ] ; let query = new Array ( 6 ) ; for ( let i = 0 ; i < 6 ; i ++ ) { query [ i ] = new Array ( 4 ) ; for ( let j = 0 ; j < 4 ; j ++ ) { query [ i ] [ j ] = 0 ; } } let j = 0 ; for ( let i = 1 ; i <= m ; i ++ ) { query [ i ] [ 0 ] = temp [ j ++ ] ; query [ i ] [ 1 ] = temp [ j ++ ] ; query [ i ] [ 2 ] = temp [ j ++ ] ; } for ( let i = 1 ; i <= m ; i ++ ) if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ;