#P11158. 【MX-X6-T4】夢重力

【MX-X6-T4】夢重力

Background

Original problem link: https://oier.team/problems/X6E.


空を仰げば\\ 青さが僕を\\ 飲み込んでしまう気がしてて\\ 無重力なら楽だろうか\\ 宇宙まで行けたら

—— 夢重力 - Nanatsukaze

In the random motions of celestial bodies, how can we find a point with no gravity?

Problem Description

You are given an n×nn \times n grid with nn key points. It is guaranteed that each row and each column contains exactly one key point. It is also guaranteed that nn is even.

We define a zero-gravity region in the grid as a sub-square of size n2×n2\dfrac{n}{2} \times \dfrac{n}{2} formed by n2\dfrac{n}{2} consecutive rows and n2\dfrac{n}{2} consecutive columns, such that it contains no key points.

Let f(i,j)f(i,j) be the number of different zero-gravity regions after swapping row ii and row jj of the grid. For all possible swaps, compute the sum of f(i,j)f(i,j), i.e., you need to compute:

1i<jnf(i,j)\sum_{1\leq i<j\leq n}f(i,j)

Note that computing ff 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 nn. It is guaranteed that nn is even.

The next line contains nn space-separated integers p1,p2,,pnp_1, p_2, \dots, p_n, meaning the nn key points are located at (1,p1),(2,p2),,(n,pn)(1,p_1),(2,p_2),\dots,(n,p_n). It is guaranteed that pp 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 66 grids correspond to all possible swaps (in order: swapping (1,2),(1,3),(1,4),(2,3),(2,4),(3,4)(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)). 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 2+2+0+0+2+2=82+2+0+0+2+2=8.

Constraints.

For all testdata, it is guaranteed that 2n2×1052\leq n\leq 2\times 10^5 and nn is even, and pp is a permutation.

Bundled test, with a total of 4 subtasks. The limits are:

  • Subtask 1 (12 pts): n10n\leq 10.
  • Subtask 2 (19 pts): n200n\leq 200.
  • Subtask 3 (34 pts): n2000n\leq 2000.
  • Subtask 4 (35 pts): no additional constraints.

Translated by ChatGPT 5