LightOj 1088 - Points in Segments (二分枚举)
题目链接;
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 = ;
cin >> T;
while (T --)
{
int n, q; scanf ("%d %d", &n, &q); for (int i=; 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 ;
}
/*
4
30 40 10
12.619429 8.163332 3
10 10 3
10 10 1
*/
LightOj 1088 - Points in Segments (二分枚举)的更多相关文章
- LightOJ 1088 - Points in Segments 二分
http://www.lightoj.com/volume_showproblem.php?problem=1088 题意:给出N个点,Q个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边 ...
- Lightoj 1088 - Points in Segments 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个. 思路:寻 ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- uva 12587 二分枚举
思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
随机推荐
- mysql有哪几种索引
从数据结构角度 1. B+树索引(O(log(n))) 2. hash索引 3. FULLTEXT索引 4. R-Tree索引 从物理存储角度 1. 聚集索引 2. 非聚集索引 从逻辑角度 ...
- 简单的dp hdu 数塔(水题)
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- run as maven test报错解决办法
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- These interactions can be expressed as complicated, large scale graphs. Mining data requires a distributed data processing engine
https://databricks.com/blog/2014/08/14/mining-graph-data-with-spark-at-alibaba-taobao.html
- 手游服务器php架构比较
从swoole项目开始到现在,一直有人在问这个问题.今天来抽空讲一下它.为什么swoole非要使用纯C来写而不是PHP代码来实现,核心的原因有2点: 1. PHP无法直接调用操作系统API 如send ...
- group by where having 联合使用
having子句与where有相似之处但也有区别,都是设定条件的语句.在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先执行.而where子句在查询过程中执行优 ...
- 正则表达式pattern的匹配格式
0> 匹配 -------------------------------------------------------------------------------- (pattern) ...
- iframe引入页面
将外层css框架单独存放在一个css文件之中,将iframe的css单独写在一个css文件,避免iframe中的html,body等公共部分的样式冲突. 外层框架单独写在一个css,如: frame. ...
- UILabel与UIFont的用法和属性的一些总结
初始化一个UILabel对象,并初始化大小 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100) ...
- js 中继承的几种方式
继承的方式一共有三种: 一.原型继承 通过prototype 来实现继承. function Person(name,age) { this.name=name; this.age=age; } ...