#P14085. [ICPC 2023 Seoul R] Apricot Seeds

[ICPC 2023 Seoul R] Apricot Seeds

题目描述

Sam has some apricot seeds, and he wants to sort them in non-decreasing order based on size. He uses a unique method to sort the apricot seeds, described as follows:

Given nn apricot seeds, Sam performs a total of n1n-1 steps to sort them. For each step kk from 11 to n1n-1:

  • He compares the first seed with the second seed. If the second seed is smaller, he swaps their positions.
  • He then compares the second seed with the third seed. If the third seed is smaller, he swaps their positions.
  • He continues this process until he compares the (nk)(n-k)-th seed with the (nk+1)(n-k+ 1)-th seed and swaps their positions if the (nk+1)(n-k+ 1)-th seed is smaller.

Sam's friend Tom quickly realizes that this is the famous bubble sorting algorithm. To illustrate the inefficiency of this algorithm to Sam, Tom decides to ask Sam qq questions. A question is represented as a tuple [s,e,m,l,r][s,e,m,l,r].

For given a sequence of nn seeds, each question [s,e,m,l,r][s,e,m,l,r] asks for the sum of the sizes of seeds from position ll to rr of the (partially) sorted subsequence after applying the first mm steps of Sam's method to the subsequence of seeds from position 𝑠 to 𝑒 of the initial sequence.

For instance, consider four (n=4n=4) seeds with sizes of (1,3,4,2)(1,3,4,2) and two (q=2q = 2) questions [2,4,1,2,2][2,4,1,2,2] and [1,4,2,3,4][1,4,2,3,4]. For the first question, the subsequence of the sizes from the second (s=2s = 2) seed to the fourth (e=4e=4) seed is (3,4,2)(3,4,2). After applying one step (m=1m = 1) of Sam’s method, it becomes (3,2,4)(3,2,4). The sum of the sizes of seeds from the second position (l=2l = 2) to the second position (r=2r = 2) in this (partially) sorted subsequence is 22. For the second question, the subsequence is (1,3,4,2)(1,3,4,2). After applying two steps, it becomes (1,2,3,4)(1,2,3,4). The sum of the sizes of seeds from position 33 to 44 in this (partially) sorted sequence is 3+4=73 + 4 = 7.

Given a sequence of nn seeds and qq questions, write a program that computes the answer for each question.

输入格式

Your program is to read from standard input. The input starts with a line containing two integers, nn and qq (2n1,000,000,1q500,0002 \le n\le 1,000,000, 1 \le q \le 500,000), where nn represents the number of seeds and qq represents the number of questions. The second line contains nn integers, separated by spaces, representing the sizes of the apricot seeds in their initial order. Each size is between 11 and 10910^9, both inclusive. Each of the next qq lines contains five positive integers s,e,m,l,rs,e,m,l,r of query [s,e,m,l,r][s,e,m,l,r], separated by spaces, representing a question, where 1s<en,1mes1 \le s <e\le n, 1 \le m \le e-s, and 1lres+11 \le l\le r \le e-s+1.

输出格式

Your program is to write to standard output. For each of the qq questions, output one line with the answer. The answer for a question [s,e,m,l,r][s,e,m,l,r] is the sum of the sizes of seeds from position ll to rr of the partially sorted subsequence after applying the first mm steps of Sam's method to the subsequence of seeds from position ss to ee of the input sequence.

4 2
1 3 4 2
2 4 1 2 2
1 4 2 3 4
2
7
5 3
4 2 5 1 3
1 5 1 3 3
1 3 1 3 3
2 4 2 1 2
1
5
3
6 2
5 4 5 1 1 4
3 6 1 1 3
1 6 1 1 4
6
11