Codeforces 86D Powerful array(莫队)

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 86D Powerful array(莫队)相关的知识,希望对你有一定的参考价值。

题目链接: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 lr (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(莫队)的主要内容,如果未能解决你的问题,请参考以下文章

Powerful array CodeForces - 86D (莫队算法)

CodeForces 86D Powerful array(莫队算法)

Powerful array CodeForces - 86D

Powerful array CodeForces - 86D(莫队)

CodeForces - 86D Powerful array (莫队)

86D - Powerful array