GreedyXploit's blog

By GreedyXploit, history, 4 years ago, In English

QUESTION LINK

#include <iostream>
#include<algorithm>
using namespace std;
int binarySearch(int left , int right , int a[] , int n)
{
    int c = 0;
    for(int i = left; i<=right ; i++)
    {
        int l = -1;
        int r = n;
        while(r>l+1)
        {
            int mid = (l+r)/2;
            if(a[mid] >= i)
            {
                r = mid;
                //cout<<a[r]<<"\n";
            }

            else
                l = mid;

        }

        if(r<n && a[r] == i)
        {
            ++c;
            if(count(a , a + n , i) > 1)
                c = c+(count(a , a + n , i)-1);

        }

    }
    return c;
}

int main()
{
    int n ;
    cin>>n;
    int a[n];
    for(int i = 0 ; i<n;i++)
        cin>>a[i];
    int k;
    cin>>k;
    sort(a , a+n);
    for(int i = 0 ; i<k;i++)
    {
        int l , r;
        cin>>l>>r;
        cout<<binarySearch(l , r , a , n)<<"\n";
    }

    return 0;
}

  • Vote: I like it
  • -7
  • Vote: I do not like it

| Write comment?