1. 程式人生 > >Codeforces 86D Powerful array(莫隊)

Codeforces 86D Powerful array(莫隊)

element call bar side return 題目 length content into

題目鏈接:http://codeforces.com/problemset/problem/86/D

題目:

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 Copy
3 2
1 2 1
1 2
1 3
output
3
6
input Copy
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 3^2·1 + 2^2·2 + 1^2·3 = 20.

題意:給出一個由n個正整數形成的數組,t次詢問,每次詢問一個區間[l,r]內所有 K^2*a的和,K為數a在區間內出現的次數。

題解:更改下add和del即可。add的話(k+1)^2-k^2=2k+1,增加2k+1個a[i];del的話k^2-(k-1)^2=2k-1,減少2k-1個a[i]。

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 typedef long long LL;
 7 const int N=1e6+10;
 8 struct node{
 9     int l,r,id;
10 }Q[N];
11 
12 LL a[N],ans[N],cnt[N];
13 int BLOCK;
14 bool cmp(node x,node y){
15     if(x.l/BLOCK==y.l/BLOCK) return x.r<y.r;
16     return x.l/BLOCK<y.l/BLOCK;
17 }
18 
19 int n,m;
20 LL Ans=0;
21 
22 void add(int x){
23     Ans+=a[x]*(cnt[a[x]]*2+1);
24     cnt[a[x]]++;
25 }
26 
27 void del(int x){
28     Ans-=a[x]*(cnt[a[x]]*2-1);
29     cnt[a[x]]--;
30 }
31 
32 int main(){
33     scanf("%d%d",&n,&m);
34     BLOCK=sqrt(n);
35     for(int i=1;i<=n;i++){
36         scanf("%lld",&a[i]);
37     }
38     for(int i=1;i<=m;i++){
39         scanf("%d%d",&Q[i].l,&Q[i].r);
40         Q[i].id=i;
41     }
42     sort(Q+1,Q+1+m,cmp);
43     int L=1,R=0;
44     for(int i=1;i<=m;i++){
45         while(L<Q[i].l){
46             del(L);
47             L++;
48         }
49         while(L>Q[i].l){
50             L--;
51             add(L);
52         }
53         while(R<Q[i].r){
54             R++;
55             add(R);
56         }
57         while(R>Q[i].r){
58             del(R);
59             R--;
60         }
61         ans[Q[i].id]=Ans;
62     }
63     for(int i=1;i<=m;i++)
64     printf("%lld\n",ans[i]);
65     return 0;
66 }

Codeforces 86D Powerful array(莫隊)