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 productsKs·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.
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 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).
3 2 1 2 1 1 2 1 3
3 6
8 3 1 1 2 2 1 3 1 1 2 7 1 6 2 7
20 20 20
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
给出了一个固定的数组,t个区间,询问这个区间 每一个值其个数^2*该值 的和。
莫队再练习一道。然后一开始一直TLE。update函数这样写。
void update(int x, int d)
{
ans -= num[val[x]] * num[val[x]] * val[x];
num[val[x]] += d;
ans += num[val[x]] * num[val[x]] * val[x];
}
后来发现可以化简,原来x^2变(x+1)^2直接 加上2*x+1就好了。同理减法也是。
void update(int x, int d)
{
ll s = val[x];
if (d == 1)
{
ans += ((num[s] << 1) + 1) * s;
num[s] ++;
}
else
{
num[s]--;
ans -= ((num[s] << 1) + 1) * s;
}
}
3秒多水过。。。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x3fffffff
const int mod = 1e9 + 7;
const int maxn = 3000005;
int n, t, bk;
ll val[maxn], pos[maxn];
ll num[maxn], res[maxn], ans;
struct no
{
int le;
int ri;
int id;
}qu[maxn];
bool cmp(no a, no b)
{
if (pos[a.le] == pos[b.le])
{
return a.ri < b.ri;
}
else
{
return pos[a.le] < pos[b.le];
}
}
void input()
{
int i, j;
scanf("%d%d", &n, &t);
memset(num, 0, sizeof(num));
bk = ceil(sqrt(1.0*n));
for (i = 1; i <= n; i++)
{
scanf("%I64d", &val[i]);
pos[i] = (i - 1) / bk;
}
for (i = 0; i < t; i++)
{
scanf("%d%d", &qu[i].le, &qu[i].ri);
qu[i].id = i;
}
}
void update(int x, int d)
{
ll s = val[x];
if (d == 1)
{
ans += ((num[s] << 1) + 1) * s;
num[s] ++;
}
else
{
num[s]--;
ans -= ((num[s] << 1) + 1) * s;
}
}
void solve()
{
int i, j, pl, pr, id;
sort(qu, qu + t, cmp);
pl = 1; pr = 0;
ans = 0;
for (i = 0; i < t; i++)
{
id = qu[i].id;
if (qu[i].le == qu[i].ri)
{
res[id] = val[qu[i].le];
continue;
}
if (pr < qu[i].ri)
{
for (j = pr + 1; j <= qu[i].ri; j++)
{
update(j, 1);
}
}
else
{
for (j = pr ; j > qu[i].ri; j--)
{
update(j, -1);
}
}
pr = qu[i].ri;
if (pl < qu[i].le)
{
for (j = pl; j < qu[i].le; j++)
{
update(j, -1);
}
}
else
{
for (j = pl - 1; j >= qu[i].le; j--)
{
update(j, 1);
}
}
pl = qu[i].le;
res[id] = ans;
}
for (i = 0; i < t; i++)
{
printf("%I64d\n", res[i]);
}
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
input();
solve();
return 0;
}