Light oj 1030 二分查找

1088 - Points in Segments

  PDF (English) Statistics Forum
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;
}

时间: 2024-08-01 12:36:05

Light oj 1030 二分查找的相关文章

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够

Light oj 1030 概率DP

D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1030 Description You are in a cave, a long cave! The cave can be represented by a 1

Light OJ 1030 - Discovering Gold(期望)

1030 - Discovering Gold PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in

[水+期望dp] light oj 1030 Discovering Gold

题意: 给n个点,每个点都有一个财宝. 你从1这个点开始出发,假设你在i这个点,每次随机走1~min(6,n-i)步. 每到达一个点就拿走财宝. 问最后拿到财宝的期望. 思路: 水的题目. dp[n]=v[n] 然后逐个往前推. 就是注意一下步数是 1~min(6,n-i) 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #inc

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

二分查找二维数组

转载请注明出处:http://blog.csdn.net/ns_code/article/details/24977113 剑指offer上的第一道题目,在九度OJ上测试通过 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和列数

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>

【剑指offer】二分查找二维数组

转载请注明出处:http://blog.csdn.net/ns_code/article/details/24977113 剑指offer上的第三道题目.在九度OJ上測试通过 题目描写叙述: 在一个二维数组中,每一行都依照从左到右递增的顺序排序.每一列都依照从上到下递增的顺序排序.请完毕一个函数,输入这种一个二维数组和一个整数,推断数组中是否含有该整数. 输入: 输入可能包括多个測试例子,对于每一个測试案例, 输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的矩阵的行数和