二分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 ...
随机推荐
- 跨平台字符编码转换GBK、UTF8
#if (defined _WIN32 || defined _WIN64) # include <windows.h> # include <stdio.h> # inclu ...
- Python之爬虫-中国大学排名
Python之爬虫-中国大学排名 #!/usr/bin/env python # coding: utf-8 import bs4 import requests from bs4 import Be ...
- Spring核心技术(六)——Spring中Bean的生命周期
前文已经描述了Bean的作用域,本文将描述Bean的一些生命周期作用,配置还有Bean的继承. 定制Bean 生命周期回调 开发者通过实现Spring的InitializeingBean和Dispos ...
- Lucene_Hello(示例)
(1)创建project (2)导入Lucene的核心包 (3)编写代码建立索引 /lucene01/src/cn/hk/lucene/TestIndex.java: package cn.hk.lu ...
- [BZOJ2667][cqoi2012]模拟工厂
[BZOJ2667][cqoi2012]模拟工厂 试题描述 有一个称为“模拟工厂”的游戏是这样的:在时刻0,工厂的生产力等于1.在每个时刻,你可以提高生产力或者生产商品.如果选择提高生产力,在下一个时 ...
- 舒适的路线(codevs 1001)
题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤ ...
- Quartz进一步学习与使用
一.再思考 了解Quartz.NET的基本使用方法了.但如果想方便的知道某个作业执行情况,需要暂停,启动等操作行为,这时候就需要个Job管理的界面,如何才能达到我们想到的效果,查看相关Quartz.n ...
- JSP内置对象和EL内置对象
JSP共有九大内置对象: (1) HttpSession类的session对象作用:主要用于来分别保存每个用户信息,与请求关联的会话: 会话状态维持是Web应用开发者必须面对的问题. ...
- linux 文件系统 磁盘分区 格式化
1.du -sh test #查看文件或者目录的大小 2.cat file | wc -l #查看文件的行数 3.ls dirname | wc -l #查看文件个数 4.stat install.l ...
- 289. Game of Live
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...