HNU2019 Summer Training 4 E.Fishermen

Problem Description

The ocean can be represented as the first quarter of the Cartesian plane. There are n fish in the ocean. Each fish has its own coordinates. There may be several fish at one point.

There are also m fishermen. Each fisherman has its own x-coordinate. The y-coordinate of each fisherman is equal to 0.

Each fisherman has a fishing rod of length l. Therefore, he can catch a fish at a distance less than or equal to l. The distance between a fisherman in position x and a fish in position (a, b) is |a − x| + b.

Find for each fisherman how many fish he can catch.

Input

The first line contains three integers nnn, mmm, and l(1≤n,m≤2⋅105,1≤l≤109)l (1 ≤ n, m ≤ 2 · 10^5, 1 ≤ l ≤ 10^9)l(1n,m2105,1l109) — the number of fish, the number of fishermen, and the length of the fishing rod, respectively.

Each of the next nnn lines contains two integers xix_ixi and yi(1≤xi,yi,≤109)y_i (1 ≤ x_i, y_i, ≤ 10^9)yi(1xi,yi,109) — the fish coordinates.

Next line contains mmm integers ai(1≤ai≤109)a_i (1 ≤ a_i ≤ 10^9)ai(1ai109) — the fishermen coordinates.

Output

For each fisherman, output the number of fish that he can catch, on a separate line.

Sample Input

8 4 4
7 2
3 3
4 5
5 1
2 2
1 4
8 4
9 4
6 1 4 9

Sample Output

2
2
3
2

Note


The picture illustrates for the above example the area on which the third fisherman can catch fish.

Solution:

每个鱼都有自己会被捕获的区间,对于每个渔夫,找到自己落在多少个区间内即可。
一开始二重循环找,附带了一点优化(优化循环区间),以为能过,结果超时了

后来又想到了一种nlogn 的方法,对于每个渔夫做一次二分查找,再做一次减法就能找到区间数
具体见代码

AC Code:

#include <bits/stdc++.h>

using namespace std;
int const maxn = 200005;
int ans[maxn] = {0};
int leftArr[maxn];
int rightArr[maxn];
int intervalCount = 0;

int main() {
    int n, m, l;
    scanf("%d%d%d", &n, &m, &l);
    for (int i = 0; i < n; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        if (y > l)
            continue;
        int foo = l - y;
        leftArr[intervalCount] = max(0, x - foo);
        rightArr[intervalCount++] = x + foo + 1;
    }
    sort(leftArr, leftArr + intervalCount);
    sort(rightArr, rightArr + intervalCount);
    for (int i = 0; i < m; ++i) {
        int foo;
        scanf("%d", &foo);
        unsigned long ub = upper_bound(rightArr, rightArr + intervalCount, foo) - rightArr;
        unsigned long lb = upper_bound(leftArr, leftArr + intervalCount, foo) - leftArr;
        ans[i] += lb - ub;
    }
    for (int i = 0; i < m; ++i)
        printf("%d\n", ans[i]);

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值