CodeForces - 86D D. 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.
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 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).
3 2 1 2 1 1 2 1 3
3 6
8 3 1 1 2 2 1 3 1 1 2 7 1 6 2 7
20 20 20
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
题解:
现场做的题,结果出现了三个错误:
1.模板抄错了,原因是抄了一部分,另一部分类似,所以就复制,然后再修改,结果漏了一个地方没改,又硬是发现不了,结果卡了n久。所以以后还是老老实实地一个字符一个字符地打模板。
2.有个数组开小了,要常常注意n的范围和ai的范围。
3.把变量全部定为long long,结果超时了,而用int就过了。听说是long long的字节比较长,处理起来就相对慢了。所以以后在int范围内的变量就尽量用int定义了, 如果计算超出范围,加个:1LL*
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const int INF = 2e9; 16 const LL LNF = 9e18; 17 const int mod = 1e9+7; 18 const int maxn = 2e5+10; 19 20 int n,m; 21 struct Query 22 { 23 int L, R, id; 24 }node[maxn]; 25 26 LL ans[maxn], tmp; 27 28 int a[maxn], num[maxn*10], unit; //注意num[]下标的最大值为1e6 29 bool cmp(Query a, Query b) 30 { 31 if(a.L/unit != b.L/unit) return a.L/unit <b.L/unit; 32 else return a.R<b.R; 33 } 34 35 void add(int val) 36 { 37 tmp -= 1LL*num[val]*num[val]*val; 38 num[val]++; 39 tmp += 1LL*num[val]*num[val]*val; 40 } 41 42 void del(int val) 43 { 44 tmp -= 1LL*num[val]*num[val]*val; 45 num[val]--; 46 tmp += 1LL*num[val]*num[val]*val; 47 } 48 49 void work() 50 { 51 tmp = 0; 52 ms(num,0); 53 int L = 1; 54 int R = 0; 55 for(int i = 0; i<m; i++) 56 { 57 while(R<node[i].R) R++, add(a[R]); 58 while(R>node[i].R) del(a[R]), R--; 59 while(L<node[i].L) del(a[L]), L++; 60 while(L>node[i].L) L--, add(a[L]); 61 ans[node[i].id] = tmp; 62 } 63 } 64 65 int main() 66 { 67 scanf("%d%d",&n,&m); 68 for(int i = 1; i<=n; i++) 69 scanf("%d",&a[i]); 70 for(int i = 0; i<m; i++) 71 { 72 node[i].id = i; 73 scanf("%d%d",&node[i].L, &node[i].R); 74 } 75 unit = (int)sqrt(n); 76 sort(node,node+m,cmp); 77 work(); 78 for(int i = 0; i<m; i++) 79 printf("%I64d\n",ans[i]); 80 }