問題一覧 > 通常問題

No.924 Kizuna Akari

レベル : / AI推定難易度 : 2050 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 55
作問者 : polylogK / テスター : lumc_
お気に入りにしたユーザー ProblemId : 3124 / 自分の提出
問題文最終更新日: 2019-11-08 20:00:09
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 230 (順位表) の他の問題:

If you are experiencing an issue where math formulas are not displayed, please take the following steps.

https://twitter.com/yukicoder/status/1191757446596812800?s=20

Problem Statement

You are given an integer sequence $A$ of length $N$. The $i$-th value of the sequence $A$ is $A_i$.
You are given $Q$ queries of the following form; process them.

  • In the $i$-th query, a pair of integers $L_i, R_i$ is given. Find the minimum value of the following function $f$.
    • $\displaystyle f(x) = \sum_{k = L_i}^{R_i} |x - A_k|$

[Note] In this problem, even a correct solution may get TLE in slow languages such as Python [Sorry]

Input

$N\ Q$
$A_1\ A_2\ \cdots\ A_{N - 1}\ A_N$
$L_1\ R_1$
$\vdots$
$L_Q\ R_Q$
  • $1 \le N, Q \le 2 \times 10^5$
  • $-10^9 \le A_i \le 10^9$
  • $1 \le L_i \le R_i \le N$
  • All input values are integers.

Output

Print $Q$ lines. On the $i$-th line, print the answer to query $i$ as an integer.

It can be proved that the minimum value is always an integer.

Sample

Sample 1
Input
5 5
1 2 3 4 5
1 5
2 4
1 2
3 5
2 4
Output
6
2
1
2
2

In query $1$, $f(x) = |x-1| + |x-2| + |x-3| + |x-4| + |x-5|$. The minimum value $f(x) = 6$ is achieved when $x = 3$.

Sample 2
Input
5 3
1 3 3 3 3
1 3
3 3
3 3
Output
2
0
0

Sample 3
Input
20 10
-574 627 212 956 -285 161 -331 -107 -229 -87 -414 -917 842 835 566 117 -298 -608 -964 -182
2 10
9 10
11 12
4 12
19 19
6 17
14 15
1 13
5 13
16 20
Output
2908
142
503
2870
0
4730
269
5461
2756
1507

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。