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代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...
随机推荐
- keypress和keydown事件及keyCode和keyChar
二:keypress和keydown事件: 现只在IE10.chrome(版本 32.0.1700.107 m).firefox(25.0.1)中测试了. IE8 chrome firefox $ ...
- MapDB:专为Java设计的高性能的数据库
MapDB是一个快速.易用的嵌入式Java数据库引擎,它提供了基于磁盘或者堆外(off-heap允许Java直接操作内存空间, 类似于C的malloc和free)存储的并发的Maps.Sets.Que ...
- Jetty9开发(1)
Version: 9.2.14.v20151106 Jetty : 开发文档 jetty的官网:http://www.eclipse.org/jetty/ Jetty : 开发文档 目录 I. je ...
- 解决ie6显示透明图的问题
在我们设置png透明图片时,其他浏览器都显示很正常,唯独只有ie6看着不是透明的状态. 第一种办法是:单独设置ie6的样式.例: _background: none; _filter:progid:D ...
- Linux_Shell符号及各种解释对照表
Shell符号及各种解释对照表: Shell符号 使用方法及说明 # 注释符号(Hashmark[Comments]) 1.在shell文件的行首,作为shebang标记,#!/bin/bash; 2 ...
- [每日一题JS] 正则表达式
判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /\b[a-zA-Z]{1}[a-zA-Z0-9_]{4,19}\b/; var fl ...
- composer在ubuntu下安装
通过curl下载composer安装包 curl -sS https://getcomposer.org/installer | php 将安装包移动到全局安装文件夹下 mv composer.pha ...
- 如何看懂XDEBUG+WEBGRIND?(转)
看到一个很有用的东东,收藏.. http://blog.csdn.net/yukon12345/article/details/11408617 ~~~~~~~~~~ 使用: ...
- POJ 1755 Triathlon
http://poj.org/problem?id=1755 题意:铁人三项,每个人有自己在每一段的速度,求有没有一种3条路线长度都不为0的设计使得某个人能严格获胜? 我们枚举每个人获胜,得到不等式组 ...
- T-SQL函数类别统计