LightOJ1088 (二分查找)

题解

二分

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100000
#define LL long long
int cas=1,T;
int a[100005];
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        printf("Case %d:\n",cas++);
        int n,q;
        scanf("%d%d",&n,&q);
        for (int i = 0;i<n;i++)
            scanf("%d",&a[i]);
        for (int i = 0;i<q;i++)
        {
            int lr,rr;
            scanf("%d%d",&lr,&rr);
            printf("%d\n",upper_bound(a,a+n,rr)-upper_bound(a,a+n,lr-1));
        }
    }
    //freopen("in","r",stdin);
    //scanf("%d",&T);
    //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
    return 0;
}

题目

Description

Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B.

For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there are 2 points that lie in the segment.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points in ascending order. All the integers are distinct and each of them range in [0, 108].

Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.

Output

For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.

Sample Input

1
5 3
1 4 6 8 10
0 5
6 10
7 100000

Sample Output

Case 1:
2
3
2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值