#FT0000224. Xếp hàng và nghịch thế động (Dynamic Inversions After Swaps)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xếp hàng và nghịch thế động (Dynamic Inversions After Swaps)
Dynamic Inversions After Swaps
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
There are students standing in a line. Let be the height of the student currently at position . Different students may have equal heights.
Consider every pair of positions with . The pair forms an inversion exactly when : the student in front is taller than the student behind. Two equal heights do not form an inversion.
A supervisor performs operations in order. Operation swaps the students currently at positions and ; their heights move with them. The two positions are always distinct.
Print the inversion count of the initial line, followed by the inversion count after each operation. Every operation acts on the result of the preceding operations.
Input
- The first line contains the number of students .
- The second line contains exactly positive integers , their initial heights in line order.
- The third line contains the number of swaps .
- Each of the following lines contains two distinct positions that are swapped in operation .
Output
Print exactly lines, each containing one nonnegative integer. The first line is the inversion count before any operation. For , line is the inversion count immediately after operation .
Subtasks
- Subtask 1 (20 points): ; ; for every ; and for every .
- Subtask 2 (30 points): ; ; for every ; and for every .
- Subtask 3 (50 points): ; ; for every ; and for every .
Examples
Input
3
130 150 140
2
2 3
1 3
Output
1
0
3
Explanation
Initially the heights are . Only the pair of positions forms an inversion because , so the first output line is 1.
- Swapping positions and gives . Every height is now in nondecreasing order, so there are inversions; the second line is
0. - Next, swapping positions and of the current line gives . All three pairs , , and are inversions; the third line is
3.
Since the initial state and both resulting states must be reported, the answer consists of 1, 0, 3.