LightOJ 1088 - Points in Segments (二分)
来源:互联网 发布:logo设计软件app 编辑:程序博客网 时间:2024/06/05 20:07
题目链接:http://lightoj.com/volume_showproblem.php?problem=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
Output for Sample Input
1
5 3
1 4 6 8 10
0 5
6 10
7 100000
Case 1:
2
3
2
Note
Dataset is huge, use faster I/O methods.
题目大意:n个数,然后m个区间,求各个区间在n个数中包含个数
题目解析:直接二分搜索即可
代码如下:
#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<vector>#include<set>#include<string>#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#define N 100009using namespace std;const int inf = 1e9;const int mod = 1<<30;const double eps = 1e-8;const double pi = acos(-1.0);typedef long long LL;int a[N];int main(){ int n, i, k, t; int s, e, cnt = 0; cin >> t; while(t--) { printf("Case %d:\n", ++cnt); scanf("%d%d", &n, &k); for(i = 0; i < n; i++) scanf("%d", &a[i]); a[n] = inf; for(i = 1; i <= k; i++) { scanf("%d%d", &s, &e); int f = 0, ans = lower_bound(a, a + n + 1, e) - lower_bound(a, a + n + 1, s); f = e == *lower_bound(a, a + n + 1, e) ? 1 : f; if(f) ans++; // 或者 ans = upper_bound(a, a + n, e) - lower_bound(a, a + n, s); printf("%d\n", ans); } } return 0;}
- LightOJ 1088 - Points in Segments (二分)
- Lightoj 1088 - Points in Segments 【二分】
- LightOJ - 1088 Points in Segments
- Light OJ 1088 Points in Segments-二分(水)
- lighoj 1088 Points in Segments | 二分
- LightOJ--1089 - Points in Segments (II)
- LOJ 1088 Points in Segments
- [线段树+离散化]LightOJ 1089 - Points in Segments (II)
- LightOJ 1089 Points in Segments (II) 离散化 标记
- Points in Segments (II)(线段树)
- c++二分法简单应用(Points in Segments)
- 二分法应用--light oj1088 Points in Segments
- LightOJ 1112 Curious Robin Hood && LightOJ 1266 Points in Rectangle(树状数组)
- [树状数组]LightOJ 1266 - Points in Rectangle
- lightoj 1138 (二分)
- CF 121D Lucky Segments (two points)
- 【CodeForces429E】Points and Segments(欧拉回路)
- codeforces_652D. Nested Segments(树状数组、二分)
- 多指标综合评价方法汇总
- Cows
- MVP架构下Android的Rxjava与Retrofit 结合
- hdu3394Railway
- 总结
- LightOJ 1088 - Points in Segments (二分)
- 代码规范
- 26. Remove Duplicates from Sorted Array(移除有序数组中的重复元素)
- android studio 第一个真机调试
- 程序员的绘图利器 — Graphviz
- Solr-----2、Solr后台管理配置登录权限
- 进程
- Debug Assertion Failed! Expression: _pFirstBlock == pHead
- scikit-learn学习笔记(1)---安装和通用线性模型