Chocolate problem

3.3

3 votes
Fast Fourier transform, Advanced Algorithms
Problem

Raju likes the number ‘p’ alot. He want to buy only one chocolate and only one candy. Each candy and chocolate has a price associated with it. He will only buy a pair of chocolate and candy if the sum of numbers associated with them equals ‘p’. He is wondering, how many pairs he has to choose from? Since Raju is busy in finding her girlfriend, can you help him out?

Input:

The first line contains an integer ‘t’ the number of test cases.

In the next line each test case contains ‘n’ and ‘m’ , where ‘n’ is the number of chocolates and ‘m’ is the number of candies.

The next line contains the prices which are associated with the chocolates and the next line contains the prices which are associated with the candies.

Finally the last line contains ‘q’ which are the total number of queries. Each query contains number ‘p’ in the next line.

Output:

For each query print the total ways(pairs). No need to print the pairs just the count of them.

Constraints:

1<=T<=10

1<=n<=m<=10^6

1<=chocolates[i]<=10^9

1<=candies[i]<=10^9

1<=q<=10^6

1<=p<=2*10^6

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

First query ‘p’=8 answer is 2 ,as he can choose the pairs (4,4) and (2,6).

Second query ‘p’=5 answer is 2, as only pair is (3,2)and (4,1) .

Third query ‘p’=9 answer is 2 , as he can choose the pairs (3,6) , (8,1) and (7,2).

(chocolates,candies).

 

Editor Image

?