【二分答案】Problem A:天堂_珍珠

Problem A:天堂_珍珠

Time Limit:5000MS  Memory Limit:65536K
Total Submit:232 Accepted:106

Description

我有很多很多(n条)用魔法合成的珍珠项链……(其实神仙比凡人更爱美),每天起来我都要从中挑一条戴上……挑哪条很有讲究,如果比情敌**的难看,那么就会被**(-_-),如果比天后Hera的好看,那么就完蛋了(-_-)。所以我希望你能帮帮我,解决这个令人头疼的问题——每天帮我算算,那天我能戴的项链有多少条。

Input

第一行为正整数n(项链总条数)。 
第二行有n个整数(代表每条项链晶的好看程度Xi,0<=Xi<=maxlongint。) 
第三行为正整数m,表示总天数(也就是总询问次数)。 
以下m行,每行两个整数Ai,Bi(1<=Ai,Bi<=maxlongint),询问好看程度在Ai到Bi之间的项链条数(含等于Ai或Bi的,Ai与Bi大小关系不确定)。 

Output

输出m行,对于每次询问输出一行,从Ai到Bi(含Ai,Bi)好看程度在Ai到Bi之间的项链条数。

Sample Input

7
8 2 3 5 6 7 7
6
1 5
8 6
1 10
5 5
4 4
7 8

Sample Output

3
4
7
1
0
3

Hint

对于25%数据,有m,n<=1000。 
对于100%数据,有m,n<=100000。 

#include <algorithm>  
#include <cstdio>
#include <iostream>
using namespace std;
int n,a1,b,b1;
long long  a[1000010];
int find(int a1)
{
    int low=0,high=n+1;
    while(low+1<high)
    {
        int mid=(low+high)/2;
        if(a[mid]<a1) low=mid;
        else high=mid;
    }
   return low;
}
int find2(int a1)
{
      int low=0,high=n+1;
    while(low+1<high)
    {
        int mid=(low+high)/2;
        if(a[mid]<=a1) low=mid;
        else high=mid;
    }
   return low;
}
int main()
{
   cin>>n;
    for(int i=1; i<=n; i++) scanf("%d",&a[i]); 
    sort(a+1,a+1+n);
    a[0]=-1;
    a[n+1]=1000001;
    cin>>b;
    for(int i=1;i<=b;i++)
    {
        cin>>a1>>b1;
        if(b1<a1) swap(b1,a1);
        cout<<find2(b1)-find(a1)<<endl;
    } 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/thj0305/p/9419549.html