PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000224. Xếp hàng và nghịch thế động (Dynamic Inversions After Swaps)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn students standing in a line. Let hih_i be the height of the student currently at position ii. Different students may have equal heights.

Consider every pair of positions (i,j)(i,j) with 1≤i<j≤n1\le i<j\le n. The pair forms an inversion exactly when hi>hjh_i>h_j: the student in front is taller than the student behind. Two equal heights do not form an inversion.

A supervisor performs mm operations in order. Operation tt swaps the students currently at positions ata_t and btb_t; 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 nn.
  • The second line contains exactly nn positive integers h1,h2,…,hnh_1,h_2,\ldots,h_n, their initial heights in line order.
  • The third line contains the number of swaps mm.
  • Each of the following mm lines contains two distinct positions at,bta_t,b_t that are swapped in operation tt.

Output

Print exactly m+1m+1 lines, each containing one nonnegative integer. The first line is the inversion count before any operation. For 1≤t≤m1\le t\le m, line t+1t+1 is the inversion count immediately after operation tt.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; 1≤m≤151\le m\le 15; 1≤hi≤1091\le h_i\le 10^9 for every ii; 1≤at,bt≤n1\le a_t,b_t\le n and at≠bta_t\ne b_t for every 1≤t≤m1\le t\le m.
  • Subtask 2 (30 points): 1≤n≤2001\le n\le 200; 1≤m≤2001\le m\le 200; 1≤hi≤1091\le h_i\le 10^9 for every ii; 1≤at,bt≤n1\le a_t,b_t\le n and at≠bta_t\ne b_t for every 1≤t≤m1\le t\le m.
  • Subtask 3 (50 points): 1≤n≤2⋅1041\le n\le 2\cdot 10^4; 1≤m≤2⋅1031\le m\le 2\cdot 10^3; 1≤hi≤1091\le h_i\le 10^9 for every ii; 1≤at,bt≤n1\le a_t,b_t\le n and at≠bta_t\ne b_t for every 1≤t≤m1\le t\le m.

Examples

Input

3
130 150 140
2
2 3
1 3

Output

1
0
3

Explanation

Initially the heights are [130,150,140][130,150,140]. Only the pair of positions (2,3)(2,3) forms an inversion because 150>140150>140, so the first output line is 1.

  • Swapping positions 22 and 33 gives [130,140,150][130,140,150]. Every height is now in nondecreasing order, so there are 00 inversions; the second line is 0.
  • Next, swapping positions 11 and 33 of the current line gives [150,140,130][150,140,130]. All three pairs (1,2)(1,2), (1,3)(1,3), and (2,3)(2,3) 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.