stl二分用法

来源:互联网 发布:ubuntu ant安装 编辑:程序博客网 时间:2024/05/16 18:38

在数组排好序的情况下,求数组中 在 x 到 y 区 间 里 有 多 少 个 数 ?

样例代码如下:

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int a[1000];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i = 0;i < n;i ++)        scanf("%d",&a[i]);        sort(a,a + n);        int x,y,v = 5;        while(v --)        {            scanf("%d %d",&x,&y);            if(x > y)            swap(x,y);            printf("%d\n",upper_bound(a,a + n,y) - lower_bound(a,a + n,x));        }    }    return 0;}

大笑