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

#FT0000217. Số nghịch thế khi xóa động (Dynamic Inversions)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số nghịch thế khi xóa động (Dynamic Inversions)

Dynamic Inversions

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation aa containing each integer from 11 to nn exactly once.

An inversion of the current array is a pair of positions (i,j)(i,j) with i<ji<j and ai>aja_i>a_j. Positions refer to the remaining sequence after previous removals, without gaps.

You will remove mm elements in a prescribed order, specified by their values. Each value is deleted at most once. Removing a value preserves the relative order of the remaining elements.

Before each removal, print the number of inversions in the current array. Inversions involving the element about to be removed must still be counted, because the removal has not yet happened.

Input

  • The first line contains integers n,mn,m: the initial permutation size and the number of deletions.
  • The following nn lines each contain one integer aia_i: the value originally located at position ii.
  • The following mm lines each contain one integer bjb_j: the value removed at deletion step jj. All bjb_j values are distinct and occur in the original permutation.

Output

Print exactly mm lines. Line jj contains the number of inversions immediately before deleting value bjb_j. The answer may exceed the range of a 3232-bit integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; 1≤m≤n1\le m\le n; aa is a permutation of 1,2,…,n1,2,\ldots,n; b1,…,bmb_1,\ldots,b_m are distinct and all belong to [1,n][1,n].
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤m≤min⁡(n,1000)1\le m\le\min(n,1000); aa is a permutation of 1,2,…,n1,2,\ldots,n; b1,…,bmb_1,\ldots,b_m are distinct and all belong to [1,n][1,n].
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤m≤min⁡(n,50000)1\le m\le\min(n,50000); aa is a permutation of 1,2,…,n1,2,\ldots,n; b1,…,bmb_1,\ldots,b_m are distinct and all belong to [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 is [1,5,3,4,2][1,5,3,4,2]. Answers are computed before performing their deletions:

  1. Before removing 55, the five inversions are (5,3)(5,3), (5,4)(5,4), (5,2)(5,2), (3,2)(3,2), (4,2)(4,2). Print 5; the remaining array becomes [1,3,4,2][1,3,4,2].
  2. Before removing 11, [1,3,4,2][1,3,4,2] has inversions (3,2)(3,2) and (4,2)(4,2). Print 2; the array becomes [3,4,2][3,4,2].
  3. Before removing 44, [3,4,2][3,4,2] has those same two inversions. Print 2; the array becomes [3,2][3,2].
  4. Before removing 22, [3,2][3,2] has exactly one inversion (3,2)(3,2). Print 1.

Thus the four lines are 5, 2, 2, and 1.