您的位置:首页 > 其它

LightOj 1088 - Points in Segments (二分枚举)

2016-04-20 15:28 232 查看
题目链接;

  http://www.lightoj.com/volume_showproblem.php?problem=1088

题目描述:

  给出一个n位数升序排列的数列,然后q个查询,每个查询问指定的区间覆盖了数列中几个数?

解题思路:

  二分枚举区间的起始点和终点在数列中的位置。

  upper_bound() 返回数列中第一个大于所查询数的位置,或者没有大于所查询的数返回数列长度(越界)

  lower_bound() 返回数列中第一个等于或者大于所查询数的位置,或者没有等于,大于所查询数返回数列长度(越界)

  哦,对对对!还有噢,不能用cin, cout输入,会TLE的,亲试~

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

#define LL long long
#define maxn 100010
#define esp 1e-12
#define PI acos(-1.0)

int a[maxn];

int main ()
{
int T, L = 1;
cin >> T;
while (T --)
{
int n, q;

scanf ("%d %d", &n, &q);

for (int i=0; i<n; i++)
scanf ("%d", &a[i]);
//sort (a, a+n);

int x, y;
printf ("Case %d:\n", L++);
while (q --)
{
scanf ("%d %d", &x, &y);
int ans = upper_bound (a, a+n, y) - a;
ans -= lower_bound (a, a+n, x) - a;

printf ("%d\n", ans);
}
}
return 0;
}
/*
4
30 40 10
12.619429 8.163332 3
10 10 3
10 10 1
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: