luogu#P7910. [CSP-J 2021] 插入排序

[CSP-J 2021] 插入排序

Problem Description

Insertion sort is a very common and simple sorting algorithm. Little Z is a freshman in college, and today in class, Teacher H has just explained the insertion sort algorithm.

Assume that comparing two elements takes O(1)\mathcal O(1) time. Then insertion sort can sort an array of length nn in O(n2)\mathcal O(n^2) time. Suppose these nn numbers are stored in a1,a2,,ana_1, a_2, \ldots, a_n. The following pseudocode gives one of the simplest implementations of insertion sort:

The following is sample code in C/C++:

for (int i = 1; i <= n; i++)
	for (int j = i; j >= 2; j--)
		if (a[j] < a[j-1]) {
			int t = a[j-1];
			a[j-1] = a[j];
			a[j] = t;
		}

The following is sample code in Pascal:

for i:=1 to n do
	for j:=i downto 2 do
		if a[j]<a[j-1] then
			begin
				t:=a[i];
				a[i]:=a[j];
				a[j]:=t;
			end;

To help Little Z better understand insertion sort, Teacher H left the following homework:

Teacher H gives an array aa of length nn, with indices starting from 11, and all elements are non-negative integers. Little Z needs to support QQ operations on array aa. There are two types of operations, with parameters as follows:

1 x v1~x~v: This is the first type of operation. It modifies the value of the xx-th element of aa, i.e. axa_x, to vv. It is guaranteed that 1xn1 \le x \le n, 1v1091 \le v \le 10^9. Note that this operation changes the array elements. The modified array will be kept and will affect subsequent operations.

2 x2~x: This is the second type of operation. Suppose Teacher H sorts array aa according to the pseudocode above. You need to tell Teacher H the position of the original xx-th element of aa, i.e. axa_x, in the new array after sorting. It is guaranteed that 1xn1 \le x \le n. Note that this operation does not change the array elements. The sorted array will not be kept and will not affect subsequent operations.

Teacher H does not like too many modifications, so he guarantees that the number of type 11 operations does not exceed 50005000.

Little Z has not studied competitive programming, so he cannot solve this problem. He comes to you for help.

Input Format

The first line contains two positive integers n,Qn, Q, representing the array length and the number of operations.

The second line contains nn space-separated non-negative integers, where the ii-th non-negative integer represents aia_i.

The next QQ lines each contain 232 \sim 3 positive integers, representing one operation. The operation format is described in Description.

Output Format

For each query of type 22, output one line with one positive integer as the answer.

3 4
3 2 1
2 3
1 3 2
2 2
2 3

1
1
2

Hint

Sample Explanation #1

Before the modification operation, suppose Teacher H performs one insertion sort. Then the positions of the three elements in the original sequence after sorting are 3,2,13, 2, 1, respectively.

After the modification operation, suppose Teacher H performs one insertion sort. Then the positions of the three elements in the original sequence after sorting are 3,1,23, 1, 2, respectively.

Note that although a2=a3a_2 = a_3 at this time, we cannot treat them as the same element.

Sample #2

See the attached files sort/sort2.in and sort/sort2.ans.

The Constraints for this test point are the same as test points 121 \sim 2.

Sample #3

See the attached files sort/sort3.in and sort/sort3.ans.

The Constraints for this test point are the same as test points 373 \sim 7.

Sample #4

See the attached files sort/sort4.in and sort/sort4.ans.

The Constraints for this test point are the same as test points 121412 \sim 14.

Constraints

For all testdata, 1n80001 \le n \le 8000, 1Q2×1051 \le Q \le 2 \times {10}^5, 1xn1 \le x \le n, 1v,ai1091 \le v,a_i \le 10^9.

For all testdata, among all QQ operations, there are at most 50005000 operations of type 11.

The additional constraints and scores for each test point are shown in the table below.

Test Point nn \le QQ \le Special Property
141 \sim 4 1010 None
595 \sim 9 300300
101310 \sim 13 15001500
141614 \sim 16 80008000 80008000 It is guaranteed that all input ai,va_i, v are pairwise distinct
171917 \sim 19 None
202220 \sim 22 2×1052 \times 10^5 It is guaranteed that all input ai,va_i, v are pairwise distinct
232523 \sim 25 None

Translated by ChatGPT 5