D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

时间:2023-03-10 07:07:53
D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;
D. Powerful array
time limit per test
seconds
memory limit per test
megabytes
input
standard input
output
standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + ..., ar, where  ≤ 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 ( ≤ n, t ≤ ) — the array length and the number of queries correspondingly. Second line contains n positive integers ai ( ≤ ai ≤ ) — the elements of the array. Next t lines contain two positive integers l, r ( ≤ 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 -bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
Examples
Input Output Input Output Note Consider the following array (see the second sample) and its [, ] subarray (elements of the subarray are colored):
Then K1 = , K2 = , K3 = , so the power is equal to · + · + · = . /**
题目:D. Powerful array
链接:http://codeforces.com/problemset/problem/86/D
题意:给定n个数,m次查询;每次查询[l,r]的权值;
权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x;
所有贡献和即为该区间的值;
思路:由于静态区间,所以离线+莫队算法; 然后(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x;
来优化计算;常规暴力计算超时; */
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps=1e-;
const int maxn = 2e5+;
const ll mod = 1e9+;
ll num[], c[maxn], pos[maxn];
ll ans;
int n , m;
struct node
{
ll l, r;
ll ans;
int id; }t[maxn];
int cmp_id(node a,node b)
{
return a.id<b.id;
}
int cmp(node a,node b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void update(int place,int add)
{
ll v = c[place];
if(add==){
ans += v*(*num[v]+);
num[v]++;
}else
{
num[v]--;
ans -= v*(*num[v]+);
}
}
void solve()
{
memset(num, , sizeof num);
ans = ;
for(int i = t[].l; i <= t[].r; i++){
update(i,);
}
t[].ans = ans;
for(int i = ; i <= m; i++){
for(int j = t[i-].l; j < t[i].l; j++) update(j,-);//减得时候,自身开始;
for(int j = t[i-].l; j > t[i].l; j--) update(j-,);//增的时候,不包括自身;
for(int j = t[i-].r; j < t[i].r; j++) update(j+,);
for(int j = t[i-].r; j > t[i].r; j--) update(j,-);
t[i].ans = ans;
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
for(int i = ; i <= n; i++) scanf("%I64d",&c[i]); for(int i = ; i <= m; i++){
scanf("%I64d%I64d",&t[i].l,&t[i].r);
t[i].id = i;
} int N = int(sqrt(n));
for(int i = ; i <= n; i++){
pos[i] = i/N+;
} sort(t+,t++m,cmp);
solve();
sort(t+,t++m,cmp_id);
for(int i = ; i <= m; i++){
printf("%I64d\n",t[i].ans);
}
}
return ;
}