#P9596. [JOI Open 2018] 冒泡排序 2 / Bubble Sort 2
[JOI Open 2018] 冒泡排序 2 / Bubble Sort 2
Problem Description
Bubble sort is an algorithm for sorting a sequence. We want to sort a sequence of length in non-decreasing order. When two adjacent numbers are not in the correct order, bubble sort swaps these two numbers. This kind of swapping is performed while scanning the sequence. More precisely, in one scan, for in this order, if , then we swap these two numbers. It is well known that after a finite number of scans, any sequence can be sorted in non-decreasing order. For a sequence , we define the number of bubble sort scans as the number of scans needed to sort using the algorithm above.
JOI has a sequence of length . He plans to process queries that modify the values in . More precisely, in the -th query , the value of will be changed to .
JOI wants to know, after each modification, the number of bubble sort scans needed.
Input Format
On LOJ this is an interactive problem. For convenience, here it is judged in the traditional (non-interactive) format.
The first line contains two integers .
The second line contains integers .
The next lines each contain two integers .
Output Format
Output lines. The -th line should be the number of bubble sort scans for the sequence after processing the -th query.
4 2
1 2 3 4
0 3
2 1
1
2
11 12
11 4 13 6 7 3 5 12 4 10 11
8 11
4 4
6 20
0 2
7 2
3 18
5 9
0 6
8 8
9 4
0 8
6 18
5
5
5
4
6
6
6
7
7
7
7
7
Hint
Sample Explanation
Given a sequence of length , , and queries: .
- For the first query, the value of becomes . We get .
- For the second query, the value of becomes . We get .
Perform bubble sort on :
- is not sorted, so the first scan starts. Since , we swap them, and the sequence becomes . Since , we do not swap them. Since , we also do not swap them.
- Now is sorted, so the bubble sort process ends.
Therefore, for , the number of bubble sort scans is .
Perform bubble sort on :
- is not sorted, so the first scan starts. Since , we swap them, and the sequence becomes . Since , we swap them, and the sequence becomes . Since , we also do not swap them.
- is not sorted, so the second scan starts. Since , we swap them, and the sequence becomes . Since , we do not swap them. Since , we also do not swap them.
- Now is sorted, so the bubble sort process ends.
Therefore, for , the number of bubble sort scans is .
Constraints
There are four subtasks. The score and additional constraints for each subtask are as follows:
| Subtask ID | Score | |||
|---|---|---|---|---|
Translated by ChatGPT 5