#P11158. 【MX-X6-T4】夢重力
【MX-X6-T4】夢重力
Background
Original problem link: https://oier.team/problems/X6E.
空を仰げば 青さが僕を 飲み込んでしまう気がしてて 無重力なら楽だろうか 宇宙まで行けたら
In the random motions of celestial bodies, how can we find a point with no gravity?
Problem Description
You are given an grid with key points. It is guaranteed that each row and each column contains exactly one key point. It is also guaranteed that is even.
We define a zero-gravity region in the grid as a sub-square of size formed by consecutive rows and consecutive columns, such that it contains no key points.
Let be the number of different zero-gravity regions after swapping row and row of the grid. For all possible swaps, compute the sum of , i.e., you need to compute:
Note that computing does not actually perform the swap on the grid, and the grid will not be modified throughout the process.
Input Format
The first line contains an integer . It is guaranteed that is even.
The next line contains space-separated integers , meaning the key points are located at . It is guaranteed that is a permutation.
Output Format
Output one integer in a single line, representing the answer.
4
1 2 3 4
8
10
9 8 1 10 7 2 4 3 6 5
27
Hint
Sample Explanation #1.

In the figure above, the top-left corresponds to the original grid. The gray parts indicate key points.
The following grids correspond to all possible swaps (in order: swapping ). The existing zero-gravity regions are marked in red and blue (purple indicates the intersection of two zero-gravity regions). It is easy to see that the answer is .
Constraints.
For all testdata, it is guaranteed that and is even, and is a permutation.
Bundled test, with a total of 4 subtasks. The limits are:
- Subtask 1 (12 pts): .
- Subtask 2 (19 pts): .
- Subtask 3 (34 pts): .
- Subtask 4 (35 pts): no additional constraints.
Translated by ChatGPT 5