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; 
}
posted on 2017-07-23 14:08 mthoutai 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/7224547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值