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 21 2 11 21 3

output

36

input

Copy

8 31 1 2 2 1 3 1 12 71 62 7

output

202020

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 }

原文地址:https://www.cnblogs.com/Leonard-/p/8460549.html

时间: 2024-07-30 23:10:05

Codeforces 86D Powerful array(莫队)的相关文章

CodeForces - 86D Powerful array (莫队)

题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[pos]*cnt[a[pos]]*cnt[a[pos]],将cnt[a[pos]]+1,再将乘积加回.其实根据数学原理,K^2和(K+1)^2差值是2K+1,那么其实每次更新时只要加上或减去a[pos]*(2*cnt[pos]+1)即可,这样更高效. #include<bits/stdc++.h>

codeforces 86D D. Powerful array(莫队算法)

题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, wh

D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array 典型的莫队算法题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include

CodeForces 86D Powerful array(莫队算法)

和BZOJ2038差不多..复习一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 int block; 6 struct Query{ 7 int i,l,r; 8 bool operator<(const Query &q)const{ 9 if(l/block==q.l/block) return r<q.r; 10 re

Codeforces#86D Powerful array(分块暴力)

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

【分块】Codeforces 86d Powerful array

通道 题意:询问[l,r]区间的权和,权定义为sum(k^2*a[i]),k表示a[i]出现的次数 思路:区间每增加一个a[i],增量是(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i] 分块排序即可,块内按r排序 代码: #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define N 200100 typedef

86D - Powerful array

莫队. 统计ai[i]的出现次数,每一次先还原贡献,再加上或减去当前的贡献即可. #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #i

(莫队算法)两题莫队算法统计数量的入门题

因为这两题差不多,而且比较简单,就放一起,做了这题,这种题目就是巨水的题了.随便写都行. CodeForces - 86D  Powerful array 题意: 多次查询数列中从L到R每个数字出现次数的平方乘这个数字的和. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <map> 6 #includ

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

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 th