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. Python创建Cocos2d-x 2.2方法

    把创建项目做成一个批处理,当创建项目时可以省时省力很多. 操作步骤 1.在 E:\cocos2d-x-2.2.1\tools\project-creator 目录下创建 create_project. ...

  2. Spring 中注入 properties 中的值

    <bean id="ckcPlaceholderProperties" class="org.springframework.beans.factory.confi ...

  3. php报错日志:PHP Deprecated:Automatically populating $HTTP_RAW_POST_DATA is deprecated

    前几天将线上php服务升级到5.6.x版本后,php-error.log报出错误:PHP Deprecated: Automatically populating $HTTP_RAW_POST_DAT ...

  4. poj 3352

    Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11215 Accepted: 5575 De ...

  5. redmine Windows装配

    原文:http://www.myexception.cn/windows/1219064.html redmine Windows安装 官方地址:http://www.redmine.org/ 所需环 ...

  6. 【转】【C#】判断两个文件是否相同

    使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否相同 该哈希算法为一个文件生成一个小的二进制“指纹”,从统计学 ...

  7. android Camera 中如何修改缩放变焦参数

    如何修改 zoomRatio   修改过程:   1, 先找到 gZoomRatio 数组序列的值   Location: V:\project_code\project_name\ALPS.JB.M ...

  8. SpringMVC中的@PathVariable

    @PathVariable是用来动态获得url中的参数的,代码示例如下: 可以在代码中获得lev_1.lev_2和target参数的值看一下 // 支持跳转到WEB-INF/目录下二层目录 @Requ ...

  9. Web API路由

    前言 本文描述了 ASP.NET Web API 如何将 HTTP 请求路由到控制器. 如果你熟悉Asp.Net MVC,Web API的路由与Asp.Net MVC的路由是非常类似的.这主要的区别就 ...

  10. memcached协议

    memcached协议 旧版:http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt 新版:https://githu ...