题目链接:115.157.200.87/front/problem?problemId=1056(HNU内网访问)

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 n, m, and 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 n lines contains two integersxi​ and yi(1≤xi​,yi​,≤109) — the fish coordinates.

Next line contains m integers ai (1 ≤ ai ≤ 10^9) — 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.

题意:

n条鱼,m个渔夫,钓鱼线长l,渔夫的位置是(0,aj),和鱼(xi,yi)的距离是|ai-xi|+yi,问每个渔夫最多能钓到多少条鱼。(要能钓到鱼需要钓鱼线长大于他们间的距离)

题解:

对于鱼(xi,yi),很显然,如果yi大于l那么那个渔夫都钓不上;否则的话,每个鱼在x轴上能被钓到的区间为[xi-l+yi]。转化成一个区间问题:对于每个aj落在多少个区间内。

用left[i]表示第i个可能能被钓上的鱼在x轴上的左边界,right[i]为右边界。从小到大排序后利用二分:

找出第一个右边界大于等于aj的序号lb,说明左边的[0,lb-1]共lb个区间不可能包含aj;

找出第一个左边界大于aj的序号rb,说明右边的[rb,cnt-1]共cnt-rb个区间不可能包含aj;

对于每一个aj,能落的区间个数即是cnt-(cnt-rb)-lb=rb-lb;(cnt为可能被钓上的鱼的个数)

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=2e5+5;
 4 const int MAXM=2e5+5;
 5 int Left[MAXN],Right[MAXN];
 6 int a[MAXM];
 7 int read()
 8 {
 9     int s=1,x=0;
10     char ch=getchar();
11     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
12     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
13     return x*s;
14 }
15 int main()
16 {
17     int n=read(),m=read(),l=read();
18     int xi,yi,cnt=0;
19     for(int i=1;i<=n;++i)
20     {
21         xi=read(),yi=read();
22         if(yi>l) continue;
23         Left[++cnt]=xi-l+yi;
24         Right[cnt]=xi+l-yi;
25     }
26     sort(Left+1,Left+cnt+1);
27     sort(Right+1,Right+cnt+1);
28     int lb,ub,ai;
29     queue<int> ans;
30     for(int i=1;i<=m;++i)
31     {
32         ai=read();
33         lb=lower_bound(Right+1,Right+cnt+1,ai)-Right;
34         ub=upper_bound(Left+1,Left+cnt+1,ai)-Left;
35         ans.push(ub-lb);
36     }
37     while(!ans.empty())
38     {
39         cout<<ans.front()<<"\n";
40         ans.pop();
41     }
42 }

View Code

转载于:https://www.cnblogs.com/wangzhebufangqi/p/11269451.html

HOJ1056 Fishermen(区间问题、思维)相关推荐

  1. 【蓝桥杯 - 练习】k倍区间(思维,数组)

    题干: http://lx.lanqiao.cn/problem.page?gpid=T444 问题描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1 ...

  2. 【CodeForces - 827A】String Reconstruction(并查集合并区间,思维)

    题干: Ivan had string s consisting of small English letters. However, his friend Julia decided to make ...

  3. 寒假每日一题(提高组)【Week 2 完结】

    目录 1230. K倍区间[思维 前缀和] 1613. 数独简单版[dfs] 122. 糖果传递[贪心] 125. 耍杂技的牛[贪心] 499. 聪明的质监员[二分+前缀和] 503. 借教室[二分+ ...

  4. 洛谷or牛客数据结构+算法

    栈思想:先进后出 tips:栈里能放下标就放下标 (牛客)小c的计事本(直接用stack可以简化代码,且不会被自己绕晕,当时没意识到) (牛客)吐泡泡(没意识到用栈),(牛客)好串 1.后缀表达式(栈 ...

  5. 刷题记录:牛客NC15447wyh的问题

    传送门:牛客 题目描述: 我国现在能源消耗非常严重,现在政府有这样一个工作,每天早上都需要把一些路灯关掉,但是他们想 让在关闭的过程中所消耗的能源是最少的,负责路灯关闭的工作人员以1m/s的速度进行行 ...

  6. 狂补dp Atcoder Educational DP Contest(全)

    狂补dp Atcoder Educational DP Contest(全) 注意 为了减少篇幅,本博客代码部分只放主代码部分,其余省略快读.快输.头文件等代码,即把代码看做伪代码更佳 A - Fro ...

  7. 第十一篇:欧赔与亚盘在运用中各自的优劣

    很希望能从自己的思维空间里,找到一个更好的比喻,来表述欧赔与亚盘的关系,暂且将它们比作刀叉与筷子的关系吧.用刀叉吃饭,你会觉得分外别扭,这已经不是习惯的问题,而是适不适合的问题了.欧洲人,大概一定是先 ...

  8. 洛谷 - P3358 最长k可重区间集问题(最大费用最大流+思维建边)

    题目链接:点击查看 题目大意:给出n个开区间,现在要求从中选取一定数量的区间,需要满足: 对于任意点x,所选取的区间中包含点x的个数小于等于k 区间长度和最大 要求输出最长的区间长度和 题目分析:一开 ...

  9. Sasha and a Bit of Relax K倍区间 (前缀和异或 前缀和计数 思维)

    (29条消息) CodeForces - 1109A Sasha and a Bit of Relax(思维+异或和,好题)_Frozen_Guardian的博客-CSDN博客 Sasha and a ...

  10. P4766 [CERC2014]Outer space invaders——思维+区间dp

    题目传送门 思路 这道题思维的转换真的很妙,第 i 个外星人出现的时间区间是 [ai,bi][a_i,b_i][ai​,bi​] ,距离为 did_idi​ , 我们可以以时间区间为横坐标,距离区间为 ...

最新文章

  1. 编写高性能Java代码的最佳实践
  2. 南大和中大“合体”拯救手残党:基于GAN的PI-REC重构网络,“老婆”画作有救了 | 技术头条...
  3. A novel evolutionary model for constructing gene coexpression networks with comprehensive features
  4. linux 内核参数 max_map_count 进程可拥有的虚拟内存区域数量 简介
  5. 如何迭代pandas dataframe的行
  6. 1013: C语言程序设计教程(第三版)课后习题6.3
  7. 搞懂深浅拷贝JavaScript内存之栈和堆
  8. Mysql默认隔离级别为什么是可重复读?
  9. boost::mpl::equal_to相关的测试程序
  10. 单行溢出 和多行溢出
  11. flink java生成流式数据
  12. 【疑点】当子元素全部浮动时,怎么解决父元素塌陷的问题?
  13. 用简单的实例来实践TDD的核心思想
  14. matlab编译桁架有限元计算(附有完整代码)
  15. html渐变显示动画效果,css3实现背景动态渐变效果
  16. oracle中imp命令详解,Oracle中imp命令详解
  17. 关于eclipse工作空间打不开问题
  18. 高等代数 具有度量的线性空间(第10章)2 实内积空间
  19. 一步一步学Spring Boot(一)
  20. 引用参考文献标准格式

热门文章

  1. 【图像隐藏】基于matlab像素预测和位平面压缩的加密图像可逆数据隐藏【含Matlab源码 2218期】
  2. linux死机,Linux 死机了怎么办
  3. 如何把自己打造成为一名金领架构师-前言
  4. 数据挖掘 顶级期刊_澳大利亚麦考瑞大学, 国际数据挖掘顶级期刊ACM TKDD副主编招收5名数据挖掘全奖PhD...
  5. 【全国第二批】供应链创新与应用示范企业和示范城市申报材料条件内容认定好处费用时间
  6. 以太网(报头格式 | MTU | ARP)
  7. 模拟题【枚举计数】咒语
  8. 苏大计算机专业就业如何,苏州大学好就业吗?附苏州大学就业率最高的专业名单...
  9. android代码禁用软键盘,Android 禁用软键盘
  10. 【Win7开启DMA】