Universal Cup Judging System

Universal Cup

时间限制: 1.0 s 内存限制: 1024 MB 总分: 100
统计

You are playing a grid filling game. On the game interface, several squares are arranged in a row, and you need to fill each square with a positive integer. The system will then calculate the score based on the numbers you fill in.

Before each round of the game starts, the system will provide you with constraints for filling the squares. Suppose there are $n$ squares arranged in a row for the current game, the system will provide two positive integers $l_i, r_i$ for the $i$-th square, indicating that the number $x_i$ you fill in the $i$-th square must satisfy $l_i \le x_i \le r_i$. After you have filled in the numbers, the system will separate the sequence of squares into several segments based on adjacent squares with different numbers. The system will then calculate the sum of the squares of the lengths of each segment, which is referred to as the continuity of the filling scheme.

For example, for the filling scheme $x = [1, 1, 3, 3, 5, 5, 5, 3, 3]$, the segmentation result is $\underline{1, 1}, \underline{3, 3}, \underline{5, 5, 5}, \underline{3, 3}$, and the continuity corresponds to $2^2 + 2^2 + 3^2 + 2^2 = 21$.

The goal of this game is to minimize continuity. You need to determine the minimum value of continuity under the optimal filling strategy.

Input

You need to answer for multiple rounds of the game. The first line contains an integer $T$ ($1 \le T \le 10^5$), representing the number of game rounds.

For each round of the game:

The first line of input contains a positive integer $n$ ($1 \le n \le 10^6$), representing the number of squares.

The next line contains $n$ positive integers $l_i$ ($1 \le l_i \le 10^9$), representing the lower bound constraints for each square.

The following line contains $n$ positive integers $r_i$ ($1 \le r_i \le 10^9, l_i \le r_i$), representing the upper bound constraints for each square.

It is guaranteed that the total sum of $n$ across all games does not exceed $10^6$.

Output

For each round of the game, output a single integer representing the minimum value of continuity.

Example

Sample Input 1

3
3
1 1 4
5 1 4
6
1 2 3 4 4 4
3 3 3 4 5 6
7
1 1 2 2 1 1 1
1 2 2 2 2 1 1

Sample Output 1

3
6
17

Note

For the first round of the game, one possible filling scheme is $x = [3, 1, 4]$, which corresponds to a continuity of $1^2 + 1^2 + 1^2 = 3$.

For the second round of the game, one possible filling scheme is $x = [1, 2, 3, 4, 5, 6]$, which corresponds to a continuity of $1^2 + 1^2 + 1^2 + 1^2 + 1^2 + 1^2 = 6$.

For the third round of the game, one possible filling scheme is $x = [1, 1, 2, 2, 2, 1, 1]$, which corresponds to a continuity of $2^2 + 3^2 + 2^2 = 17$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.