Build query matrix
for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } let query = [ [ 1 , 1 , 2 ] , [ 1 , 4 , 5 ] , [ 2 , 1 , 2 ] , [ 2 , 1 , 3 ] , [ 2 , 3 , 4 ] ] ; let record = new Array ( m ) ; for ( let i = 0 ; i < record . length ; i ++ ) { record [ i ] = 0 ; } for ( let i = m - 1 ; i >= 0 ; i -- ) {