LightOJ 1088 Points in Segments 二分查找

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.

题解

这个不是二分答案的那种题了,是比较正常的那种集合区间中的那种元素查找的二分题目了。题意很简单,给n个有序的数,这些数分布在一个坐标轴上。给q次查找,询问在区间[x, y](这里的符号和代码的保持一致)中有多少个数据。

解法就是自己写一个二分函数upper_bound和lower_bound,然后直接计算区间即可。

代码示例

/****
	*@author    Shen
	*@title     LightOj 1088
	*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxN = 100005;
int a[maxN];
int t, tt;
int n, q, x, y;

int Bsearch_lower_bound(int x)
{
    int l = 0, r = n - 1, mid = 0;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (a[mid] < x) l = mid + 1;
        else r = mid - 1;
    }
    return l;
}

int Bsearch_upper_bound(int x)
{
    int l = 0, r = n - 1, mid = 0;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (a[mid] <= x) l = mid + 1;
        else r = mid - 1;
    }
    return l;
}

void solve()
{
    scanf("%d%d", &n, &q);
    printf("Case %d:\n", ++tt);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < q; i++)
    {
        scanf("%d%d", &x, &y);
        int l = Bsearch_lower_bound(x);
        int r = Bsearch_upper_bound(y);
        printf("%d\n", r - l);
    }
}

int main()
{
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}

LightOJ 1088 Points in Segments 二分查找

时间: 2024-07-29 07:03:59

LightOJ 1088 Points in Segments 二分查找的相关文章

LightOJ 1088 - Points in Segments 二分

http://www.lightoj.com/volume_showproblem.php?problem=1088 题意:给出N个点,Q个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边界问题 /** @Date : 2016-12-17-19.03 * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : */ #include<bits/stdc++.h> #defin

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include

LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化

http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询的是点,考虑将其离线并离散化,普通线段树即可. /** @Date : 2016-12-17-20.49 * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : */ #include<bi

lightoj-1088 - Points in Segments(二分法)

1088 - Points in Segments PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBGiven 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

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 seg

LightOJ 1307 Counting Triangles 二分查找

[题解整理]二分题 题目类型: 二分查找: 二分答案. 大致解题思路: 查找注意有序和返回值: 浮点数注意精度: 整数注意返回值,建议另外维护一个变量,用于储存可行解. 题目 分类 传送门 WA点 poj 2785 二分查找 题解 lightoj 1088 二分查找 题解 lightoj 1307 二分查找 题解 longlong poj 2456 整数二分答案 题解 poj 3104 整数二分答案 题解 poj 3258 整数二分答案 题解 poj 3273 整数二分答案 题解 lightoj

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

codeforces 895B XK Segments 二分 思维

codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\(k\)满足:\[a_i \le k \le a_j\] 思路: 整体数组排序,对于当前\(a_i\)寻找符合条件的\(a_j\)的最大值和最小值 有:\[(a_i-1)/x+k=a_j/x\] 所以可以通过二分查找获得,这里我寻找\(((a_i-1)/x+k)*x\)为下界,\(((a_i-1)/x