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 <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

using namespace std;

int n, q;
int a[100010];
int x, y;
int ok;

int query(int t,int &ok)
{
    ok = 0;
    int left = 1;
    int right = n;
    int mid;
    while (left <= right)
    {
        int mid = (left + right) / 2;
        if (a[mid] >= t)
        {
            if (a[mid] == t)
                ok = 1;
            right = mid - 1;
        }
        else
            left = mid + 1;
    }
    return left;
}

int main()
{
    int t;
    scanf("%d",&t);
    int cases = 1;
    while (t--)
    {
        scanf("%d%d",&n,&q);
        for (int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        printf("Case %d:\n", cases++);
        while (q--)
        {
            scanf("%d%d", &x, &y);
            int ok1 = 0, ok2 = 0, tmp = 0;
            int t1 = query(x,ok1);
            int t2 = query(y,ok2);
            if (ok2) tmp = 1;
            //printf("      %d %d\n",t1,t2);
            printf("%d\n", t2 - t1 + tmp);
        }
    }
    return 0;
}

版权声明:转载请注明出处。

时间: 2024-08-06 07:55:02

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个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边界问题 /** @Date : 2016-12-17-19.03 * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : */ #include<bits/stdc++.h> #defin

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