#P11162. [OOI 2025] Order Statistics

[OOI 2025] Order Statistics

题目描述

You are given an array a1a_1, a2a_2, \ldots, ana_n, consisting of integers, as well as integers kk and mm. The following operation is performed mm times on the array:

  • Select i1i_1, i2i_2, \ldots, iki_k --- the positions of the kk largest elements in the array aa. If two elements are equal, the element that appears earlier in the array is considered larger.
  • Decrease ai1a_{i_1}, ai2a_{i_2}, \ldots, aika_{i_k} by 11.

For xx from 11 to nn, let Fm,k(x)F_{m,k}(x) denote the value of the xx-th order statistic in the array obtained from aa after applying the operation mm times with the given parameter kk. For xx from 11 to nn, the xx-th order statistic of the array a1,a2,,ana_1,a_2,\ldots,a_n is the element that would be in position xx if the array aa were sorted in non-decreasing order.

For all l,rl, r such that 1lrn1 \le l \le r \le n, let Sm,k(l,r)S_{m,k}(l,r) denote the sum of Fm,k(x)F_{m,k}(x) for all integers xx from ll to rr. More formally:

Sm,k(l,r)=x=lrFm,k(x)S_{m,k}(l,r)=\sum_{x=l}^r F_{m,k}(x)

You are given integers m0m_0 and k0k_0. You must compute the values of Fm0,k0(x)F_{m_0,k_0}(x) for all xx from 11 to nn.

After that, you must process qq queries. The jj-th query (1jq1 \le j \le q) can be one of three types:

  • Compute the value of Fmj,kj(xj)F_{m_j,k_j}(x_j).
  • Change the value of apja_{p_j} to vjv_j.
  • Compute the value of Smj,kj(lj,rj)S_{m_j,k_j}(l_j,r_j).

All calculations of the functions FF and SS are performed independently each time and do not change the array. All changes to the array in queries of the second type are preserved for subsequent queries.

输入格式

The first line contains four integers nn, m0m_0, k0k_0, and qq (1n2000001 \le n \le 200\,000, 0m01090 \le m_0 \le 10^9, 1k0n1 \le k_0 \le n, 0q2000000 \le q \le 200\,000) --- the length of the array aa; the number of operations; the number of largest elements decreased in each operation, and the number of queries.

The second line contains nn integers a1, a2, , ana_1,\ a_2,\ \ldots,\ a_n (109ai109-10^9 \le a_i \le 10^9, 1in1 \le i \le n) --- the elements of the array aa.

The next qq lines contain the queries. In the jj-th query, the first number is tjt_j (1tj31 \le t_j \le 3) --- the type of the jj-th query.

  • If tj=1t_j=1, then the next line contains three integers mjm_j, kjk_j, and xjx_j (0mj1090 \le m_j \le 10^9, 1kj,xjn1 \le k_j,x_j \le n)--- the parameters of the first type query.
  • If tj=2t_j=2, then the next line contains two integers pjp_j and vjv_j (1pjn1 \le p_j \le n, 109vj109-10^9 \le v_j \le 10^9)--- the parameters of the second type query.
  • If tj=3t_j=3, then the next line contains four integers mjm_j, kjk_j, ljl_j, and rjr_j (0mj1090 \le m_j \le 10^9, 1kj,lj,rjn1 \le k_j,l_j,r_j \le n, ljrjl_j \le r_j)--- the parameters of the third type query.

输出格式

In the first line, output nn integers $F_{m_0,k_0}(1),F_{m_0,k_0}(2),\ldots,F_{m_0,k_0}(n)$.

Then, for each first type query, output the value Fmj,kj(xj)F_{m_j, k_j}(x_j) on a separate line, and for each third type query, output the value Smj,kj(lj,rj)S_{m_j,k_j}(l_j,r_j)~--- the answer to the jj-th query.

输入输出样例 #1

输入 #1

8 3 2 16
3 1 2 -1 0 2 -1 4
3 3 2 2 6
1 3 2 4
3 4 5 3 5
1 4 5 6
2 5 -1
2 6 3
1 3 2 1
1 3 2 3
1 3 2 4
1 3 2 8
1 0 5 6
2 1 5
3 1 3 7 8
3 2 3 5 8
3 3 3 4 7
3 4 3 4 7

输出 #1

-1 -1 0 1 1 1 1 2
2
1
-4
-1
-1
-1
1
2
3
7
8
4
2

说明/提示

In the example, n=8n=8, m0=3m_0=3, k0=2k_0=2, q=16q=16. Initially, the array aa is [3,1,2,1,0,2,1,4][3, 1, 2, -1, 0, 2, -1, 4]. Let's see how the array will change if we apply the operation m0m_0 times with parameter k0k_0:

  • The array is [3,1,2,1,0,2,1,4][3,1,2,-1,0,2,-1,4]. The two largest elements are in positions 11 and 88. They are decreased by 11, after which the array becomes [2,1,2,1,0,2,1,3][2,1,2,-1,0,2,-1,3].
  • The array is [2,1,2,1,0,2,1,3][2,1,2,-1,0,2,-1,3]. The two largest elements are in positions 11 and 88. They are decreased by 11, after which the array becomes [1,1,2,1,0,2,1,2][1,1,2,-1,0,2,-1,2].
  • The array is [1,1,2,1,0,2,1,2][1,1,2,-1,0,2,-1,2]. The two largest elements are in positions 33 and 66. They are decreased by 11, after which the array becomes [1,1,1,1,0,1,1,2][1,1,1,-1,0,1,-1,2].

We find that after applying the operation 33 times with parameter 22 to the array aa, it becomes [1,1,1,1,0,1,1,2][1,1,1,-1,0,1,-1,2]. If this array is sorted, it results in the array [1,1,0,1,1,1,1,2][-1,-1,0,1,1,1,1,2]. Thus, the order statistics are F3,2(1)=1F_{3,2}(1)=-1, F3,2(2)=1F_{3,2}(2)=-1, F3,2(3)=0F_{3,2}(3)=0, F3,2(4)=1F_{3,2}(4)=1, F3,2(5)=1F_{3,2}(5)=1, F3,2(6)=1F_{3,2}(6)=1, F3,2(7)=1F_{3,2}(7)=1, F3,2(8)=2F_{3,2}(8)=2.

In the example, we need to process 1616 queries; let's analyze the first 1010 of them in detail:

  • The first query is of type t1=3t_1=3, with parameters m1=3m_1=3, k1=2k_1=2, l1=2l_1=2, r1=6r_1=6 and requires computing the value of S3,2(2,6)S_{3,2}(2,6). We have already computed the values of F3,2(x)F_{3,2}(x) for xx from 11 to 88, from which we find the answer to the query:
$$S_{3,2}(2,6)=F_{3,2}(2)+F_{3,2}(3)+F_{3,2}(4)+F_{3,2}(5)+F_{3,2}(6)=(-1)+0+1+1+1=2 $$
  • The second query is of type t2=1t_2=1, with parameters m2=3m_2=3, k2=2k_2=2, x2=4x_2=4 and requires computing the value of F3,2(4)F_{3,2}(4). We have already computed it, and it equals 11.
  • The third query is of type t3=3t_3=3, with parameters m3=4m_3=4, k3=5k_3=5, l3=3l_3=3, r3=5r_3=5 and requires computing the value of S4,5(3,5)S_{4,5}(3,5), that is, to calculate the sum of the order statistics from the third to the fifth in the array obtained from aa after applying the operation m3=4m_3=4 times with parameter k3=5k_3=5. At the time of the third query, the array aa is [3,1,2,1,0,2,1,4][3, 1, 2, -1, 0, 2, -1, 4]. The five largest elements are in positions 1,2,3,6,81,2,3,6,8. Decreasing them by 11, we get the array [2,0,1,1,0,1,1,3][2,0,1,-1,0,1,-1,3]. Applying the operation three more times, we get the array [1,2,2,2,1,1,1,0][-1,-2,-2,-2,-1,-1,-1,0]. After sorting, it becomes [2,2,2,1,1,1,1,0][-2,-2,-2,-1,-1,-1,-1,0]. Thus, the answer to the query is
$$S_{4,5}(3,5)=F_{4,5}(3)+F_{4,5}(4)+F_{4,5}(5)=(-2)+(-1)+(-1)=-4 $$
  • The fourth query is of type t4=1t_4=1, with parameters m4=4m_4=4, k4=5k_4=5, x4=6x_4=6. After applying four operations with parameter 55 and sorting the array aa, it will be [2,2,2,1,1,1,1,0][-2,-2,-2,-1,-1,-1,-1,0], so the sixth order statistic is 1-1.
  • The fifth query is of type t5=2t_5=2, with parameters p5=5p_5=5 and v5=1v_5=-1. It changes the value of a5a_5 to 1-1, after which the array aa becomes [3,1,2,1,1,2,1,4][3,1,2,-1,-1,2,-1,4].
  • The sixth query is of type t6=2t_6=2, with parameters p6=6p_6=6 and v6=3v_6=3. It changes the value of a6a_6 to 33, after which the array aa becomes [3,1,2,1,1,3,1,4][3,1,2,-1,-1,3,-1,4].
  • The seventh query requires finding the value of F3,2(1)F_{3,2}(1). At the time of the seventh query, the array aa is [3,1,2,1,1,3,1,4][3,1,2,-1,-1,3,-1,4]. After applying 33 times the operation with parameter 22, it will be [1,1,1,1,1,2,1,2][1, 1, 1, -1, -1, 2, -1, 2]. The first order statistic of this array is 1-1.
  • The eighth, ninth, and tenth queries require finding the values of F3,2(3)F_{3,2}(3), F3,2(4)F_{3,2}(4) and F3,2(8)F_{3,2}(8), that is, the third, fourth, and eighth order statistics in the array [1,1,1,1,1,2,1,2][1, 1, 1, -1, -1, 2, -1, 2]. They are equal to 1-1, 11, and 22, respectively.

Scoring

The tests for this problem consist of eleven groups. Points for each group are given only if all tests of the group and all tests of the required groups are passed. Please note that passing the example tests is not required for some groups. Offline-evaluation\textbf{Offline-evaluation} means that the results of testing your solution on this group will only be available after the end of the competition.

If there are constraints on mm or kk in a subtask, they apply to both m0m_0 and k0k_0, as well as to the parameters of all first and third type queries.

Group Points Additional constraints: nn mm kk qq Required Groups Comment
0 -- -- -- -- Examples.
1 4 n1000n \le 1000 m1000m \le 1000 q=0q=0 --
2 5 -- -- k=1k=1
3 6 q100000q \le 100\,000 2 tj=1t_j=1 for all queries
4 7 2, 3 tj3t_j \ne 3 for all queries
5 11 k=2k=2 q=0q=0 -- --
6 9 m106m \le 10^6 -- 1
7 10 n1000n \le 1000 --
8 7 -- 1, 2, 5 -- 7
9 11 q100000q \le 100\,000 1 -- 3, 5 -- 8 tj=1t_j=1 for all queries
10 13 1 -- 3, 5 -- 9 tj2t_j\ne 2 for all queries
11 9 0 -- 10 --
12 8 -- 0 -- 11 Offline-evaluation.