氵题解
初步审题,很容易就能想到枚举每条鱼,找到能钓到这一条鱼的渔夫最靠左与最靠右的渔夫的位置,直接枚举,时间复杂度为O(n^2)
但对于线性的枚举很容易就可以想到排序+二分
由于蒟蒻不会重载运算符,所以手打排序
基本思路确定了,那么怎样确定能钓到每一条鱼的渔夫最靠左与最靠右的渔夫的位置
由此,我们可以推出 若鱼的坐标为 (x , y),则可以钓到第i条鱼的渔夫最左边可能在 x ± \pm ± (l-y)
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=2*1e5;
struct node {
int x,y;
} fish[maxn+5];//鱼的位置
int ans[maxn+5],num[maxn+5],c[maxn+5],fisher[maxn+5];
//num[i]表示实际位置为i的点是当前在num[i] , c[i]表示i点实际编号为c[i]
//fisher表示每一个渔夫的位置
inline void rd(int &rex) {//读优
rex=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
rex=rex*10+c-'0',c=getchar();
}
void swap(int &x,int &y) {//交换
int t=x;
x=y,y=t;
}
void qsort(int l,int r) {//手写快排
int i=l,j=r,mid=fisher[(l+r)/2],p;
while(i<=j) {
while(fisher[i]<mid)i++;
while(fisher[j]>mid)j--;
if(i<=j) {
swap(fisher[i],fisher[j]);
swap(c[i],c[j]);
i++,j--;
}
}
if(l<j)qsort(l,j);
if(i<r)qsort(i,r);
}
int main() {
int n,m,l;
rd(n);
rd(m);
rd(l);
for(int i=1; i<=n; i++) {
rd(fish[i].x);
rd(fish[i].y);
}
for(int i=1; i<=m; i++)
rd(fisher[i]),c[i]=i;
qsort(1,m);//使渔夫的位置单调递增,满足二分的条件
for(int i=1; i<=n; i++) {
if(fish[i].y>l)continue;
int head=-(l-fish[i].y)+fish[i].x,tail=l-fish[i].y+fish[i].x;
head=lower_bound(fisher+1,fisher+1+m,head)-fisher;
tail=upper_bound(fisher+1,fisher+1+m,tail)-fisher;
ans[head]++,ans[tail]--;
}
for(int i=1; i<=m; i++)
ans[i]+=ans[i-1],num[c[i]]=i;
for(int i=1; i<=m; i++)
printf("%d\n",ans[num[i]]);
return 0;
}