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

#SRD0000026. Đếm nghịch thế khi thay đổi phần tử (Inversion Counting)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm nghịch thế khi thay đổi phần tử (Inversion Counting)

Inversion Counting

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn positive integers a1,…,ana_1,\ldots,a_n. Process mm assignments (x,y)(x,y), each setting ax=ya_x=y. Immediately after each assignment, output the number of inversions (i,j)(i,j) with i<ji<j and ai>aja_i>a_j across the entire current array. Equal values do not form inversions.

Input

The first line is nn, followed by nn initial values. The third line contains mm. Each of the next mm lines gives x,yx,y.

Output

Print exactly mm lines, with the inversion count after assignment tt on line tt.

Subtasks

  • Subtask 1 (10 points): 1≤n≤501\le n\le 50; 1≤m≤601\le m\le 60; 1≤ai,y≤5⋅1041\le a_i,y\le 5\cdot10^4; 1≤x≤n1\le x\le n. Reassigning the same value and duplicates are allowed.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤m≤3001\le m\le 300; 1≤ai,y≤5⋅1041\le a_i,y\le 5\cdot10^4; 1≤x≤n1\le x\le n. Reassigning the same value and duplicates are allowed.
  • Subtask 3 (30 points): 1≤n≤300001\le n\le 30000; 1≤m≤25001\le m\le 2500; 1≤ai,y≤5⋅1041\le a_i,y\le 5\cdot10^4; 1≤x≤n1\le x\le n. Reassigning the same value and duplicates are allowed.
  • Subtask 4 (40 points): 1≤n≤2500001\le n\le 250000; 1≤m≤100001\le m\le 10000; 1≤ai,y≤5⋅1041\le a_i,y\le 5\cdot10^4; 1≤x≤n1\le x\le n. Reassigning the same value and duplicates are allowed.

Examples

Input

10
2 6 6 4 7 6 3 5 9 1
7
8 8
5 1
5 6
10 5
7 1
10 10
4 6

Output

17
18
16
13
14
8
6

Explanation

The example contains 1010 values and 77 assignments. After the first assignment 8 8, the array has 1717 inversions. The subsequent inversion totals are 18,16,13,14,8,618,16,13,14,8,6, always measured after the current assignment.