Yandex.Algorithm 2011: Round 2
D
Powerful array
Codeforces
2200
原题
← CE →
翻译中…

An array of positive integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ is given. Let us consider its arbitrary subarray $*a*_{*l*}, *a*_{*l* + 1}..., *a*_{*r*}$, where $1 ≤ *l* ≤ *r* ≤ *n*$. For every positive integer $*s*$ denote by $*K*_{*s*}$ the number of occurrences of $*s*$ into the subarray. We call the power of the subarray the sum of products $*K*_{*s*}·*K*_{*s*}·*s*$ for every positive integer $*s*$. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.

翻译中…

You should calculate the power of $*t*$ given subarrays.

翻译中…

Input

翻译中…

First line contains two integers $*n*$ and $*t*$ ($1 ≤ *n*, *t* ≤ 200000$) — the array length and the number of queries correspondingly.

翻译中…

Second line contains $*n*$ positive integers $*a*_{*i*}$ ($1 ≤ *a*_{*i*} ≤ 10^{6}$) — the elements of the array.

翻译中…

Next $*t*$ lines contain two positive integers $*l*$, $*r*$ ($1 ≤ *l* ≤ *r* ≤ *n*$) each — the indices of the left and the right ends of the corresponding subarray.

翻译中…

Output

翻译中…

Output $*t*$ lines, the $*i*$-th line of the output should contain single positive integer — the power of the $*i*$-th query subarray.

翻译中…

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).

翻译中…

Note

翻译中…

Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

翻译中…

image

翻译中…

Then

翻译中…

K1 = 3

翻译中…

,

翻译中…

K2 = 2

翻译中…

,

翻译中…

K3 = 1

翻译中…

, so the power is equal to

翻译中…

32·1 + 22·2 + 12·3 = 20

翻译中…

.

翻译中…

样例

Input #1
3 2
1 2 1
1 2
1 3
Output #1
3
6
InputCopy OutputCopy
Input #2
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
Output #2
20
20
20
InputCopy OutputCopy
题目信息
题号
86D
时间限制
5 s
内存限制
256 MB
标签
数据结构
模拟
数学
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。