#P1337. 插入排序(2021)
插入排序(2021)
Description
插入排序是一种非常常见且简单的排序算法。小 Z 是一名大一的新生,今天 H 老师刚刚在上课的时候讲了插入排序算法。
假设比较两个元素的时间为 <math xmlns="http://www.w3.org/1998/Math/MathML">\mathcal O(1)O(1),则插入排序可以以 <math xmlns="http://www.w3.org/1998/Math/MathML">\mathcal O(n^2)O(n2) 的时间复杂度完成长度为 <math xmlns="http://www.w3.org/1998/Math/MathML">nn 的数组的排序。不妨假设这 <math xmlns="http://www.w3.org/1998/Math/MathML">nn 个数字分别存储在 <math xmlns="http://www.w3.org/1998/Math/MathML">a_1, a_2, \ldots, a_na1,a2,…,an 之中,则如下伪代码给出了插入排序算法的一种最简单的实现方式:
这下面是 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 老师给了一个长度为 <math xmlns="http://www.w3.org/1998/Math/MathML">nn 的数组 <math xmlns="http://www.w3.org/1998/Math/MathML">aa,数组下标从 <math xmlns="http://www.w3.org/1998/Math/MathML">11 开始,并且数组中的所有元素均为非负整数。小 Z 需要支持在数组 <math xmlns="http://www.w3.org/1998/Math/MathML">aa 上的 <math xmlns="http://www.w3.org/1998/Math/MathML">QQ 次操作,操作共两种,参数分别如下:
<math xmlns="http://www.w3.org/1998/Math/MathML">1~x~v 1 x v:这是第一种操作,会将 <math xmlns="http://www.w3.org/1998/Math/MathML">aa 的第 <math xmlns="http://www.w3.org/1998/Math/MathML">xx 个元素,也就是 <math xmlns="http://www.w3.org/1998/Math/MathML">a_xax 的值,修改为 <math xmlns="http://www.w3.org/1998/Math/MathML">vv。保证 <math xmlns="http://www.w3.org/1998/Math/MathML">1 \le x \le n1≤x≤n,<math xmlns="http://www.w3.org/1998/Math/MathML">1 \le v \le 10^91≤v≤109。注意这种操作会改变数组的元素,修改得到的数组会被保留,也会影响后续的操作。
<math xmlns="http://www.w3.org/1998/Math/MathML">2~x 2 x:这是第二种操作,假设 H 老师按照上面的伪代码对 <math xmlns="http://www.w3.org/1998/Math/MathML">aa 数组进行排序,你需要告诉 H 老师原来 <math xmlns="http://www.w3.org/1998/Math/MathML">aa 的第 <math xmlns="http://www.w3.org/1998/Math/MathML">xx 个元素,也就是 <math xmlns="http://www.w3.org/1998/Math/MathML">a_xax,在排序后的新数组所处的位置。保证 <math xmlns="http://www.w3.org/1998/Math/MathML">1 \le x \le n1≤x≤n。注意这种操作不会改变数组的元素,排序后的数组不会被保留,也不会影响后续的操作。
H 老师不喜欢过多的修改,所以他保证类型 <math xmlns="http://www.w3.org/1998/Math/MathML">11 的操作次数不超过 <math xmlns="http://www.w3.org/1998/Math/MathML">50005000。
小 Z 没有学过计算机竞赛,因此小 Z 并不会做这道题。他找到了你来帮助他解决这个问题。
Input Format
第一行,包含两个正整数 <math xmlns="http://www.w3.org/1998/Math/MathML">n, Qn,Q,表示数组长度和操作次数。
第二行,包含 <math xmlns="http://www.w3.org/1998/Math/MathML">nn 个空格分隔的非负整数,其中第 <math xmlns="http://www.w3.org/1998/Math/MathML">ii 个非负整数表示 <math xmlns="http://www.w3.org/1998/Math/MathML">a_iai。
接下来 <math xmlns="http://www.w3.org/1998/Math/MathML">QQ 行,每行 <math xmlns="http://www.w3.org/1998/Math/MathML">2 \sim 32∼3 个正整数,表示一次操作,操作格式见【题目描述】。
Output Format
对于每一次类型为 <math xmlns="http://www.w3.org/1998/Math/MathML">22 的询问,输出一行一个正整数表示答案。3 4
3 2 1
2 3
1 3 2
2 2
2 3
1
1
2
Hint
说明/提示
【样例解释 #1】
在修改操作之前,假设 H 老师进行了一次插入排序,则原序列的三个元素在排序结束后所处的位置分别是 <math xmlns="http://www.w3.org/1998/Math/MathML">3, 2, 13,2,1。
在修改操作之后,假设 H 老师进行了一次插入排序,则原序列的三个元素在排序结束后所处的位置分别是 <math xmlns="http://www.w3.org/1998/Math/MathML">3, 1, 23,1,2。
注意虽然此时 <math xmlns="http://www.w3.org/1998/Math/MathML">a_2 = a_3a2=a3,但是我们不能将其视为相同的元素。
【样例 #2】
见附件中的 sort/sort2.in
与 sort/sort2.ans
。
该测试点数据范围同测试点 <math xmlns="http://www.w3.org/1998/Math/MathML">1 \sim 21∼2。
【样例 #3】
见附件中的 sort/sort3.in
与 sort/sort3.ans
。
该测试点数据范围同测试点 <math xmlns="http://www.w3.org/1998/Math/MathML">3 \sim 73∼7。
【样例 #4】
见附件中的 sort/sort4.in
与 sort/sort4.ans
。
该测试点数据范围同测试点 <math xmlns="http://www.w3.org/1998/Math/MathML">12 \sim 1412∼14。
【数据范围】
对于所有测试数据,满足 <math xmlns="http://www.w3.org/1998/Math/MathML">1 \le n \le 80001≤n≤8000,<math xmlns="http://www.w3.org/1998/Math/MathML">1 \le Q \le 2 \times {10}^51≤Q≤2×105,<math xmlns="http://www.w3.org/1998/Math/MathML">1 \le x \le n1≤x≤n,<math xmlns="http://www.w3.org/1998/Math/MathML">1 \le v,a_i \le 10^91≤v,ai≤109。
对于所有测试数据,保证在所有 <math xmlns="http://www.w3.org/1998/Math/MathML">QQ 次操作中,至多有 <math xmlns="http://www.w3.org/1998/Math/MathML">50005000 次操作属于类型一。
各测试点的附加限制及分值如下表所示。
测试点 | <math xmlns="http://www.w3.org/1998/Math/MathML">n \len≤ | <math xmlns="http://www.w3.org/1998/Math/MathML">Q \leQ≤ | 特殊性质 |
---|---|---|---|
<math xmlns="http://www.w3.org/1998/Math/MathML">1 \sim 41∼4 | <math xmlns="http://www.w3.org/1998/Math/MathML">1010 | <math xmlns="http://www.w3.org/1998/Math/MathML">1010 | 无 |
<math xmlns="http://www.w3.org/1998/Math/MathML">5 \sim 95∼9 | <math xmlns="http://www.w3.org/1998/Math/MathML">300300 | <math xmlns="http://www.w3.org/1998/Math/MathML">300300 | 无 |
<math xmlns="http://www.w3.org/1998/Math/MathML">10 \sim 1310∼13 | <math xmlns="http://www.w3.org/1998/Math/MathML">15001500 | <math xmlns="http://www.w3.org/1998/Math/MathML">15001500 | 无 |
<math xmlns="http://www.w3.org/1998/Math/MathML">14 \sim 1614∼16 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | 保证所有输入的 <math xmlns="http://www.w3.org/1998/Math/MathML">a_i,vai,v 互不相同 |
<math xmlns="http://www.w3.org/1998/Math/MathML">17 \sim 1917∼19 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | 无 |
<math xmlns="http://www.w3.org/1998/Math/MathML">20 \sim 2220∼22 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | <math xmlns="http://www.w3.org/1998/Math/MathML">2 \times 10^52×105 | 保证所有输入的 <math xmlns="http://www.w3.org/1998/Math/MathML">a_i,vai,v 互不相同 |
<math xmlns="http://www.w3.org/1998/Math/MathML">23 \sim 2523∼25 | <math xmlns="http://www.w3.org/1998/Math/MathML">80008000 | <math xmlns="http://www.w3.org/1998/Math/MathML">2 \times 10^52×105 | 无 |
Source
四阶相关
在下列比赛中: