Problem 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.

题意:一个长度为 n 的数字序列,m 次询问,每次询问给出一个区间 [l,r],求这个区间内出现过的值与其次数平方的乘积的和

思路:普通莫队模版题,要求每个区间出现过的值与其出现次数平方的乘积的和,只需修改 O(1) 部分即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=10007;
const int N=1000000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;struct Node{int l,r;//询问的左右端点int id;//询问的编号
}q[N];
int n,m,a[N];
int block;//分块
LL ans,cnt[N*2];
LL res[N];bool cmp(Node a,Node b){//奇偶性排序return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
}void add(int x){ans-=cnt[a[x]]*cnt[a[x]]*a[x];//减去旧的cnt[a[x]]++;ans+=cnt[a[x]]*cnt[a[x]]*a[x];//加上新的
}
void del(int x){ans-=cnt[a[x]]*cnt[a[x]]*a[x];//减去旧的cnt[a[x]]--;ans+=cnt[a[x]]*cnt[a[x]]*a[x];//加上新的
}int main(){while(scanf("%d%d",&n,&m)!=EOF){memset(cnt,0,sizeof(cnt));ans=0;for(int i=1;i<=n;++i)scanf("%d",&a[i]);for(int i=1;i<=m;i++){scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;}block=sqrt(m*2/3*1.0);//分块,卡常数sort(q+1,q+m+1,cmp);//对询问进行排序int l=1,r=0;//左右指针for(int i=1;i<=m;i++){int ql=q[i].l,qr=q[i].r;//询问的左右端点while(l>ql) add(--l);while(r<qr) add(++r);while(l<ql) del(l++);while(r>qr) del(r--);res[q[i].id]=ans;//获取答案}for(int i=1;i<=m;i++)printf("%lld\n",res[i]);}return 0;
}

Powerful array(CF-86D)相关推荐

  1. Codeforces D. Powerful array(莫队)

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

  2. C++_STL——array(C++11)

    C++_STL--array(C++11) 1.类模板 template < class T, size_t N > class array; 1.1容器属性 容器属性 序列 序列容器中的 ...

  3. 【解题报告】随便练练二(CF 2300)

    [解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...

  4. 【打CF,学算法——四星级】CodeForces 86D Powerful array (莫队算法)

    [CF简介] 题目链接:CF 86D 题面: D. Powerful array time limit per test 5 seconds memory limit per test 256 meg ...

  5. LeetCode算法题-K-diff Pairs in an Array(Java实现)

    这是悦乐书的第254次更新,第267篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第121题(顺位题号是532).给定一个整数数组和一个整数k,您需要找到数组中唯一的k- ...

  6. Commentator problem(CF 2)

    题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...

  7. C. Three Parts of the Array(切割字符串)

    C. Three Parts of the Array You are given an array d1,d2,-,dnd1,d2,-,dn consisting of nn integer num ...

  8. CF831A-Unimodal Array(单峰阵列)

    A.单峰阵列 Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it ...

  9. 【leetcode】442. Find All Duplicates in an Array(Python C++)

    442. Find All Duplicates in an Array 题目链接 442.1 题目描述: Given an array of integers, 1 ≤ a[i] ≤ n (n = ...

最新文章

  1. 二维几何基础大合集!《计算几何全家桶(一)》(基础运算、点、线、多边形、圆、网格)
  2. 剑指offer:扑克牌顺子
  3. ORACLE 数据字典
  4. JsonMappingException:找不到类型[simple type,class]的合适构造函数:无法从JSON对象实例化
  5. Oracle在线重定义
  6. 一、自然语言处理概述
  7. Linux操作系统知识
  8. CocoaPods did not set the base configuration of your project 问题解决方案
  9. 深度学习平台的未来:谁会赢得下半场?
  10. 用FileOutputStream将内容写入到文本
  11. SIAM International Conference on Data Mining, SDM 会议怎么样?
  12. Hive原理及其使用(六)
  13. 2022-2028年中国环保减速机行业运行动态及投资机会分析报告
  14. yum 报错:Another app is currently holding the yum lock; waiting for it to exit......
  15. uni-app小程序实现图片上传和压缩
  16. FastQC评估测序数据的质量
  17. win7装sql2000找不到服务器,WIN7 64位系统 SQL2000服务无法启动
  18. hover出不来是什么原因css,关于css的:hover失效问题
  19. 在线模拟linux终端,linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解...
  20. Django实现websocket聊天室

热门文章

  1. NGenerics算法库是学习的好代码
  2. 国资委发文!10本书讲透数字化时代新机遇
  3. 粘包拆包,Netty及远洋通信中的解决方案!超实用
  4. docker安装xxl-job-admin步骤
  5. CSDN又力推一优秀开源项目jeecg,跨时代重构精华版发布
  6. phonegap免费视频
  7. Redis, Memcache 基本使用
  8. 好程序员分享居中一个float元素
  9. iTween 动画类型
  10. nagios-3种报警方式–声音–email/邮件—短信