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>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

int a[N];
int query(int x, int y, int n)//二分细节阿阿阿 边界问题
{
    int p1 = -1, p2 = -1;

    int l = 0, r = n - 1;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        //cout << "l1:" < a[mid])
            l = mid + 1;
    }

    l = 0, r = n - 1;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        //cout << "l2:" <= a[mid])
            l = mid + 1, p2 = mid;
    }
    if(!(~p1) || !(~p2))//
        return 0;
    else
        return p2 - p1 + 1;
}

int main()
{
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        int n, q;
        scanf("%d%d", &n, &q);
        for(int i = 0; i < n; i++)
            scanf("%d", a + i);

        printf("Case %d:\n", ++cnt);
        while(q--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            int ans = query(x, y, n);
            printf("%d\n", ans);
        }
    }
    return 0;
}
时间: 2024-07-29 07:03:52

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

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 segme

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

Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point{ int index, pos; Point(int index_ = 0, int pos_ = 0){ index = index_; pos = pos_; } bool operator < (const Point& a) const{ return p

LightOJ 1076 Get the Containers 二分答案

1076 - Get the Containers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB A conveyor belt has a number of vessels of different capacities each filled to brim with milk. The milk from conveyor belt is to be filled into 'm' c

Codeforces 430A Points and Segments (easy)

题意:让你染色点,要求在给出的区间内|红色个数-蓝色个数|<=1 思路:排序后依次交替染色就能达到效果 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 110; int arr[MAXN]; int n,m,x,y; int

【CF429E】Points and Segments 欧拉回路

[CF429E]Points and Segments 题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能是+1,-1或0. $n\le 10^5$ 题解:首先,我们用扫描线,整个数轴被分成若干个小区间.对于一个小区间,如果有偶数条线段包含它,则它的权值只能是0,否则可以是+1/-1.我们可以在所有权值为+1/-1的小区间处人为的增加一条线段,这样的话我们只需要让所有小区间权值都是0就行了. 嗯...每

ACM ——Points in Segments

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 t