二分lower_bound()与upper_bound()的运用
<span style="color:#6633ff;">/*
G - 二分
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status
Description
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 Hint
Dataset is huge, use faster I/O methods.
By Grant Yuan
2014.7.16
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int t;
long long low,high,mid,ans;
int n,m;
int a[100005];
long long x,y;
int ct=1;
int main()
{int l,r;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m); for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Case %d:\n",ct++);
while(m--){
scanf("%d%d",&x,&y);
if(x<a[0]) l=0;
else {l=lower_bound(a,a+n,x)-a;
}
if(y>a[n-1]) r=n;
else {r=upper_bound(a,a+n,y)-a;
}
printf("%d\n",r-l);}}
return 0;
}
</span>
二分lower_bound()与upper_bound()的运用的更多相关文章
- LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- 二分搜素——(lower_bound and upper_bound)
因为每个人二分的风格不同,所以在学习二分的时候总是被他们的风格搞晕.有的人二分风格是左闭右开也就是[L,R),有的人是左开右闭的(L,R]. 二分的最基本条件是,二分的序列需要有单调性. 下面介绍的时 ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- hdu 5178(二分-lower_bound,upper_bound)
pairs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
随机推荐
- windows mac配置host方法
配置host方法如下: 1.windows 系统配置host (1)打开电脑的系统盘(一般默认为C盘):C盘 -> Windows -> System32 -> drives -&g ...
- Centos6.8 安装mongo3.6以及权限配置和开启外网链接
目录 安装环境和版本说明,以及参考文档链接 安装MongoDB数据库 运行MongoDB数据库 删除卸载MongoDB 配置MongoDB管理员用户 修改配置文件,允许外网链接 安装配置完成,使用Ro ...
- html页面加载初始化方法
js: 方法一: window.onload=function(){内容} 方法二(自己定义方法): function onload(){内容} onload(); jQuery: 方法一: $(do ...
- 程序包javax.servlet.http不存在
在maven test项目时,出现错误: java:[7,26] 程序包javax.servlet.http不存在 原因:pom.xml中未引入javax.servlert-api相关的包 <d ...
- LeetCode(43)Multiply Strings
题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- 集训第六周 数学概念与方法 概率 F题
Submit Status Description Sometimes some mathematical results are hard to believe. One of the common ...
- [1143] [CTSC2008]祭祀river(最大独立集 || 偏序集最大反链)
传送门 网上说这是偏序集最大反链,然而我实在不理解. 所以我换了一个思路,先用floyd,根据点的连通性连边, 问题就转换成了找出最多的点,使任意两个点之间不连边,也就是最大独立集. ——代码 #in ...
- node.js 读取文件--createReadStream
createReadStream 是fs模块里面读流的一个方法 这个方法基于fs模块的,所以我们先要引进fs模块 let fs=require("fs"); createReadS ...
- Redis 命令与连接【十一】
---------------------Redis 命令--------------- Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客 ...
- 利用mysql分析小规模数据
1 获取数据 示例:(/home/work/data/1.data) 123457,chenli,70 123458,liuyang,71 2 create table CREATE TABLE sc ...