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
python
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.
import sys
def main():
input = sys.stdin.read().split()
ptr = 0
q = int(input[ptr])
ptr += 1
for _ in range(q):
# Skip empty lines if any
while ptr < len(input) and input[ptr] == '':
ptr += 1
n = int(input[ptr])
k = int(input[ptr+1])
ptr += 2
a = list(map(int, input[ptr:ptr+k]))
ptr += k
t = list(map(int, input[ptr:ptr+k]))
ptr += k
# Sort by a in ascending order
sorted_pairs = sorted(zip(a, t), key=lambda x: x[0])
sorted_a = [x[0] for x in sorted_pairs]
sorted_t = [x[1] for x in sorted_pairs]
# Compute left_min
left_min = [0] * (n + 2) # 1-based index
min_left = float('inf')
ptr_left = 0
for i in range(1, n + 1):
while ptr_left < k and sorted_a[ptr_left] <= i:
current_t = sorted_t[ptr_left]
current_a = sorted_a[ptr_left]
min_left = min(min_left, current_t - current_a)
ptr_left += 1
left_min[i] = min_left
# Sort by a in descending order
sorted_pairs_desc = sorted(zip(a, t), key=lambda x: -x[0])
sorted_a_desc = [x[0] for x in sorted_pairs_desc]
sorted_t_desc = [x[1] for x in sorted_pairs_desc]
# Compute right_min
right_min = [0] * (n + 2)
min_right = float('inf')
ptr_right = 0
for i in range(n, 0, -1):
while ptr_right < k and sorted_a_desc[ptr_right] >= i:
current_t = sorted_t_desc[ptr_right]
current_a = sorted_a_desc[ptr_right]
min_right = min(min_right, current_t + current_a)
ptr_right += 1
right_min[i] = min_right
# Calculate result for each cell
res = []
for i in range(1, n + 1):
l_val = left_min[i] + i
r_val = right_min[i] - i
res.append(str(min(l_val, r_val)))
# Print the result with a trailing space and newline
print(' '.join(res), end=' ')
print()
if __name__ == "__main__":
main()