Points in Segments LightOJ - 1088
-
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
题目大意
- 给出一些点以及一些区间,要求在每一个区间内找到出现了的点的个数。
思路
- 直接利用upper__ bound(返回大于value的第一个元素位置)以及lower__ bound(返回大于等于value的第一个位置)函数求得所给区间在有序数列当中的上届与下界作差即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
long long int aa[100005];
int cnt;
int main(){
int n;
cin>>n;
while(n){
n--;
cnt++;
int a,b;
cin>>a>>b;
for(int i = 0;i<a;i++){
cin>>aa[i];
}
cout<<"Case "<<cnt<<":"<<endl;
int l,h;
for(int j = 0;j<b;j++){
cin>>l>>h;
cout<<(upper_bound(aa,aa+a,h)-lower_bound(aa,aa+a,l))<<endl;
}
memset(aa,0,sizeof(aa));
}
}