Sprinklers
Practice
4.1 (9 votes)
Prefix
Dynamic programming
Algorithms
Introduction to dynamic programming 1
Problem
90% Success 1847 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(i^{th}\) sprinkler can sprinkle water from \([Xi-Ri,\ 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 \(i^{th}\) 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 \(i^{th}\) query.

Constraints

\(1 \leq T \leq 10000\)

\(1 \leq N \leq 100000\)

\(1 \leq Q \leq N\)

\(1 \le |X_i| \le N\)

\(0 \leq R_i \leq N\)

\(-2N \leq K \leq 2N\)

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

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
Easy
Points:30
13 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingEasy
Points:30
97 votes
Tags:
Dynamic ProgrammingEasyOpenRecursion