1785: 插入排序

Memory Limit:128 MB Time Limit:1.000 S
Judge Style:Text Compare Creator:
Submit:1 Solved:0

Description

插入排序是一种非常常见且简单的排序算法。小 Z 是一名大一的新生,今天 H 老师刚刚在上课的时候讲了插入排序算法。

假设比较两个元素的时间为 lns="http://www.w3.org/1998/Math/MathML">\mathcal O(1),则插入排序可以以 lns="http://www.w3.org/1998/Math/MathML">\mathcal O(n^2) 的时间复杂度完成长度为 lns="http://www.w3.org/1998/Math/MathML">n 的数组的排序。不妨假设这 lns="http://www.w3.org/1998/Math/MathML">n 个数字分别存储在 lns="http://www.w3.org/1998/Math/MathML">a_1, a_2, \ldots, a_n 之中,则如下伪代码给出了插入排序算法的一种最简单的实现方式:

这下面是 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;
		} 

这下面是 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; 

为了帮助小 Z 更好的理解插入排序,小 Z 的老师 H 老师留下了这么一道家庭作业:

H 老师给了一个长度为 lns="http://www.w3.org/1998/Math/MathML">n 的数组 lns="http://www.w3.org/1998/Math/MathML">a,数组下标从 lns="http://www.w3.org/1998/Math/MathML">1 开始,并且数组中的所有元素均为非负整数。小 Z 需要支持在数组 lns="http://www.w3.org/1998/Math/MathML">a 上的 lns="http://www.w3.org/1998/Math/MathML">Q 次操作,操作共两种,参数分别如下:

lns="http://www.w3.org/1998/Math/MathML">1~x~v:这是第一种操作,会将 lns="http://www.w3.org/1998/Math/MathML">a 的第 lns="http://www.w3.org/1998/Math/MathML">x 个元素,也就是 lns="http://www.w3.org/1998/Math/MathML">a_x 的值,修改为 lns="http://www.w3.org/1998/Math/MathML">v。保证 lns="http://www.w3.org/1998/Math/MathML">1 \le x \le nlns="http://www.w3.org/1998/Math/MathML">1 \le v \le 10^9注意这种操作会改变数组的元素,修改得到的数组会被保留,也会影响后续的操作

lns="http://www.w3.org/1998/Math/MathML">2~x:这是第二种操作,假设 H 老师按照上面的伪代码对 lns="http://www.w3.org/1998/Math/MathML">a 数组进行排序,你需要告诉 H 老师原来 lns="http://www.w3.org/1998/Math/MathML">a 的第 lns="http://www.w3.org/1998/Math/MathML">x 个元素,也就是 lns="http://www.w3.org/1998/Math/MathML">a_x,在排序后的新数组所处的位置。保证 lns="http://www.w3.org/1998/Math/MathML">1 \le x \le n注意这种操作不会改变数组的元素,排序后的数组不会被保留,也不会影响后续的操作

H 老师不喜欢过多的修改,所以他保证类型 lns="http://www.w3.org/1998/Math/MathML">1 的操作次数不超过 lns="http://www.w3.org/1998/Math/MathML">5000

小 Z 没有学过计算机竞赛,因此小 Z 并不会做这道题。他找到了你来帮助他解决这个问题。

Input

第一行,包含两个正整数 lns="http://www.w3.org/1998/Math/MathML">n, Q,表示数组长度和操作次数。

第二行,包含 lns="http://www.w3.org/1998/Math/MathML">n 个空格分隔的非负整数,其中第 lns="http://www.w3.org/1998/Math/MathML">i 个非负整数表示 lns="http://www.w3.org/1998/Math/MathML">a_i

接下来 lns="http://www.w3.org/1998/Math/MathML">Q 行,每行 lns="http://www.w3.org/1998/Math/MathML">2 \sim 3 个正整数,表示一次操作,操作格式见【题目描述】。

Output

对于每一次类型为 lns="http://www.w3.org/1998/Math/MathML">2 的询问,输出一行一个正整数表示答案。

Sample Input Copy

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

Sample Output Copy

1
1
2

HINT

【样例解释 #1】

在修改操作之前,假设 H 老师进行了一次插入排序,则原序列的三个元素在排序结束后所处的位置分别是 lns="http://www.w3.org/1998/Math/MathML">3, 2, 1

在修改操作之后,假设 H 老师进行了一次插入排序,则原序列的三个元素在排序结束后所处的位置分别是 lns="http://www.w3.org/1998/Math/MathML">3, 1, 2

注意虽然此时 lns="http://www.w3.org/1998/Math/MathML">a_2 = a_3,但是我们不能将其视为相同的元素

【样例 #2】

见附件中的 sort/sort2.in 与 sort/sort2.ans

该测试点数据范围同测试点 lns="http://www.w3.org/1998/Math/MathML">1 \sim 2

【样例 #3】

见附件中的 sort/sort3.in 与 sort/sort3.ans

该测试点数据范围同测试点 lns="http://www.w3.org/1998/Math/MathML">3 \sim 7

【样例 #4】

见附件中的 sort/sort4.in 与 sort/sort4.ans

该测试点数据范围同测试点 lns="http://www.w3.org/1998/Math/MathML">12 \sim 14

【数据范围】

对于所有测试数据,满足 lns="http://www.w3.org/1998/Math/MathML">1 \le n \le 8000lns="http://www.w3.org/1998/Math/MathML">1 \le Q \le 2 \times {10}^5lns="http://www.w3.org/1998/Math/MathML">1 \le x \le nlns="http://www.w3.org/1998/Math/MathML">1 \le v,a_i \le 10^9

对于所有测试数据,保证在所有 lns="http://www.w3.org/1998/Math/MathML">Q 次操作中,至多有 lns="http://www.w3.org/1998/Math/MathML">5000 次操作属于类型一。

各测试点的附加限制及分值如下表所示。

测试点 lns="http://www.w3.org/1998/Math/MathML">n \le lns="http://www.w3.org/1998/Math/MathML">Q \le 特殊性质
lns="http://www.w3.org/1998/Math/MathML">1 \sim 4 lns="http://www.w3.org/1998/Math/MathML">10 lns="http://www.w3.org/1998/Math/MathML">10
lns="http://www.w3.org/1998/Math/MathML">5 \sim 9 lns="http://www.w3.org/1998/Math/MathML">300 lns="http://www.w3.org/1998/Math/MathML">300
lns="http://www.w3.org/1998/Math/MathML">10 \sim 13 lns="http://www.w3.org/1998/Math/MathML">1500 lns="http://www.w3.org/1998/Math/MathML">1500
lns="http://www.w3.org/1998/Math/MathML">14 \sim 16 lns="http://www.w3.org/1998/Math/MathML">8000 lns="http://www.w3.org/1998/Math/MathML">8000 保证所有输入的 lns="http://www.w3.org/1998/Math/MathML">a_i,v 互不相同
lns="http://www.w3.org/1998/Math/MathML">17 \sim 19 lns="http://www.w3.org/1998/Math/MathML">8000 lns="http://www.w3.org/1998/Math/MathML">8000
lns="http://www.w3.org/1998/Math/MathML">20 \sim 22 lns="http://www.w3.org/1998/Math/MathML">8000 lns="http://www.w3.org/1998/Math/MathML">2 \times 10^5 保证所有输入的 lns="http://www.w3.org/1998/Math/MathML">a_i,v 互不相同
lns="http://www.w3.org/1998/Math/MathML">23 \sim 25 lns="http://www.w3.org/1998/Math/MathML">8000 lns="http://www.w3.org/1998/Math/MathML">2 \times 10^5