D. Powerful array
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.
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).
Sample test(s)
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):
-------------------------------------code------------------------------------------------------------------------------
#include<iostream> using namespace std; typedef long long int lli; #include<bits/stdc++.h> lli arr[200005]; long long int result[2000005]; struct node { lli x,y,index; } qry[1000000]; int has[1000005]; long long int ans=0; lli bck_sz=441; lli read_int() { char r; bool start=false,neg=false; lli ret=0; while(true){ r=getchar(); if((r-'0'<0 || r-'0'>9) && r!='-' && !start){ continue; } if((r-'0'<0 || r-'0'>9) && r!='-' && start){ break; } if(start)ret*=10; start=true; if(r=='-')neg=true; else ret+=r-'0'; } if(!neg) return ret; else return -ret; } bool compare(node a,node b) { lli i,j; i=a.x/bck_sz; j=b.x/bck_sz; if(i==j) { if(a.y<b.y) return 1; else return 0; } else{ if(i<j) return 1; else return 0; } } lli join(lli num) { // cout<<" adding "<<num<<endl; ans-=num*has[num]*has[num]; has[num]++; ans+=num*has[num]*has[num]; // cout<<" after adding ans is "<<ans<<endl; } lli del(lli num) { ans-=num*has[num]*has[num]; has[num]--; ans+=num*has[num]*has[num]; } int main() { lli n,q; // cin>>n>>q; scanf("%lld%lld",&n,&q); for(lli i=0;i<n;i++) { scanf("%lld",&arr[i]); } for(lli i=0;i<q;i++) { lli l,r; scanf("%lld%lld",&l,&r); // l=read_int(); //r=read_int(); l--; r--; qry[i].x=l; qry[i].y=r; qry[i].index=i; } sort(qry,qry+q,compare); lli r=-1; lli l=0; for(lli i=0;i<q;i++) { lli x=qry[i].x; lli y=qry[i].y; lli index=qry[i].index; // cout<<" query in the range "<<x<<" "<<y<<endl; while(r<y) { r++; join(arr[r]); } while(l>x) { l--; join(arr[l]); } while(l<x) { del(arr[l]); l++; } while(r>y) { del(arr[r]); r--; } result[index]=ans; } for(lli i=0;i<q;i++) printf("%lld\n",result[i]); }
No comments:
Post a Comment