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.

 
 
 
正解:莫队算法
解题报告:
  莫队裸题,很好处理,感觉没什么好说的。
  这别codeforces坑死了,只能用cout。。。严重拖慢我的速度。
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int size = ;
int n,m,a[MAXN];
int cnt[size];
int kuai;
int nowl,nowr;
LL ans;
LL out[MAXN]; struct wen{
int l,r,id,belong;
}q[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(wen p,wen pp){ if(pp.belong==p.belong) return p.r<pp.r; return p.belong<pp.belong; } inline bool ccmp(wen p,wen pp){ return p.id<pp.id; } inline void add(int x,int type){
if(type==) {
ans+=(LL)(cnt[a[x]]*+)*a[x];
cnt[a[x]]++;
}
else{
cnt[a[x]]--;
ans-=(LL)(cnt[a[x]]*+)*a[x];
}
} inline void work(){
n=getint(); m=getint(); kuai=sqrt(n);
for(int i=;i<=n;i++) a[i]=getint();
for(int i=;i<=m;i++) q[i].l=getint(),q[i].r=getint(),q[i].id=i,q[i].belong=(q[i].l-)/kuai+;
sort(q+,q+m+,cmp);
nowl=q[].l; nowr=q[].r;
for(int i=q[].l;i<=q[].r;i++) {
//ans-=(LL)cnt[a[i]]*cnt[a[i]]*a[i];
ans+=(LL)(cnt[a[i]]*+)*a[i];//简化
cnt[a[i]]++;
//ans+=(LL)cnt[a[i]]*cnt[a[i]]*a[i];
}
out[q[].id]=ans;
//注意nowl、nowr是当前位置,处理完毕的状态
for(int i=;i<=m;i++) {
while(nowl<q[i].l) add(nowl,-),nowl++;
while(nowl>q[i].l) add(nowl-,),nowl--;//nowl已经插入,需要插入nowl-1
while(nowr<q[i].r) add(nowr+,),nowr++;
while(nowr>q[i].r) add(nowr,-),nowr--;
out[q[i].id]=ans;
}
for(int i=;i<=m;i++) cout<<out[i]<<endl;
} int main()
{
work();
return ;
}

codeforces 86D : Powerful array的更多相关文章

  1. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  3. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

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

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  5. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  6. Codeforces 86D Powerful array(莫队算法)

    和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  7. CodeForces - 86D Powerful array (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  8. Codeforces D. Powerful array(莫队)

    题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...

  9. CF 86D Powerful array 【分块算法,n*sqrt(n)】

    给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数. t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i]) 离线+分块 将n个数分成sqrt(n ...

随机推荐

  1. 为Unity项目生成文档(一)

    VS生成chm帮助文档 VS代码中使用Xml注释,并通过Sandcastle生成chm文档的文章,这几篇值得分享: 使用.NET中的XML注释(一) -- XML注释标签讲解 使用.NET中的XML注 ...

  2. Unity Sample Bootcamp

    M4枪 射击特效 Gun.js源码 function GenerateGraphicStuff(hit : RaycastHit) { var hitType : HitType; var body ...

  3. java 13-2 Arrays工具类

    1.Arrays:针对数组进行操作的工具类.比如说排序和查找. 1:public static String toString(int[] a) 把数组转成字符串  2:public static v ...

  4. c++ web服务器

    https://github.com/facebook/proxygen http://tengine.taobao.org/ http://code.google.com/p/mongoose/ht ...

  5. C/C++ 常用工具集

    1. c++filt  //注意:就是这个名字 "c++file". 能把c++的函数签名转换成代码形参格式: 如:# c++filt _ZNSt4priv17_Rb_tree_i ...

  6. 颗粒翻页(css3效果展示)

    用css3效果做了一个颗粒翻页效果,布局上,一张图片做底层,在这张图片上用js创建一层小的行和列各为r和c的小span,给这些span分别设置background-position:用来覆盖原来的一张 ...

  7. beanFactoory介绍

  8. 16SpringMvc_在业务控制方法中写入User,Admin多个模型收集参数——引出问题

    上面文章时普通的业务那个方法中收集一个实体类,这篇文章想收集两个实体类. 文本要做的是:在person.jsp页面上,有两个表单.分别是普通用户和管理员用户的表单(普通用户的表单和管理员用户的表单里面 ...

  9. 《JAVA与模式》之适配器模式(转)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...

  10. DWZ (JUI) 教程 navTab 刷新分析

    navTab的刷新在doc文件里也有说明 首先 在form表单里指定好回调函数 * <form action="/user.do?method=save" onsubmit= ...