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的,亲试~
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 */
相关文章推荐
- cronolog分割tomcat的catalina.out日志
- lucene学习之基础searcher排序
- Linux Tomcat远程调试
- python 爬取煎蛋网妹子的图片
- 如何避免愚蠢的勤奋--摘自罗辑思维
- 如何实现两个View间 4000 的跳转
- JSON.parse()和JSON.stringify()
- linux ftpusers和user_list 区别
- mongoDB数据导出
- log4j.properties文件自动加载
- jQuery学习笔记
- ubuntu Linux离线安装软件包
- 有关static静态方法知识的收集
- 第一阶段站立会议2
- mysql 生成排名字段
- Ant :DataType
- centos 登陆不进去了
- 【教程】第 10 章 基于UDP协议的接收和发送
- iOS播放器之基于VLCKit的自定义播放器
- 四类NoSQL数据库适用场景总结