Queries to check if a number lies in N ranges of L | Python program to check if the number lies in given range ; Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; check if greater is hashed as 1 ; Driver Code ; function call to answer queries
from bisect import bisect_left as lower_bound NEW_LINE def answerQueries ( a : list , n , queries : list , q ) : NEW_LINE INDENT v = list ( ) NEW_LINE mpp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT v . append ( a [ i ] [ 0 ] ) NEW_LINE mpp [ a [ i ] [ 0 ] ] = 1 NEW_LINE v . append ( a [ i ] [ 1 ] ) NEW_LINE mpp [ a [ i ] [ 1 ] ] = 2 NEW_LINE DEDENT v . sort ( ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT num = queries [ i ] NEW_LINE ind = lower_bound ( v , num ) NEW_LINE if v [ ind ] == num : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT if mpp [ v [ ind ] ] == 2 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ [ 5 , 6 ] , [ 1 , 3 ] , [ 8 , 10 ] ] NEW_LINE n = 3 NEW_LINE queries = [ 2 , 3 , 4 , 7 ] NEW_LINE q = len ( queries ) NEW_LINE answerQueries ( a , n , queries , q ) NEW_LINE DEDENT