Light oj 1030 二分查找
1088 - Points in Segments
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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.
题目大意:数据量非常大,直接搞肯定就搞死了,明显二分查找,调用lower__bound和upper__bound就可以。
输出注意要用标准输入输出,否则超时gg.
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=100000+100;
int a[maxn];
int main()
{
int kase=0;
int T;
int n,p;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&p);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Case %d:\n",++kase);
while(p--)
{
int c,d;
scanf("%d%d",&c,&d);
int t1=upper_bound(a,a+n,d)-a;
int t2=lower_bound(a,a+n,c)-a;
printf("%d\n",t1-t2);
}
}
return 0;
}
Light oj 1030 二分查找的更多相关文章
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
- Light OJ 1030 - Discovering Gold
题目大意: 给你一个1*N的方格,你初始位置是在1,给你一个骰子,假设你现在的位置是X,你投掷一个骰子掷的点数是y, 那么你的新位置就是 X+y, 并且你可以得到新位置的宝藏.假如X+y > N ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- 九度OJ 1349 数字在排序数组中出现的次数 -- 二分查找
题目地址:http://ac.jobdu.com/problem.php?pid=1349 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数 ...
- 【大视野入门OJ】1083:数组的二分查找
Description 在1500个整数中查整数x的位置,这些数已经从小到大排序了.若存在则输出其位置,若不存在则输出-1. Input 第一行,一个整数x 后面1500行,每行一个整数 Output ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
- leetcode二分查找问题整理
自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...
随机推荐
- UVA10090 数论基础 exgcd
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- WinForm(C#)自定义控件之——RoundButton(圆形按钮)
最近需要做一个圆形的按钮,去CodeProject找了一下,发现有现成的可用,但不能完全满足我的需求.因此自己试着利用WinForm中的自定义组件功能,制作一个圆形按钮.圆形按钮小制作即将开始之前,先 ...
- HTML5屏幕适配标签设置
开发HTML5游戏中,我们常用的一些mata标签: <meta name="viewport" content="width=device-width, initi ...
- php基础之三 数组
一.正则表达式: 1. "/"代表界定符, "^"代表开始符号 "&"结束符号 eg: $reg="/(13[0-9] ...
- 恢复root用户目录,及~目录
普通帐号登su;mkdir /root;chown root:root /root cp -R /etc/skel/.[!.]* ./
- ThinkPHP内置函数详解D、F、S、C、L、A、I
单字母函数D.F.S.C.L.A.I 他们都在ThinkPHP核心的ThinkPHP/Mode/Api/functions.php这个文件中定义. 下面我分别说明一下他们的功能: D() 加载Mode ...
- About abstract class.
Abstract means should be realized. Virtual means could be overrided. It is very different!
- TextField控件详解2
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- MySQL 优化方案
基本上通过索引来解决 . 通常索引键在where , group by , order by 相关的列 一个表只能用一个索引(查询的时候)所以当要执行复杂查询时最好使用联合索引就是 index (a, ...
- 一直想测试的NGINX变量输出,最于有办法了。
参考URL: http://blog.ailms.me/2013/08/04/nginx-server_name-and-host-difference.html 要是可能在正则测试及REWRITE就 ...