#P17445. 长廊调律 / Corridor Tuning

长廊调律 / Corridor Tuning

Problem Description

Along a corridor, nn resonators are placed in order. The current frequency offset of the ii-th resonator is aia_i, and the target offset is bib_i.

The tuning device can only be activated from one end of the corridor. In one operation, you may choose any non-zero integer xx, and perform one of the following two actions:

  1. Choose a kk (1≤k≤n1\le k\le n), and add xx to every number in the prefix a1,a2,…,aka_1,a_2,\ldots,a_k.
  2. Choose a kk (1≤k≤n1\le k\le n), and add xx to every number in the suffix ak,ak+1,…,ana_k,a_{k+1},\ldots,a_n.

xx can be positive or negative. No matter how large ∣x∣|x| is, this modification counts as exactly one operation.

Find the minimum number of operations needed to transform sequence aa into sequence bb.

Input Format

The first line contains an integer TT (1≤T≤1041\le T\le 10^4), the number of test cases.

For each test case:

  • The first line contains an integer nn (1≤n≤2×1051\le n\le 2\times 10^5).
  • The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−109≤ai≤109-10^9\le a_i\le 10^9).
  • The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (−109≤bi≤109-10^9\le b_i\le 10^9).

It is guaranteed that the sum of nn over all test cases does not exceed 2×1052\times 10^5. Also, over all test cases, the sum of ∑∣ai−ai−1∣(i≥2)\sum |a_i-a_{i-1}|(i\ge 2) and the sum of ∑∣bi−bi−1∣(i≥2)\sum |b_i-b_{i-1}|(i\ge 2) are each no more than 10410^4.

Output Format

For each test case, output one line with one integer, the minimum number of operations.

4
3
0 0 0
-1 2 0
3
0 0 0
3 1 4
4
1 4 2 8
6 9 7 13
2
-7 10
-7 10
2
3
1
0

Hint

For the first test case, you can do:

  1. Add −3-3 to the prefix of length 11, obtaining [−3,0,0][-3,0,0].
  2. Add 22 to the prefix of length 22, obtaining [−1,2,0][-1,2,0].

For the third test case, adding 55 to the entire sequence is enough.

Translated by ChatGPT 5