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 time. Then insertion sort can sort an array of length in time. Suppose these numbers are stored in . 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 of length , with indices starting from , and all elements are non-negative integers. Little Z needs to support operations on array . There are two types of operations, with parameters as follows:
: This is the first type of operation. It modifies the value of the -th element of , i.e. , to . It is guaranteed that , . Note that this operation changes the array elements. The modified array will be kept and will affect subsequent operations.
: This is the second type of operation. Suppose Teacher H sorts array according to the pseudocode above. You need to tell Teacher H the position of the original -th element of , i.e. , in the new array after sorting. It is guaranteed that . 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 operations does not exceed .
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 , representing the array length and the number of operations.
The second line contains space-separated non-negative integers, where the -th non-negative integer represents .
The next lines each contain positive integers, representing one operation. The operation format is described in Description.
Output Format
For each query of type , 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 , 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 , respectively.
Note that although 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 .
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 .
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 .
Constraints
For all testdata, , , , .
For all testdata, among all operations, there are at most operations of type .
The additional constraints and scores for each test point are shown in the table below.
| Test Point | Special Property | ||
|---|---|---|---|
| None | |||
| It is guaranteed that all input are pairwise distinct | |||
| None | |||
| It is guaranteed that all input are pairwise distinct | |||
| None | |||
Translated by ChatGPT 5