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 二分查找的更多相关文章

  1. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  2. Light oj 1030 概率DP

    D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768 ...

  3. 概率DP light oj 1030

    t组数据 n块黄金 到这里就捡起来 出发点1 到n结束  点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6  如果满6个的话 否则 ...

  4. Light OJ 1030 - Discovering Gold

    题目大意: 给你一个1*N的方格,你初始位置是在1,给你一个骰子,假设你现在的位置是X,你投掷一个骰子掷的点数是y, 那么你的新位置就是 X+y, 并且你可以得到新位置的宝藏.假如X+y > N ...

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

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  6. 九度OJ 1349 数字在排序数组中出现的次数 -- 二分查找

    题目地址:http://ac.jobdu.com/problem.php?pid=1349 题目描述: 统计一个数字在排序数组中出现的次数. 输入: 每个测试案例包括两行: 第一行有1个整数n,表示数 ...

  7. 【大视野入门OJ】1083:数组的二分查找

    Description 在1500个整数中查整数x的位置,这些数已经从小到大排序了.若存在则输出其位置,若不存在则输出-1. Input 第一行,一个整数x 后面1500行,每行一个整数 Output ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. leetcode二分查找问题整理

    自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...

随机推荐

  1. curl抓取信息

    <?php $hotel = new curl(false,0); $str = $hotel -> post("http://www.todayinns.com/login.p ...

  2. PHP 中const 与define 区别

    1.const用于类成员变量定义,一旦定义且不能改变其值.define定义全局常量,在任何地方都可以访问. 2.define不能在类中定义而const可以. 3.const不能在条件语句中定义常量 i ...

  3. C语言格式化输入输出函数

    一:格式输出函数printf() 1.调用形式一般为:printf("格式化控制字符串",输出表列): 2.格式化控制字符串用于指定输出格式,它有三种形式: 1.格式说明符:规定了 ...

  4. Request对象 --web浏览器向web服务端的请求

    一]Request对象常用方法        1)StringBuffer getRequestURL()            url表示访问web应用的完整路径            2)Stri ...

  5. linux系统时间和硬件时钟问题(date和hwclock)

    http://blog.chinaunix.net/uid-182041-id-3464524.html http://blog.csdn.net/duyiwuer2009/article/detai ...

  6. cf B. Levko and Permutation

    http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...

  7. AIX和Linux中wtmp的不同处理方式

    wtmp 记录用户登录和退出事件.它和utmp日志文件相似,但它随着登陆次数的增加,它会变的越来越大,有些系统的ftp访问也在这个文件里记录,同时它也记录正常的系统退出时间,可以用ac和last命令访 ...

  8. jquery 实现全选反选

    jquery代码 $(function () { $('#inputCheck').click(function () { if ($(this).attr("checked")) ...

  9. bzoj3038 上帝造题的七分钟2

    Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对 ...

  10. 淡淡de馨香---职业尊严

    大学四年时光匆匆而过,仿佛一切都不曾远去,那种熟悉又激动的感觉好似就在昨天,但是.一切都仅仅是一种感觉,在现实面前没有谁能够逆天,在被遗忘的什么时候,曾经是想着无数的梦想与自定义,同样也是在被遗忘的什 ...