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(1≤n,m≤2⋅105,1≤l≤109) — 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(1≤xi,yi,≤109) — the fish coordinates.
Next line contains mmm integers ai(1≤ai≤109)a_i (1 ≤ a_i ≤ 10^9)ai(1≤ai≤109) — 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;
}