# Codeforces D. Powerful array（莫队）

## 题目描述：

Problem Description

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

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):

Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

## 思路：

1     for(int i = 1;i<=bulk;i++)
2     {
3         for(int j = (i-1)*size+1;j<=i*size;j++)
4         {
5             belong[j] = i;
6         }
7     }

（bulk是块数，size是每块的大小）

1 for(int i = 1;i<=n;i++)
2 scanf("%d",&col[i]),Be[i]=i/unit+1;

## 卡常技巧：

cmp函数改为莫队玄学奇偶性排序（代码中的cmp2），实际上可以帮你每个点平均优化200ms（可怕）

## 优化结果：

 1 #include <iostream>
2 #include <algorithm>
3 #include <cmath>
4 #define max_n 200005
5 using namespace std;
6 int a[max_n];
7 int cnt[1000005];
8 int belong[max_n*2];
9 int bulk;
10 int size;
11 long long ans = 0;
12 long long sum[max_n];
13 int n;
14 int m;
15 struct node
16 {
17     int r;
18     int l;
19     int id;
20 }q[max_n];
21 int cmp(node a,node b)
22 {
23     return (belong[a.l]==belong[b.l])?a.r<b.r:a.l<b.l;
24 }
25 int cmp2(node a,node b)
26 {
27     return (belong[a.l]^belong[b.l])?(a.l<b.l):(belong[a.l]&1)?a.r<b.r:a.r>b.r;
28 }
29
31 {
32     ans -= (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
33     /*if(cnt[a[pos]]==0)
34     {
35         ans++;
36     }*/
37     cnt[a[pos]]++;
38     ans += (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
39 }
40 void del(int pos)
41 {
42     ans -= (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
43     cnt[a[pos]]--;
44     /*if(cnt[a[pos]]==0)
45     {
46         ans--;
47     }*/
48     ans += (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
49 }
50 #pragma GCC optimize(2)
51 int main()
52 {
53     cin >> n >> m;
54     size = sqrt((double)n);
55     bulk = ceil((double)n/size);
56     for(int i = 1;i<=bulk;i++)
57     {
58         for(int j = (i-1)*size;j<=i*size;j++)
59         {
60             belong[j] = i;
61         }
62     }
63     for(int i = 1;i<=n;i++)
64     {
65         cin >> a[i];
66     }
67
68     for(int i = 1;i<=m;i++)
69     {
70         cin >> q[i].l >> q[i].r;
71         q[i].id = i;
72     }
73     sort(q+1,q+m+1,cmp2);
74     /*for(int i = 0;i<n;i++)
75     {
76         cout << belong[i] << " ";
77     }
78     cout << endl;*/
79     int l = 1;
80     int r = 0;
81     for(int i = 1;i<=m;i++)
82     {
83         int nl = q[i].l;
84         int nr = q[i].r;
87         while(l<nl) del(l++);
88         while(r>nr) del(r--);
89         //cout << q[i].id << endl;
90         sum[q[i].id] = ans;
91     }
92     for(int i = 1;i<=m;i++)
93     {
94         cout << sum[i] << endl;
95     }
96     return 0;
97 }

## 参考文章：

hzwer，「分块」数列分块入门1 – 9 by hzwer，http://hzwer.com/8053.html（分块算法）

WAMonster，莫队算法——从入门到黑题，https://www.cnblogs.com/WAMonster/p/10118934.html（莫队算法良心讲解）

（两篇结合互补食用效果为佳）

posted @ 2019-07-22 10:43 小张人 阅读(...) 评论(...) 编辑 收藏