Queries to check if a number lies in N ranges of L | C ++ 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 ; else check if greater is hashed as 1 ; Driver code ; function call to answer queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void answerQueries ( int a [ ] [ 2 ] , int n , int queries [ ] , int q ) { vector < int > v ; unordered_map < int , int > mpp ; for ( int i = 0 ; i < n ; i ++ ) { v . push_back ( a [ i ] [ 0 ] ) ; mpp [ a [ i ] [ 0 ] ] = 1 ; v . push_back ( a [ i ] [ 1 ] ) ; mpp [ a [ i ] [ 1 ] ] = 2 ; } sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < q ; i ++ ) { int num = queries [ i ] ; int ind = lower_bound ( v . begin ( ) , v . end ( ) , num ) - v . begin ( ) ; if ( v [ ind ] == num ) { cout << " Yes STRNEWLINE " ; } else { if ( mpp [ v [ ind ] ] == 2 ) cout << " Yes STRNEWLINE " ; cout << " No STRNEWLINE " ; } } } int main ( ) { int a [ ] [ 2 ] = { { 5 , 6 } , { 1 , 3 } , { 8 , 10 } } ; int n = 3 ; int queries [ ] = { 2 , 3 , 4 , 7 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; answerQueries ( a , n , queries , q ) ; return 0 ; }