Sprinklers

4.1

9 votes
Problem

There are N sprinklers in a field. Each sprinkler has some range up to where it can sprinkle water.
All the sprinklers are on the X-axis at coordinates (X1,0), (X2,0), ...,(XN,0) and their respective ranges are R1, R2, R3,...,RN meaning that the ith sprinkler can sprinkle water from [XiRi, Xi+Ri] inclusive.
There is a big wall at 0 meaning that the water can not go another side irrespective of range.

You are asked Q queries and in the ith query, you will be given an integer K. Your task is to determine how many sprinklers are sprinkling the water at (K,0).
Assume, there is no sprinkler at (0,0) and there is no query that has K=0.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two integers N and Q denoting the number of sprinklers and number of queries.
  • The second line of each test case contains N space-separated integers denoting the X-coordinates of the sprinklers.
  • The third line of each test case contains N space-separated integers denoting the range of the sprinklers.
  • Next Q line of each test case contains an integer K for the query.

Output format

For each test case, print Q lines denoting the number of sprinklers that sprinkle water at the position in the ith query.

Constraints

1T10000

1N100000

1QN

1|Xi|N

0RiN

2NK2N

The sum of N over all test cases do not exceed 100000.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 3 sprinklers which sprinkle water at -3 which are at (-2,-1,-3). Note that sprinker at co-ordinate x=1 has range upto -4 but there is a big wall at x=0.
Similar is the case with -2 and -6.
There are two sprinklers at (1,2) which can sprinkle water at 5.
 

Editor Image

?