📜  查询以检查数字是否在LR的N个范围内

📅  最后修改于: 2021-05-07 01:15:37             🧑  作者: Mango

给定N个范围和由数字组成的Q个查询。每个范围都由L和R组成。任务是检查每个查询的给定数字是否在给定范围内。

注意:没有重叠范围。

例子:

方法:下面是解决此问题的分步算法:

  1. 将每个范围的L散列为1,并将每个范围的R散列为2。
  2. 将L和R分别推入容器中。
  3. 对容器中的元素进行排序,所有范围L和R将彼此相邻,因为它们不重叠。
  4. 对于每个查询,请使用二进制搜索来查找数字等于或大于该数字的第一个匹配项。这可以使用lower_bound函数。
  5. 如果存在等于查询的任何值,则该数字与范围重叠。
  6. 如果没有等于查询的值,则检查更大的元素是否被哈希为1或2。
  7. 如果将其散列为1,则该数字不重叠,否则它确实重叠。

下面是上述方法的实现:

C++
// C++ program to check if the
// number lies in given range
#include 
  
using namespace std;
  
// Function that answers every query
void answerQueries(int a[][2], int n, int queries[], int q)
{
  
    // container to store all range
    vector v;
  
    // hash the L and R
    unordered_map mpp;
  
    // Push the element to container
    // and hash the L and R
    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 the elements in container
    sort(v.begin(), v.end());
    for (int i = 0; i < q; i++) {
  
        // each query
        int num = queries[i];
  
        // get the number same or greater than integer
        int ind = lower_bound(v.begin(), v.end(), num) - v.begin();
  
        // if it lies
        if (v[ind] == num) {
            cout << "Yes\n";
        }
  
        else {
  
            // check if greater is hashed as 2
            if (mpp[v[ind]] == 2)
                cout << "Yes\n";
  
            else // check if greater is hashed as 1
                cout << "No\n";
        }
    }
}
  
// Driver code
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]);
  
    // function call to answer queries
    answerQueries(a, n, queries, q);
  
    return 0;
}


Java
// Java program to check if the 
// number lies in given range
import java.io.*;
import java.util.*;
  
class GFG 
{
  
    // Function that answers every query
    static void answerQueries(int[][] a, int n, 
                               int[] queries, int q) 
    {
  
        // container to store all range
        Vector v = new Vector<>();
  
        // hash the L and R
        HashMap mpp = new HashMap<>();
  
        // Push the element to container
        // and hash the L and R
        for (int i = 0; i < n; i++) 
        {
            v.add(a[i][0]);
            mpp.put(a[i][0], 1);
            v.add(a[i][1]);
            mpp.put(a[i][1], 2);
        }
  
        // sort the elements in container
        Collections.sort(v);
        for (int i = 0; i < q; i++) 
        {
  
            // each query
            int num = queries[i];
  
            // get the number same or greater than integer
            int ind = lowerBound(v, num);
  
            // if it lies
            if (ind >= 0 && v.elementAt(ind) == num)
                System.out.println("Yes");
  
            else 
            {
  
                // check if greater is hashed as 2
                if (ind >= 0 && mpp.get(v.elementAt(ind)) == 2)
                    System.out.println("Yes");
  
                else // check if greater is hashed as 1
                    System.out.println("No");
            }
        }
    }
  
    // Lower bound implementation
    static int lowerBound(Vector array, int value)
    {
        int low = 0;
        int high = array.size();
        while (low < high)
        {
            final int mid = (low + high) / 2;
            if (value <= array.elementAt(mid))
            {
                high = mid;
            } 
            else
            {
                low = mid + 1;
            }
        }
        return low;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        int[][] a = {{ 5, 6 }, { 1, 3 }, { 8, 10 }};
        int n = 3;
        int[] queries = {2, 3, 4, 7};
        int q = queries.length;
  
        // function call to answer queries
        answerQueries(a, n, queries, q);
    }
}
  
// This code is contributed by
// sanjeev2552


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


C#
// C# program to check if the 
// number lies in given range
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // Function that answers every query
    static void answerQueries(int[,] a, int n, 
                            int[] queries, int q) 
    {
  
        // container to store all range
        List v = new List();
  
        // hash the L and R
        Dictionary mpp = new Dictionary();
  
        // Push the element to container
        // and hash the L and R
        for (int i = 0; i < n; i++) 
        {
            v.Add(a[i, 0]);
            if(!mpp.ContainsKey(a[i, 0]))
                mpp.Add(a[i, 0], 1);
            v.Add(a[i, 1]);
            if(!mpp.ContainsKey(a[i, 1]))
                mpp.Add(a[i, 1], 2);
        }
  
        // sort the elements in container
        v.Sort();
        for (int i = 0; i < q; i++) 
        {
  
            // each query
            int num = queries[i];
  
            // get the number same or greater than integer
            int ind = lowerBound(v, num);
  
            // if it lies
            if (ind >= 0 && v[ind] == num)
                Console.WriteLine("Yes");
  
            else
            {
  
                // check if greater is hashed as 2
                if (ind >= 0 && mpp[v[ind]] == 2)
                    Console.WriteLine("Yes");
  
                else // check if greater is hashed as 1
                    Console.WriteLine("No");
            }
        }
    }
  
    // Lower bound implementation
    static int lowerBound(List array, int value)
    {
        int low = 0;
        int high = array.Count;
        while (low < high)
        {
            int mid = (low + high) / 2;
            if (value <= array[mid])
            {
                high = mid;
            } 
            else
            {
                low = mid + 1;
            }
        }
        return low;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
  
        int[,] a = {{ 5, 6 }, { 1, 3 }, { 8, 10 }};
        int n = 3;
        int[] queries = {2, 3, 4, 7};
        int q = queries.Length;
  
        // function call to answer queries
        answerQueries(a, n, queries, q);
    }
}
  
// This code is contributed by 29AjayKumar


输出:
Yes
Yes
No
No