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

#SRD0000031. Nghịch thế khi xóa phần tử (Dynamic Inversion)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nghịch thế khi xóa phần tử (Dynamic Inversion)

Dynamic Inversion

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation a1,…,ana_1,\ldots,a_n of 1,…,n1,\ldots,n. There are mm deletion instructions. Each instruction names the value xx to remove from the current sequence, not its position. No value is removed more than once. Before removing xx, print the current number of inversions (i,j)(i,j) with i<ji<j and ai>aja_i>a_j; then delete that value.

Input

The first line contains n,mn,m, followed by the nn permutation values and then mm values to be deleted. The PHOJ version uses one data set in this format.

Output

Print exactly mm inversion counts, each measured immediately before the corresponding deletion.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60; 1≤m≤501\le m\le 50; m≤nm\le n; aa is a permutation of 1,…,n1,\ldots,n; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); deleted values are distinct and lie in [1,n][1,n].
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤m≤10001\le m\le 1000; m≤nm\le n; aa is a permutation of 1,…,n1,\ldots,n; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); deleted values are distinct and lie in [1,n][1,n].
  • Subtask 3 (30 points): 1≤n≤200001\le n\le 20000; 1≤m≤100001\le m\le 10000; m≤nm\le n; aa is a permutation of 1,…,n1,\ldots,n; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); deleted values are distinct and lie in [1,n][1,n].
  • Subtask 4 (40 points): 1≤n≤2000001\le n\le 200000; 1≤m≤1000001\le m\le 100000; m≤nm\le n; aa is a permutation of 1,…,n1,\ldots,n; 1≤m≤min⁡(n,105)1\le m\le\min(n,10^5); deleted values are distinct and lie in [1,n][1,n].

Examples

Input

5 4
1
5
3
4
2
5
1
4
2

Output

5
2
2
1

Explanation

The initial permutation [1,5,3,4,2][1,5,3,4,2] has five inversions; print 55 before deleting value 55. The remaining [1,3,4,2][1,3,4,2] has two; print 22 before deleting 11. The final two counts are 22 and 11.