#FT0000095. Sắp xếp nổi bọt trên tiền tố (Prefix Bubble Sort)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp nổi bọt trên tiền tố (Prefix Bubble Sort)
Prefix Bubble Sort
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of .
For an integer between and , operation works as follows: visit in increasing order. At each step, swap the current and if ; otherwise do nothing. An operation performs exactly one left-to-right pass over the first positions, rather than repeatedly sorting the prefix.
You are also given a non-decreasing sequence , with . Starting from the initial permutation, execute operations in order. After each operation, output the current number of inversions.
An inversion is a pair of positions with and .
Input
Line 1 contains . Line 2 contains integers . Line 3 contains . Line 4 contains integers in execution order.
Output
Print exactly lines. Line contains the inversion count after operation has completed.
Subtasks
- Subtask 1 (20 points): ; ; is a permutation of ; .
- Subtask 2 (30 points): ; ; is a permutation of ; .
- Subtask 3 (50 points): ; ; is a permutation of ; .
Examples
Example 1
Input
6
3 2 4 1 6 5
2
4 6
Output
3
1
Explanation
Initially . Operation swaps and , leaves and , then swaps and , producing . Its three inversions are the pairs of values , so the first answer is 3. Operation then produces , containing only the inversion , so the second answer is 1.
Example 2
Input
3
3 2 1
2
2 3
Output
2
1
Explanation
Initially has three inversions. Operation swaps the first pair, producing with two inversions. Operation leaves unchanged and swaps , producing with one inversion. The two answers are 2 and 1.