Logo
(追記) (追記ここまで)

30852번 - Apricot Seeds 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 2048 MB195372523.585%

문제

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 $n$ apricot seeds, Sam performs a total of $n - 1$ steps to sort them. For each step $k$ from 1ドル$ to $n - 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 $(n - k)$-th seed with the $(n - k + 1)$-th seed and swaps their positions if the $(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 $q$ questions. A question is represented as a tuple $[s, e, m, l, r]$. For given a sequence of $n$ seeds, each question $[s, e, m, l, r]$ asks for the sum of the sizes of seeds from position $l$ to $r$ of the (partially) sorted subsequence after applying the first $m$ steps of Sam's method to the subsequence of seeds from position $s$ to $e$ of the initial sequence.

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

Given a sequence of $n$ seeds and $q$ 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, $n$ and $q$ (2ドル ≤ n ≤ 1,000円,000円,ドル 1ドル ≤ q ≤ 500,000円$), where $n$ represents the number of seeds and $q$ represents the number of questions. The second line contains $n$ integers, separated by spaces, representing the sizes of the apricot seeds in their initial order. Each size is between 1ドル$ and 10ドル^9,ドル both inclusive. Each of the next $q$ lines contains five positive integers $s,ドル $e,ドル $m,ドル $l,ドル $r$ of query $[s, e, m, l, r],ドル separated by spaces, representing a question, where 1ドル ≤ s < e ≤ n,ドル 1ドル ≤ m ≤ e - s,ドル and 1ドル ≤ l ≤ r ≤ e - s + 1$.

출력

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

제한

예제 입력 1

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

예제 출력 1

2
7

예제 입력 2

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

예제 출력 2

1
5
3

예제 입력 3

6 2
5 4 5 1 1 4
3 6 1 1 3
1 6 1 1 4

예제 출력 3

6
11

힌트

출처

ICPC > Regionals > Asia Pacific > Korea > Asia Regional - Seoul 2023 A번

Camp > Petrozavodsk Programming Camp > Winter 2024 > Day 6: K-ontest J번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /