#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 A0,A1,,AN1A_0, A_1, \ldots, A_{N-1} of length NN 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 i=0,1,,N2i = 0, 1, \ldots, N - 2 in this order, if Ai>Ai+1A_i > A_{i+1}, 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 AA, we define the number of bubble sort scans as the number of scans needed to sort AA using the algorithm above.

JOI has a sequence AA of length NN. He plans to process QQ queries that modify the values in AA. More precisely, in the (j+1)(j+1)-th query (0jQ1)(0 \le j \le Q - 1), the value of AXjA_{X_j} will be changed to VjV_j.

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 N,QN, Q.

The second line contains NN integers A0,A1,,AN1A_0, A_1, \ldots, A_{N-1}.

The next QQ lines each contain two integers Xj,VjX_j, V_j.

Output Format

Output QQ lines. The (j+1)(j+1)-th line (0jQ1)(0 \le j \le Q - 1) should be the number of bubble sort scans for the sequence after processing the (j+1)(j+1)-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 N=4N = 4, A={1,2,3,4}A = \{1, 2, 3, 4\}, and Q=2Q = 2 queries: X={0,2},V={3,1}X = \{0, 2\}, V = \{3, 1\}.

  1. For the first query, the value of A0A_0 becomes 33. We get A={3,2,3,4}A = \{3, 2, 3, 4\}.
  2. For the second query, the value of A2A_2 becomes 11. We get A={3,2,1,4}A = \{3, 2, 1, 4\}.

Perform bubble sort on A={3,2,3,4}A = \{3, 2, 3, 4\}:

  • AA is not sorted, so the first scan starts. Since A0>A1A_0 > A_1, we swap them, and the sequence becomes A={2,3,3,4}A = \{2, 3, 3, 4\}. Since A1A2A_1 \le A_2, we do not swap them. Since A2A3A_2 \le A_3, we also do not swap them.
  • Now AA is sorted, so the bubble sort process ends.

Therefore, for A={3,2,3,4}A = \{3, 2, 3, 4\}, the number of bubble sort scans is 11.

Perform bubble sort on A={3,2,1,4}A = \{3, 2, 1, 4\}:

  • AA is not sorted, so the first scan starts. Since A0>A1A_0 > A_1, we swap them, and the sequence becomes A={2,3,1,4}A = \{2, 3, 1, 4\}. Since A1>A2A_1 > A_2, we swap them, and the sequence becomes A={2,1,3,4}A = \{2, 1, 3, 4\}. Since A2A3A_2 \le A_3, we also do not swap them.
  • AA is not sorted, so the second scan starts. Since A0>A1A_0 > A_1, we swap them, and the sequence becomes A={1,2,3,4}A = \{1, 2, 3, 4\}. Since A1A2A_1 \le A_2, we do not swap them. Since A2A3A_2 \le A_3, we also do not swap them.
  • Now AA is sorted, so the bubble sort process ends.

Therefore, for A={3,2,1,4}A = \{3, 2, 1, 4\}, the number of bubble sort scans is 22.

Constraints

There are four subtasks. The score and additional constraints for each subtask are as follows:

Subtask ID Score NN QQ A,VA, V
11 1717 1N2 0001 \le N \le 2\ 000 1Q2 0001 \le Q \le 2\ 000 1Ai,Vj1091 \le A_i, V_j \le 10^9
22 2121 1N8 0001 \le N \le 8\ 000 1Q8 0001 \le Q \le 8\ 000
33 2222 1N50 0001 \le N \le 50\ 000 1Q50 0001 \le Q \le 50\ 000 1Ai,Vj1001 \le A_i, V_j \le 100
44 4040 1N500 0001 \le N \le 500\ 000 1Q500 0001 \le Q \le 500\ 000 1Ai,Vj1091 \le A_i, V_j \le 10^9

Translated by ChatGPT 5