# Codeforces Yandex.Algorithm 2011 Round 2 D

D. Powerful array
time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
An array of positive integers a1, a2, …, an is given. Let us consider its arbitrary subarray al, al + 1…, ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·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 ai (1 ≤ ai ≤ 106) — 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).

Examples
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 321$3^2·1$ + 222$2^2·2$ + 123$1^2·3$ = 20.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXM = 1111111;
const int P = 555;
int cl,cr;//头 尾 当前包括区间
int first[MAXM],nxt[MAXM],cc[MAXM];//链表 出现次数
int n,m,num[MAXM],l[MAXM],r[MAXM];//离线大法好
long long ans = 0,anss[MAXM];
int cnt = 0;//块数
//据说莫队

int main()
{
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(num,0,sizeof(num));
memset(cc,0,sizeof(cc));
scanf("%d %d",&n,&m);
for(int i = 0; i < n; i ++) scanf("%d",&num[i]);
//  cnt = n / P + 1;
cnt = (n + P - 1) / P;
for(int i = 1; i <= m; i ++)
{
scanf("%d %d",&l[i],&r[i]);
l[i] --,r[i] --;
int j = l[i] / P * cnt + r[i] / P;
nxt[i] = first[j];  first[j] = i;
}

cl = 0,cr = n - 1;
for(int i = 0; i < n; i ++)
{
ans += (long long)(num[i] * (2 * cc[num[i]] + 1));//注意这个2
//  ans += num[i] * (2 * cc[num[i]] + 1);
cc[num[i]] ++;
}

//一定注意这个奇怪的运算不然会TTTTTTTLE
for(int i = 0; i < cnt * cnt; i ++)
for(int j = first[i]; j > 0; j = nxt[j])
{
while(cl > l[j])//qu jian nei
{
cl --;
ans += (long long)(num[cl] * (2 * cc[num[cl]] + 1));
cc[num[cl]] ++;
}
while(cl < l[j])//ci shu bu gou
{
cc[num[cl]] --;
ans -= (long long)(num[cl] * (2 * cc[num[cl]] + 1));
cl ++;
}
while(cr < r[j])//qu jian nei
{
cr ++;
ans += (long long)(num[cr] * (2 * cc[num[cr]] + 1));
cc[num[cr]] ++;
}
while(cr > r[j])//ci shu bu gou
{
cc[num[cr]] --;
ans -= (long long)(num[cr] * (2 * cc[num[cr]] + 1));
cr --;
}
anss[j] = ans;
}

for(int i = 1; i <= m; i ++)    printf("%I64d\n",anss[i]);
return 0;
}