On a strip of land of length n there are k air conditioners: the i-th air conditioner is placed in cell a_i (1 ≤ a_i ≤ n). Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct).
Each air conditioner is characterized by one parameter: temperature. The i-th air conditioner is set to the temperature t_i.
Example of strip of length n=6, where k=2, a=[2,5] and t=[14,16].
For each cell i (1 ≤ i ≤ n) find it's temperature, that can be calculated by the formula min1≤j≤k(tj
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
where |a_j - i| denotes absolute value of the difference a_j - i.
In other words, the temperature in cell i is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell i.
Let's look at an example. Consider that n=6, k=2, the first air conditioner is placed in cell a_1=2 and is set to the temperature t_1=14 and the second air conditioner is placed in cell a_2=5 and is set to the temperature t_2=16. In that case temperatures in cells are:
temperature in cell 1 is: min(14 + |2 - 1|, 16 + |5 - 1|)=min(14 + 1, 16 + 4)=min(15, 20)=15;
temperature in cell 2 is: min(14 + |2 - 2|, 16 + |5 - 2|)=min(14 + 0, 16 + 3)=min(14, 19)=14;
temperature in cell 3 is: min(14 + |2 - 3|, 16 + |5 - 3|)=min(14 + 1, 16 + 2)=min(15, 18)=15;
temperature in cell 4 is: min(14 + |2 - 4|, 16 + |5 - 4|)=min(14 + 2, 16 + 1)=min(16, 17)=16;
temperature in cell 5 is: min(14 + |2 - 5|, 16 + |5 - 5|)=min(14 + 3, 16 + 0)=min(17, 16)=16;
temperature in cell 6 is: min(14 + |2 - 6|, 16 + |5 - 6|)=min(14 + 4, 16 + 1)=min(18, 17)=17.
For each cell from 1 to n find the temperature in it.
Input
The first line contains one integer q (1 ≤ q ≤ 10^4) — the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line.
Each test case contains three lines. The first line contains two integers n (1 ≤ n ≤ 3 ⋅ 10^5) and k (1 ≤ k ≤ n) — the length of the strip of land and the number of air conditioners respectively.
The second line contains k integers a_1, a_2, …, a_k (1 ≤ a_i ≤ n) — positions of air conditioners on the strip of land.
The third line contains k integers t_1, t_2, …, t_k (1 ≤ t_i ≤ 10^9) — temperatures of air conditioners.
It is guaranteed that the sum of n over all test cases does not exceed 3 ⋅ 10^5.
Output
For each test case output n integers separated by space: temperatures of air in cells.
We need to solve the problem: given positions a_j and temperatures t_j, for each cell i from 1 to n compute min_j (t_j + |a_j - i|). For each test case, we need to output n integers.
Constraints: total n over test cases ≤ 3e5, q up to 1e4. So O(n log n) per test case or O(n) per test case is fine.
We need to compute the lower envelope of linear functions (or absolute value functions) which are V-shaped. Actually each conditioner j contributes a function f_j(i) = t_j + |i - a_j|. This is a piecewise linear convex function with slope -1 for i < a_j, slope +1 for i > a_j, and minimum t_j at i = a_j.
The minimum over many such V-shaped functions is also a convex piecewise linear function (since pointwise min of convex functions is convex). The result is essentially the lower envelope of lines with slopes ±1.
We can compute the answer for all i efficiently by two passes: one from left to right and one from right to left. Classic problem: "temperature of air conditioners" Codeforces problem 1730B? Actually similar to "1472B"? Wait there is Codeforces round "1730B" about meeting on line? No.
But I recall a known trick: define an array ans[i] initially set to a large value. For each conditioner j, we could try to update, but naive O(nk) is too high.
We can use BFS-like multi-source propagation where each conditioner j is a source with initial value t_j, and we want the minimum distance (t_j + distance). This is essentially computing the distance transform on a 1D grid where each source has a starting cost t_j. Because we want for each cell i: min_j (t_j + |i - a_j|). This is exactly the 1D distance transform with L1 metric. We can compute using two passes: left to right: maintain best value propagated from left sources; right to left: from right sources. Actually for each cell i, we can consider the minimum from sources on left: for a source at position a, the cost is t + (i - a) if i >= a. Similarly from right: t + (a - i) if i <= a. So we can compute for each i: ans[i] = min( min_{a_j <= i} (t_j + i - a_j), min_{a_j >= i} (t_j + a_j - i) ). But the min over left and right can be combined by precomputing prefix and suffix minima.
Let L[i] = min_{a_j <= i} (t_j - a_j). Then min_{a_j <= i} (t_j + i - a_j) = i + min_{a_j <= i} (t_j - a_j) = i + L[i]. But careful: we need to have a_j <= i. For positions where there is no conditioner on the left, L[i] is undefined (should be INF). Similarly, define R[i] = min_{a_j >= i} (t_j + a_j). Then min_{a_j >= i} (t_j + a_j - i) = -i + min_{a_j >= i} (t_j + a_j) = -i + R[i].
Then answer[i] = min(i + L[i], -i + R[i]). So if we can compute L[i] for all i (with proper definition) and R[i] for all i, we can get answer.
L[i] is essentially the minimum of (t_j - a_j) for all conditioners with a_j <= i. So we can sort conditioners by position, compute prefix minima of (t_j - a_j) as we scan positions. Then for each i, we need to know the prefix min up to i. Since positions are integer and maybe not every cell has a conditioner, we can create an array of size n, and after sorting conditioners, we can fill an array L_val of size n with INF initially, and for each conditioner at position a, update L_val[a] = min(L_val[a], t - a). Then we can compute L[i] = min(L[i-1], L_val[i]) for i from 1 to n. That yields L[i] as the min over all conditioners with position <= i. Similarly for R: we need min over conditioners with position >= i of (t + a). We can compute from right to left: R_val[a] = min(R_val[a], t + a), and then R[i] = min(R[i+1], R_val[i]) for i from n down to 1. Then answer[i] = min(i + L[i], -i + R[i]).
We have to be careful with INF: t up to 1e9, n up to 3e5, so INF can be something like 1e18.
But is that correct? Let's test with example: n=6, conditioners: (2,14), (5,16). Compute L_val: at pos2: 14-2=12; pos5:16-5=11; others INF. L[1] = INF (since no conditioner ≤1) -> L[1] = INF? Actually we need L[1] = min_{a<=1} (t-a). None, so INF. Then i + INF is huge. So answer[1] = min( INF, -i+R[i] ). For R: R_val: at pos2:14+2=16; pos5:16+5=21. Compute R[i] from right: R[6] = INF (since no conditioner >=6? Actually pos5 is <=6, but >=6 includes pos5? Condition: a>=i, so for i=6, a>=6: only positions 6? none, so INF. R[5] = min(R[6], R_val[5]=21) =21; R[4] = min(R[5]=21, R_val[4]=INF) =21; R[3]=min(R[4]=21, R_val[3]=INF)=21; R[2]=min(R[3]=21, R_val[2]=16)=16; R[1]=min(R[2]=16, R_val[1]=INF)=16. Now compute answer[1] = min(1 + INF, -1 + 16 =15) => 15, correct. answer[2] = min(2 + L[2], -2+R[2]). L[2] = min(L[1]=INF, L_val[2]=12)=12, so 2+12=14; -2+16=14; min=14. Good. answer[3]: L[3]=min(L[2]=12, L_val[3]=INF)=12; 3+12=15; R[3]=21; -3+21=18; min=15. Works. answer[4]: L[4]=12;4+12=16; R[4]=21;-4+21=17; min=16. answer[5]: L[5]=min(L[4]=12, L_val[5]=11)=11;5+11=16; R[5]=21;-5+21=16; min=16. answer[6]: L[6]=11;6+11=17; R[6]=INF? Actually R[6] we said INF? Wait we had R[6] from computation: R[6]=INF? Let's recompute: For i=6, conditioners with a>=6: only if a=6? but we have a=5,2 so none. So R[6] should be INF. But answer[6] from formula would be min(6+ L[6]=6+11=17, -6 + INF = INF) =>17. But expected is 17. That's fine. So R[6] can be INF, but answer uses left part. So the method works.
Thus we can solve each test case in O(n + k). Sorting conditioners? Actually we need to assign L_val and R_val based on positions. The positions are given as arrays but not sorted. We could sort the conditioners by position to fill L_val and R_val in one pass, but we don't strictly need to sort if we iterate over the k conditioners and update arrays at position a_j. Since n up to 3e5 and total n sum, we can allocate arrays of size n+2 per test case and set INF, then for each conditioner update L_val[a] = min(L_val[a], t - a) and R_val[a] = min(R_val[a], t + a). That's O(k) time, unsorted but it's fine because we are directly indexing the cell position. But careful: L_val is defined for each cell index; we need to compute prefix minima over i. That's independent of order. So we don't need to sort. However, one might worry about duplicate positions? Problem states "Two or more air conditioners cannot be placed in the same cell (i.e. all a_i are distinct)." So each cell can have at most one conditioner. So we can update L_val and R_val directly.
Thus algorithm per test case:
Read n, k.
Read list a[0..k-1] (positions, 1-indexed).
Read list t[0..k-1] (temperatures).
Initialize arrays L_val[1..n] and R_val[1..n] with INF (large number, say 10**18).
Then compute L[1] = L_val[1]; for i from 2 to n: L[i] = min(L[i-1], L_val[i]).
Compute R[n] = R_val[n]; for i from n-1 down to 1: R[i] = min(R[i+1], R_val[i]).
Then for i from 1 to n: ans[i] = min(i + L[i], -i + R[i]).
Output ans[1..n].
But careful: For positions where L[i] is INF (i.e., no conditioner on left up to i), i + L[i] will be huge, but we rely on min with the other term which might be finite. Similarly for R[i]. That's fine as long as we treat INF as something not causing overflow. Use 64-bit integers.
Time complexity: O(n + k) per test case, total O(total n + total k) ≤ O(total n + total k). Since k ≤ n, total k ≤ total n? Actually each test case k ≤ n, but sum of n is ≤ 3e5, sum of k across test cases ≤ sum of n? Possibly but we can't assume, but it's bounded by total n across test cases because each k ≤ n, so total k across test cases ≤ total n (since each test case k ≤ n). So total input size is O(total n). So overall O(total n).
Memory: per test case O(n). Since total n across test cases ≤ 3e5, we can allocate new arrays each test case and reuse or allocate fresh. In Python, we need to be careful about time, but O(3e5) operations is fine.
Edge Cases: n=1, k=1: L_val[1] = t - 1, R_val[1] = t + 1. L[1] = t - 1, R[1] = t + 1. ans[1] = min(1 + (t-1) = t, -1 + (t+1) = t) = t. OK.
If there are conditioners at extremes, it's fine.
But wait: is the approach correct if there is a conditioner at position i, then L[i] includes (t_i - i) and R[i] includes (t_i + i). Then ans[i] = min(i + (t_i - i), -i + (t_i + i)) = min(t_i, t_i) = t_i, which is correct.
Thus the algorithm works.
Double-check with given examples.
Example 1: We computed and matches.
Example 2: n=10, k=1, a=7, t=30. Then L_val[7]=30-7=23; R_val[7]=30+7=37. Others INF. L: i=1..6: L[i]=INF? Actually L[1]=INF, L[2]=min(INF, INF)=INF,... L[6]=INF, L[7]=min(L[6],23)=23, L[8]=min(L[7], INF)=23, L[9]=23, L[10]=23. R: from right: R[10]=INF, R[9]=INF, R[8]=INF, R[7]=min(R[8],37)=37? Wait R[8] is INF, so R[7]=37; R[6]=min(R[7]=37, INF)=37; ... R[1]=37. Then ans: i=1: min(1+INF, -1+37=36) ->36; i=2: min(2+INF=INF, -2+37=35) ->35; i=3: -3+37=34; i=4: -4+37=33; i=5: -5+37=32; i=6: -6+37=31; i=7: min(7+23=30, -7+37=30) ->30; i=8: min(8+23=31, -8+37=29) ->29? But expected output for i=8 is 31? Let's check expected output from problem: For test case 2: Output: "36 35 34 33 32 31 30 31 32 33". Actually they gave: "36 35 34 33 32 31 30 31 32 33". So i=8 should be 31, i=9:32, i=10:33. But our computed ans for i=8 gives min(8+23=31, -8+37=29) = 29, which is wrong. Wait, there's a mistake. Let's recalc: For i=8, what is R[8]? We said R[8]=INF? But is that correct? Conditioner at position 7. For i=8, we need min_{a_j >= i} (t_j + a_j). Since a_j=7, which is less than 8, so there is no conditioner with a_j >= 8. Therefore the set for the right side is empty, so that term should be INF. So indeed min_{a_j >= i} (t_j + a_j - i) is not defined; we should only consider those conditioners with a >= i. Since none, that term is infinite. But wait, the definition ans[i] = min( min_{a_j <= i} (t_j + i - a_j), min_{a_j >= i} (t_j + a_j - i) ). That's correct. So for i=8, there is no conditioner with a >= 8, so the second term is INF. So we only have the first term: min_{a <= i} (t + i - a). For i=8, there is conditioner at a=7, so first term = 30 + (8-7) = 31. So ans[8] should be 31. But our formula ans[i] = min(i + L[i], -i + R[i]) used R[i] defined as min_{a >= i} (t + a). For i=8, there is no a>=8, so R[8] should be INF, not 37. Then -i + R[8] = -8 + INF = INF, so min gives i + L[8] = 8 + L[8]. L[8] = min_{a <= 8} (t - a) = 30-7=23. So 8+23=31. That's correct. But earlier we incorrectly computed R[8] as 37 because we used suffix minima incorrectly: we filled R_val only at position 7. In our algorithm, we compute R array as suffix minima of R_val, where R_val[a] = t + a for conditioner at position a. So R[i] = min_{j >= i} R_val[j]. If there is no conditioner at any position j >= i, then R_val[j] is INF for all j >= i, so R[i] should be INF. However, in my earlier manual calculation, I mistakenly thought R[7] = 37 and that R[8] would be min(R[9], R_val[8]) but R[9] might be INF? Let's compute correctly:
Initialize R_val of size n+2 with INF.
Set R_val[7] = 30+7=37.
Now compute R from right:
R[10] = R_val[10] = INF
R[9] = min(R[10], R_val[9]) = min(INF, INF) = INF
R[8] = min(R[9], R_val[8]) = min(INF, INF) = INF
R[7] = min(R[8], R_val[7]) = min(INF, 37) = 37
R[6] = min(R[7], R_val[6]) = min(37, INF) = 37
...
R[1] = 37.
Thus R[8] is INF, as desired. My earlier statement that R[8]=min(R[9]=INF, R_val[8]=INF)=INF, okay. Then -8+R[8] = INF, so min(8+23=31, INF) =31. So correct. So our algorithm yields correct answer.
But wait earlier I said R[8] = 37? That was a mistake. So it's fine.
Thus algorithm stands.
Now consider test case 3: n=5, k=5, a=[3,1,4,2,5], t=[3,1,4,2,5]. All positions have conditioners. Then L_val: at pos1:1-1=0; pos2:2-2=0? Actually t=2 at pos2: 2-2=0; pos3:3-3=0; pos4:4-4=0; pos5:5-5=0; so L[i] will be 0 for i>=1? L[1]=0, L[2]=0, etc. R_val: at pos1:1+1=2; pos2:2+2=4; pos3:3+3=6; pos4:4+4=8; pos5:5+5=10. Then R[i] suffix min: from right: R[5]=10, R[4]=min(10,8)=8, R[3]=min(8,6)=6, R[2]=min(6,4)=4, R[1]=min(4,2)=2. Then ans[i] = min(i + 0 = i, -i + R[i]). Compute:
i=1: min(1, -1+2=1) =>1.
i=2: min(2, -2+4=2) =>2.
i=3: min(3, -3+6=3) =>3.
i=4: min(4, -4+8=4) =>4.
i=5: min(5, -5+10=5) =>5. So output 1 2 3 4 5, matches.
Test case 4: n=7, k=1, a=1, t=1e9. L_val[1]=1e9-1, R_val[1]=1e9+1. L: L[1]=1e9-1, L[2..7]=1e9-1. R: compute suffix: R[7]=INF, R[6]=INF, ..., R[2]=INF? Actually careful: R_val only at position 1. So for i>=2, R_val[i] = INF. So suffix: R[7]=INF, R[6]=INF, R[5]=INF, R[4]=INF, R[3]=INF, R[2]=INF, R[1]=min(R[2], R_val[1]) = min(INF, 1e9+1)=1e9+1. Then ans:
i=1: min(1+(1e9-1)=1e9, -1+ (1e9+1)=1e9) =1e9.
i=2: min(2+(1e9-1)=1e9+1, -2+INF = INF) =1e9+1.
i=3: min(3+ (1e9-1)=1e9+2, INF) =1e9+2.
... up to i=7: 1e9+6. Matches output.
There's an empty line before each test case, but input parsing in Python can ignore blank lines by reading and stripping? Actually the problem statement says: "Before each test case, there is an empty line." So we need to handle that. We can just use input() and skip lines that are empty? But careful: after reading q, there is an empty line, then test case 1. In Python, we can do:
code
q = int(input().strip())
for _ in range(q):
input() # skip empty line
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = list(map(int, input().split()))
But note that the blank line might not be present after the last test case? Usually it is there, but it's safe to call input() and if it's empty, it's fine. But we need to ensure we don't miss the next line. The sample input shows:
code
5
6 2
2 5
14 16
10 1
7
30
...
So after the first line "5", there is an empty line, then "6 2". So the above works. However, some judges might omit the blank line? The problem says "Before each test case, there is an empty line." So it's guaranteed. So we can read the empty line.
Alternatively, we could read all lines and ignore empties. But we'll do as described.
Now for each test case:
n up to 3e5, but total n across tests is ≤3e5, so we can allocate arrays of length n+2 (1-indexed). Use list of size n+2 with INF.
L_val = [INF] * (n+2) (index 1..n)
R_val = [INF] * (n+2)
For each j:
pos = a[j]
L_val[pos] = min(L_val[pos], t[j] - pos)
R_val[pos] = min(R_val[pos], t[j] + pos)
Then compute prefix minima: L = [0]*(n+2) maybe? Actually we need L[i] for i=1..n. We can reuse L_val array or create new. But careful: L_val will be overwritten if we compute in place. We'll compute L array:
code
L = [INF]*(n+2)
for i in range(1, n+1):
L[i] = min(L[i-1], L_val[i])
But we can compute L directly as a variable? Actually we need L[i] for each i later when computing ans. So we need to store L[i] or compute ans on the fly? Because ans[i] depends on L[i] and R[i]. We can compute L array first, then R array, then ans. Or we can compute L and then while iterating from left we could compute ans but we also need R. So need both L and R arrays, or compute R first then compute ans in one pass using L and R? We need both at same time, so we can compute L and R separately and store them. Since n up to 3e5, storing two arrays of size n+1 is fine.
Alternatively, we can compute L and then compute ans in left-to-right using L and also maintain something for R? But R requires suffix minima, so we can compute R array first then ans. I'll compute both.
After computing L and R, create ans list of length n (0-indexed maybe). For i in 1..n: ans_i = min(i + L[i], -i + R[i]). Append to output list.
Print ans as space-separated integers.
Edge Cases: Ensure we use large INF, say 1030 or 263-1? Since t up to 1e9, n up to 3e5, so maximum t + distance could be up to 1e9 + 3e5 ~ 1e9+3e5, so INF like 1018 is safe. We'll use 1018.
But careful: When we compute L[i] for i=1, L[i-1] = L[0] should be INF. So we initialize L[0] = INF. Similarly R[n+1] = INF.
Implementation:
code
import sys
def solve():
data = sys.stdin.read().strip().split()
# This approach will ignore blank lines automatically.
# We need to parse q, then for each test case read n,k, then k a's, then k t's.
it = iter(data)
q = int(next(it))
out_lines = []
for _ in range(q):
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for __ in range(k)]
t = [int(next(it)) for __ in range(k)]
INF = 10**30
L_val = [INF] * (n + 2) # 1-indexed, ignore index 0
R_val = [INF] * (n + 2)
for pos, temp in zip(a, t):
# pos is 1-indexed
if temp - pos < L_val[pos]:
L_val[pos] = temp - pos
if temp + pos < R_val[pos]:
R_val[pos] = temp + pos
L = [INF] * (n + 2)
L[0] = INF
for i in range(1, n+1):
L[i] = min(L[i-1], L_val[i])
R = [INF] * (n + 2)
R[n+1] = INF
for i in range(n, 0, -1):
R[i] = min(R[i+1], R_val[i])
ans = []
for i in range(1, n+1):
val = min(i + L[i], -i + R[i])
ans.append(str(val))
out_lines.append(' '.join(ans))
sys.stdout.write('\n'.join(out_lines))
if __name__ == "__main__":
solve()
But we must be careful: In the input, after reading q, there may be blank lines but using split() eliminates whitespace, so it's fine. However, the problem states that before each test case there is an empty line. Using split() ignores those, so it's simpler.
But ensure that the reading order is correct: q, then for each test case: n, k, then k ints for a, then k ints for t. That's consistent.
Check with sample.
Potential pitfalls: INF must be large enough to avoid overflow when adding i? Actually INF + i could become large but still within Python's arbitrary ints. But we need min, so if INF is too large it's fine. But if we use INF as something like 1018, adding i yields ~1018, still fine.
But watch: L[i] could be INF if no conditioner on left. Then i+L[i] is INF + i = huge, but Python can handle. However, we might get overflow? Python ints are arbitrary precision, so no problem.
Time complexity: For each test case, we allocate arrays of size n+2. Since total n across test cases ≤ 3e5, total memory allocated across test cases may be okay if we allocate per test case and then let GC free. But we can reuse? Not needed.
But note: We allocate L_val and R_val of size n+2 (twice) and L and R of size n+2 (twice) maybe 4 arrays of ~3e5 each = 1.2e6 integers, each integer Python object overhead is large (~28 bytes). That could be memory heavy (like 30 MB per test case). With total n 3e5, if we allocate multiple arrays in one test case, it's okay but might be borderline but still okay: 4 * 300k ~ 1.2M Python ints, each maybe 28 bytes => ~33.6 MB. Plus overhead for lists etc. Within typical memory limit (256 MB). However, if we allocate 4 arrays per test case and we have many test cases, but total n across tests is 3e5, the sum of sizes of arrays across test cases might be high if we allocate new arrays for each test case and not free immediately; but Python will free old ones as we go. So memory usage at any time is for the current test case plus some overhead. So it's fine.
But we can reduce memory by reusing arrays: we can compute L and then reuse L_val as L? Actually we need L_val to compute L, but we can compute L in place: we can compute L directly on L_val array by doing a cumulative min: for i from 2 to n: L_val[i] = min(L_val[i-1], L_val[i]). That would convert L_val into the L array. Similarly, we can compute R in place on R_val. Then we don't need separate L and R arrays. Then we compute ans using these in-place arrays. But careful: After we compute L in place, we lose original L_val but we no longer need it. So we can use:
code
L = [INF]*(n+2)
for pos, temp in zip(a, t):
L[pos] = min(L[pos], temp - pos)
for i in range(2, n+1):
L[i] = min(L[i-1], L[i])
But note that initially L array is INF, but we need to set L[0] as INF (or we can start loop from 1). Actually we need L[0] as base. We can set L[0] = INF and then for i from 1..n: L[i] = min(L[i-1], original L[i])? Wait we need to include the contribution at i. The original contributions are set in L initially. So we can set L = [INF]*(n+1) (index 0..n). Then for each conditioner: if temp - pos < L[pos]: L[pos] = temp - pos. Then after that, for i in range(1, n+1): L[i] = min(L[i-1], L[i]). But we need L[0] = INF to not affect. That works. So L array now holds prefix minima.
Similarly for R: we can have R = [INF]*(n+2) (index 1..n+1). We can use indices 1..n, and have R[n+1] = INF. Fill R[pos] with min of (temp+pos). Then for i from n down to 1: R[i] = min(R[i+1], R[i]). So after this, R holds suffix minima.
Then ans[i] = min(i + L[i], -i + R[i]).
That uses only two arrays (L and R) plus maybe not needing separate L_val/R_val. So memory is half.
We also could compute ans on the fly without storing both arrays? But we need both L and R simultaneously. Two arrays of length n+2 is fine.
Let's adopt that.
Implementation:
code
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
q = int(next(it))
out = []
INF = 10**30
for _ in range(q):
n = int(next(it)); k = int(next(it))
a = [int(next(it)) for __ in range(k)]
t = [int(next(it)) for __ in range(k)]
L = [INF] * (n + 2) # index 0..n+1, but we use 1..n
R = [INF] * (n + 2)
for pos, temp in zip(a, t):
val_left = temp - pos
if val_left < L[pos]:
L[pos] = val_left
val_right = temp + pos
if val_right < R[pos]:
R[pos] = val_right
# prefix min on L
for i in range(1, n+1):
if L[i-1] < L[i]:
L[i] = L[i-1]
# suffix min on R
for i in range(n, 0, -1):
if R[i+1] < R[i]:
R[i] = R[i+1]
ans = []
for i in range(1, n+1):
# compute min(i + L[i], -i + R[i])
v = i + L[i]
w = -i + R[i]
if w < v:
v = w
ans.append(str(v))
out.append(' '.join(ans))
sys.stdout.write('\n'.join(out))
We need to ensure we properly handle INF: For L[i] that stays INF, i+L[i] will be huge, but min will pick the other if finite. But if both are INF? That would happen if there are no conditioners? But k>=1 always, so at least one conditioner, so for any i, at least one of the terms will be finite? Actually consider i such that there is no conditioner on left nor on right? That would be impossible because there is at least one conditioner somewhere. For i less than the minimum position, there is no conditioner on left? Actually left means a_j <= i. If i < min_a, then no conditioner on left, but there is conditioner on right (since there exists conditioner with a > i). So right term is finite. Similarly for i > max_a, left term finite. So at least one finite. So no issue.
One more check: In the prefix min loop: we started with L[0] = INF. So for i=1, L[1] = min(L[0], original L[1])? But we are not explicitly using L[0] in the min, because we just compare L[i] with L[i-1] and assign min. That works: if L[i-1] is smaller, we set L[i] to that. But what if L[i-1] is INF but L[i] is also INF? Then it's INF. Ok.
But careful: In the loop we wrote: if L[i-1] < L[i]: L[i] = L[i-1]. This updates in place. That's fine.
Similarly for suffix: we do if R[i+1] < R[i]: R[i] = R[i+1].
Edge Cases: n=1, then loops: prefix: i from 1 to 1: if L[0] < L[1], L[1] = L[0] (but L[0] is INF, so no change). suffix: i from 1 to 1: if R[2] < R[1], R[1] = R[2] (R[2] is INF). So no change. ans[1] computed correctly.
Now test with sample.
We'll run mentally or with code.
But we should test a case where multiple conditioners at same position? Not allowed.
Now consider performance: loops over n twice (prefix and suffix) plus k updates. This is O(n). Total O(total n). Python should be fast enough for 3e5 operations.
One nuance: For large INF, adding i might cause no overflow, but we should be careful about using 10**30, which is fine.
But maybe we can use a more moderate INF: since t <= 1e9, n <= 3e5, the maximum possible answer is at most 1e9 + 3e5 ≈ 1.0003e9. However, the min over left and right yields values that might be up to that. So INF could be 10**18, safe.
But in Python, integers are arbitrary, so it's okay.
We might also use a sentinel like 2**63-1, but it's fine.
Now, let's think about potential pitfalls: The input may be large; we should use sys.stdin.read() for fast input. Using split() is efficient.
Now consider the condition that there is an empty line before each test case. Our split approach will ignore them, so it's fine. But what about the fact that after reading q, there is an empty line; that will be skipped by split as whitespace. So no problem.
Now we should test with sample.
We'll implement and run.
But hold on: The sample output has a space after each line? The example shows:
"15 14 15 16 16 17 "
with trailing space. Usually not required. In our join, we produce no trailing space.
But typical Codeforces problem accepts no trailing spaces. However, it's safer to produce no trailing spaces.
Now we should consider if there is any issue with the formula when L[i] is INF: i+L[i] would be INF, and min with the other yields correct.
Now, is there any scenario where both i+L[i] and -i+R[i] are INF? As argued, no.
Now, consider the possibility that a conditioner may have t_j - a_j negative, so L can be negative. That's fine. For example, in test case 5, L[6] = -1. i+L[i] gave 5. So negative values are allowed.
Now, code.
But we must note: The positions a_i are given as 1-indexed. Our arrays are 1-indexed. So we allocate size n+2. For prefix, we use L[0] as INF. For suffix, we need R[n+1] as INF. We set them as part of initialization by making size n+2 and filling with INF. We'll use indices 0..n+1. L[0] is INF. R[n+1] is INF.
Now, in the suffix loop: for i in range(n, 0, -1): we compare R[i] with R[i+1]. Since R[i+1] is within bounds (up to n+1), it's fine.
Now, we need to set L[pos] and R[pos] initially to INF, then update with min. However, note that there could be multiple conditioners? Actually all a_i are distinct, so each position appears at most once. So we don't need min; we could assign directly. But it's safe to use min.
Now, one more potential optimization: Since k ≤ n, we could process conditioners without storing a and t arrays if we read them on the fly, but reading them as lists is fine.
Now, confirm the total sum of n is ≤ 3e5, so reading all test cases at once is fine.
Now, we should test with some edge cases manually.
Case: n=3, k=2, a=[1,3], t=[10,10]. Then L_val: pos1:10-1=9; pos3:10-3=7. L: L[1]=9; L[2]=min(9, INF)=9; L[3]=min(9,7)=7. R_val: pos1:10+1=11; pos3:10+3=13. R: from right: R[3]=13; R[2]=min(13, INF)=13; R[1]=min(13,11)=11. Then ans: i=1: min(1+9=10, -1+11=10)=10; i=2: min(2+9=11, -2+13=11)=11; i=3: min(3+7=10, -3+13=10)=10. Expected: For i=2, distance to both is 1, so t+1=11, correct. Good.
Case: t_j could be up to 1e9, but negative? No, t_j ≥ 1, so t_j - a_j can be negative if a_j large enough, but that's fine.
Now consider the possibility of overflow when using int? Python fine.
Now let's think about alternative solutions: BFS from all sources with 0-1? Not needed.
Thus the solution is straightforward.
Now I will produce the final answer.
But before finalizing, note that there is a known Codeforces problem "Air Conditioners" (problem 1547E maybe? Actually there is problem "1552C"? I'm not sure, but the solution is exactly this two-pass method.
Now I'll write final code.
Potential micro-optimizations: Use list comprehension for ans, but it's fine.
But be careful: In Python, loops over 3e5 are fast, but if we have many test cases (up to 1e4), each with small n, the overhead of array creation for each test case could be high. However total n is 3e5, so number of test cases may be large only if each n is small. For example, 1e4 test cases with average n=30. Creating arrays of size n+2 for each test case might be a bit of overhead but still within time (1e4 * small arrays). But we can handle more efficiently by using the same array and resizing? Not needed. The total number of allocated cells across all test cases is sum of (n+2) which is about 3e5, so overall memory allocation is fine. The overhead of Python list creation for many small lists might be okay. But we can optimize by reading everything and processing each test case independently.
But wait, the total sum of n is 3e5, but if we have 1e4 test cases, the sum of k also ≤ 3e5. But each test case we allocate arrays of size n+2. That means across all test cases, total allocated list length across time may be sum of (n+2) ≈ 3e5, but we allocate and deallocate per test case. That's okay.
But there is also overhead of creating these lists via [INF]*(n+2). For n=1, it's a list of size 3; for many such small lists, it's fine.
Time limit? Usually 2 seconds. Python solution with O(total n) should pass.
But we need to ensure that we use fast I/O. Using sys.stdin.read() and sys.stdout.write is good.
Now double-check with sample.
Now I'll craft final answer.
But before finishing, there is a nuance: In the prefix loop we used:
code
for i in range(1, n+1):
if L[i-1] < L[i]:
L[i] = L[i-1]
But what if L[i-1] == L[i]? It's fine.
Similarly suffix.
But ensure that we properly update L[0] = INF. Since we allocated L with size n+2 and filled with INF, L[0] is already INF. Good.
Now, I think solution is correct.
Let's consider a test case with k=1 and a=1. n=1. L[1]=t-1, L[0]=INF. After prefix: L[1] stays t-1. R[1]=t+1, R[2]=INF. After suffix: R[1] = min(R[2], original R[1]) = min(INF, t+1) = t+1. Then ans[1] = min(1 + (t-1)=t, -1 + (t+1)=t) = t. Good.
Now, ensure that we treat positions correctly: a_i between 1 and n inclusive.
Now, I will produce final answer in Python.
But there is a potential subtlety: In the problem description, temperatures t_i are up to 1e9, n up to 3e5, so t_i + n up to 1e9+3e5, which fits in 32-bit signed? 1e9+3e5 ≈ 1,000,300,000 < 2^31-1 (2,147,483,647) actually it's less. So 32-bit signed int is safe. But Python's int is unlimited, so fine.
Now, we can produce final answer.
Edge Cases: When k = n (all positions have conditioner), then L and R are all finite. Works.
Now final answer: Provide explanation and code.
But as this is a response to a problem (maybe Codeforces problem 1730B? Not sure), but we are to output the answer.
Now I'll write the solution in the final message.
But the user asked: "On a strip of land ... For each cell from 1 to n find the temperature in it." So they want solution.
So I will respond with an explanation and then code.
But they might expect just code. Since the user is ChatGPT, they might want a solution. Usually in such interactions, they want a Python program.
I'll provide Python solution with explanation.
Now, some additional consideration: The total n sum is 3e5, but we also have q up to 1e4. Using sys.setrecursionlimit not needed.
Now final code:
code
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
q = int(next(it))
out_lines = []
INF = 10**30
for _ in range(q):
n = int(next(it)); k = int(next(it))
a = [int(next(it)) for _ in range(k)]
t = [int(next(it)) for _ in range(k)]
L = [INF] * (n + 2)
R = [INF] * (n + 2)
for pos, temp in zip(a, t):
left_val = temp - pos
if left_val < L[pos]:
L[pos] = left_val
right_val = temp + pos
if right_val < R[pos]:
R[pos] = right_val
for i in range(1, n+1):
if L[i-1] < L[i]:
L[i] = L[i-1]
for i in range(n, 0, -1):
if R[i+1] < R[i]:
R[i] = R[i+1]
ans = [str(min(i + L[i], -i + R[i])) for i in range(1, n+1)]
out_lines.append(' '.join(ans))
sys.stdout.write('\n'.join(out_lines))
if __name__ == "__main__":
main()
Note: I used sys.stdin.buffer.read() for speed.
Now produce final answer with explanation and code. We need to compute, for each cell i from 1 to n, the value min_{j} (t_j + |a_j - i|). This is a classic 1D distance transform with L1 metric. The key observation is that the minimum can be split into two parts:
From conditioners on the left (a_j ≤ i): t_j + i - a_j = i + (t_j - a_j).
From conditioners on the right (a_j ≥ i): t_j + a_j - i = -i + (t_j + a_j).
Thus, for each i we need the minimum of (i + left_min) and (-i + right_min), where
left_min(i) = min_{a_j ≤ i} (t_j - a_j),
right_min(i) = min_{a_j ≥ i} (t_j + a_j).
We can precompute these prefix and suffix minima in O(n) per test case.
Implementation steps for each test case:
Read n, k, arrays a (positions) and t (temperatures).
Initialize two arrays L and R of size n+2 with a large INF value.
For each conditioner (pos, temp):
Set L[pos] = min(L[pos], temp - pos)
Set R[pos] = min(R[pos], temp + pos)
Compute prefix minima on L: for i from 1 to n: L[i] = min(L[i-1], L[i]).
Compute suffix minima on R: for i from n down to 1: R[i] = min(R[i+1], R[i]).
For each i from 1 to n, answer[i] = min(i + L[i], -i + R[i]).
Output the answers.
Complexities: O(n + k) time, O(n) memory per test case. Since total n over all test cases ≤ 3·10⁵, this runs comfortably within limits.
python
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
q = int(next(it))
out = []
INF = 10**30 # larger than any possible answer
for _ in range(q):
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for _ in range(k)]
t = [int(next(it)) for _ in range(k)]
L = [INF] * (n + 2) # 1‑based, extra cell for sentinel
R = [INF] * (n + 2)
for pos, temp in zip(a, t):
left_val = temp - pos
if left_val < L[pos]:
L[pos] = left_val
right_val = temp + pos
if right_val < R[pos]:
R[pos] = right_val
# prefix minima (left)
for i in range(1, n + 1):
if L[i-1] < L[i]:
L[i] = L[i-1]
# suffix minima (right)
for i in range(n, 0, -1):
if R[i+1] < R[i]:
R[i] = R[i+1]
ans = [str(min(i + L[i], -i + R[i])) for i in range(1, n + 1)]
out.append(' '.join(ans))
sys.stdout.write('\n'.join(out))
if __name__ == "__main__":
main()