CF BUDDY
← Problems·

1971E · Find the Car

1500 · binary search, math, sortings

Problem: Timur is in a car traveling on the number line from point 00 to point nn. The car starts moving from point 00 at minute 00.

There are k+1k+1 signs on the line at points 0,a1,a2,,ak0, a_1, a_2, \dots, a_k, and Timur knows that the car will arrive there at minutes 0,b1,b2,,bk0, b_1, b_2, \dots, b_k, respectively. The sequences aa and bb are strictly increasing with ak=na_k = n.

Between any two adjacent signs, the car travels with a constant speed. Timur has qq queries: each query will be an integer dd, and Timur wants you to output how many minutes it takes the car to reach point dd, rounded down to the nearest integer.

Input Format: The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains three integers nn, kk, and qq, (kn109k \leq n \leq 10^9; 1k,q1051 \leq k, q \leq 10^5) — the final destination, the number of points Timur knows the time for, and the number of queries respectively.

The second line of each test case contains kk integers aia_i (1ain1 \leq a_i \leq n; ai<ai+1a_i < a_{i+1} for every 1ik11 \leq i \leq k-1; ak=na_k = n).

The third line of each test case contains kk integers bib_i (1bi1091 \leq b_i \leq 10^9; bi<bi+1b_i < b_{i+1} for every 1ik11 \leq i \leq k-1).

Each of the following qq lines contains a single integer dd (0dn0 \leq d \leq n) — the distance that Timur asks the minutes passed for.

The sum of kk over all test cases doesn't exceed 10510^5, and the sum of qq over all test cases doesn't exceed 10510^5.

Output Format: For each query, output a single integer — the number of minutes passed until the car reaches the point dd, rounded down.

Note: For the first test case, the car goes from point 00 to point 1010 in 1010 minutes, so the speed is 11 unit per minute and:

  • At point 00, the time will be 00 minutes.
  • At point 66, the time will be 66 minutes.
  • At point 77, the time will be 77 minutes.

For the second test case, between points 00 and 44, the car travels at a speed of 11 unit per minute and between 44 and 1010 with a speed of 22 units per minute and:

  • At point 66, the time will be 55 minutes.
  • At point 44, the time will be 44 minutes.
  • At point 22, the time will be 22 minutes.
  • At point 77, the time will be 5.55.5 minutes, so the answer is 55.

For the fourth test case, the car travels with 1.21.2 units per minute, so the answers to the queries are:

  • At point 22, the time will be 1.661.66\dots minutes, so the answer is 11.
  • At point 66, the time will be 55 minutes.
  • At point 55, the time will be 4.164.16\dots minutes, so the answer is 44.

Sample Cases

Case 1

Input

4
10 1 3
10
10
0
6
7
10 2 4
4 10
4 7
6
4
2
7
1000000000 1 1
1000000000
1000000000
99999999
6 1 3
6
5
2
6
5

Output

0 6 7 
5 4 2 5 
99999999 
1 5 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback