france

https://github.com/francecil

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxN = 100005;
int a[maxN];
int t, tt;
int n, q, x, y;

int Bsearch_lower_bound(int x)
{
    int l = 0, r = n - 1, mid = 0;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (a[mid] < x) l = mid + 1;
        else r = mid - 1;
    }
    return l;
}

int Bsearch_upper_bound(int x)
{
    int l = 0, r = n - 1, mid = 0;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (a[mid] <= x) l = mid + 1;
        else r = mid - 1;
    }
    return l;
}

void solve()
{
    scanf("%d%d", &n, &q);
    printf("Case %d:\n", ++tt);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < q; i++)
    {
        scanf("%d%d", &x, &y);
        int l = Bsearch_lower_bound(x);
        int r = Bsearch_upper_bound(y);
        printf("%d\n", r - l);
    }
}

int main()
{
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2014-08-05 14:45  france  阅读(200)  评论(0编辑  收藏  举报