#P1975. [国家集训队] 排队

    ID: 921 远端评测题 1000ms 125MiB 尝试: 1 已通过: 1 难度: 5 上传者: 标签>WC/CTSC/集训队树套树块状链表,块状数组,分块

[国家集训队] 排队

Description

Sit in a row and eat fruit; the fruit is sweet and tasty, everyone smiles happily. One for you, one for me; the big one goes to you, the small one stays with me. After we finish eating, we sing a song, and everyone is cheerful.

The children of Red Star Kindergarten line up in a long queue to eat fruit. However, because their heights differ, the queue looks uneven and not very neat. Let the height of the ii-th child be hih_i.

Each time, the teacher selects two children and swaps their positions. Please help compute the number of inversions in the sequence after each swap. For the teacher’s convenience, you should also output the inversion count of the sequence before any swaps are performed.

Description

Input Format

  • The first line contains a positive integer nn, the number of children.
  • The second line contains nn space-separated positive integers h1,h2,,hnh_1, h_2, \dots, h_n, representing the heights in the initial queue.
  • The third line contains a positive integer mm, the number of swap operations.
  • The following mm lines each contain two positive integers ai,bia_i, b_i, indicating that the children at positions aia_i and bib_i are swapped.

Output Format

Output m+1m+1 lines.

  • Line 1: the number of inversions in the sequence before any swap.
  • For i=1,2,,mi = 1, 2, \dots, m, line i+1i+1: the number of inversions in the sequence after performing swap ii.
3
130 150 140
2
2 3
1 3
1
0
3

Hint

[Sample explanation]
Before any operation, (2,3)(2,3) is an inversion.
After operation 1, the sequence is 130 140 150130 \ 140 \ 150, and there are no inversions.
After operation 2, the sequence is 150 140 130150 \ 140 \ 130, and (1,2)(1,2), (1,3)(1,3), (2,3)(2,3) are inversions, totaling 33.

[Constraints]
For 10%10\% of the testdata, n,m15n, m \le 15.
For 25%25\% of the testdata, n,m200n, m \le 200.
Additionally, for 25%25\% of the testdata, all hih_i are distinct.
Additionally, for 15%15\% of the testdata, 110hi160110 \le h_i \le 160.
These two types of testdata are disjoint.

For 100%100\% of the testdata, 1m2×1031 \le m \le 2 \times 10^3, 1n2×1041 \le n \le 2 \times 10^4, 1hi1091 \le h_i \le 10^9, aibia_i \ne b_i, and 1ai,bin1 \le a_i, b_i \le n.

Translated by ChatGPT 5