cf86d
题意:给定n个数,m个操作,每次查询[l,r]中每个数的出现次数,结果是次数×次数×这个数的值
莫队做咯,每次减去这个数原来个数个影响,再加上更改后的影响
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 220000
#define M 1100000
int l=0,r=0,n,m,p,a[N],num[M];
long long sum=0,ans[N];
struct node{int x,y,q,num;}q[N];
bool cmd(node x,node y){return x.q<y.q || (x.q==y.q && x.y<y.y);}
int main(){
scanf("%d%d",&n,&m);p=sqrt(n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=m;i++) scanf("%d%d",&q[i].x,&q[i].y),q[i].num=i,q[i].q=(q[i].x-1)/p;
sort(q+1,q+1+m,cmd);
for(int i=1;i<=m;i++){
int x=q[i].x,y=q[i].y;
while(x<l){
l--;sum-=(long long)num[a[l]]*num[a[l]]*a[l];
num[a[l]]++;sum+=(long long)num[a[l]]*num[a[l]]*a[l];
}while(l<x){
sum-=(long long)num[a[l]]*num[a[l]]*a[l];
num[a[l]]--;sum+=(long long)num[a[l]]*num[a[l]]*a[l];l++;
}while(r<y){
r++;sum-=(long long)num[a[r]]*num[a[r]]*a[r];
num[a[r]]++;sum+=(long long)num[a[r]]*num[a[r]]*a[r];
}while(y<r){
sum-=(long long)num[a[r]]*num[a[r]]*a[r];
num[a[r]]--;sum+=(long long)num[a[r]]*num[a[r]]*a[r];r--;
}ans[q[i].num]=sum;
}
for(int i=1;i<=m;i++) printf("%I64d\n",ans[i]);
return 0;
}