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的数组a

要求:询问区间1<=L<=R<=N中,每个数字出现次数的平方与当前数字的乘积和

思路:

用一个数组flag[i] 表示 数字i在当前区间出现的次数。

然后正常的莫队add,del转移即可。。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
pll ans[maxn];
ll Ans = 0ll;
int l = 0;
int r = 0;
struct node {
int l, r, id;
} a[maxn];
int pos[maxn];
int n, m;
int len;
bool cmp(node aa, node bb)
{
if (pos[aa.l] == pos[bb.l]) {
return aa.r < bb.r;
} else {
return pos[aa.l] < pos[bb.l];
}
}
int col[maxn];
int flag[maxn];
void add(int x)
{
Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];
flag[col[x]]++;
Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
void del(int x)
{
Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];
flag[col[x]]--;
Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gg(n);
gg(m);
len = (int)(sqrt(n));
repd(i, 1, n) {
gg(col[i]);
}
repd(i, 1, m) {
gg(a[i].l);
gg(a[i].r);
a[i].id = i;
pos[i] = i / len;
}
sort(a + 1, a + 1 + m, cmp);
repd(i, 1, m) {
while (l > a[i].l) {
l--;
add(l);
}
while (r < a[i].r) {
r++;
add(r);
}
while (l < a[i].l) {
del(l);
l++;
}
while (r > a[i].r) {
del(r);
r--;
}
ans[a[i].id].fi = Ans;
}
repd(i, 1, m) {
printf("%lld\n", ans[i].fi);
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Powerful array CodeForces - 86D (莫队算法)的更多相关文章

  1. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

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

    给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和.1<=n,m<=200000.1<=s<=10^6 #include <iostr ...

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

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

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

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

  5. D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

    D. Powerful array time limit per test seconds memory limit per test megabytes input standard input o ...

  6. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

  7. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

    E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...

  8. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

    任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  9. 莫队算法初识~~CodeForces - 617E

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. 使用Navicat为Oracle新增用户

    步骤请参考帖子https://www.cnblogs.com/franson-2016/p/5925593.html 需要注意的是新增用户时不能使用小写,否则不能登录,之前新增一个小写的用户名,授予c ...

  2. linux之用户,用户组,软件操作

    用户 超级管理员用户 root 0 普通用户 (0-65535) 系统用户:启动系统服务和进程的用户,不可以登陆. (1-999centos7)(1-499centos6) 可登陆用户:能登录系统的用 ...

  3. 串口使用和CSerial类

    1 串口通信的基本原理 串口通信中无论是写入串口还是读取串口,都是对缓冲区操作的.可以理解为写串口就是向输出缓冲区写入内容,读取串口就是从输入串口缓冲区读取内容.但是何时打开串口,何时发送数据,何时接 ...

  4. C# 创建桌面快捷方式 用法

    项目--->添加引用   找到 头部 using IWshRuntimeLibrary; 核心代码 string desktoppath = System.Environment.GetFold ...

  5. Django 数据库操作

    Django 数据库操作 Django运算表达式与Q对象/F对象 1 模型查询 概述: 1 查询集:表示从数据库中获取的对象的集合 2 查询集可以有多个过滤器,通过 逻辑运算符连接 3 过滤器就是一个 ...

  6. python 实现 灰色预测 GM(1,1)模型 灰色系统 预测 灰色预测公式推导

    来源公式推导连接 https://blog.csdn.net/qq_36387683/article/details/88554434 关键词:灰色预测 python 实现 灰色预测 GM(1,1)模 ...

  7. C++学习笔记-继承

    类之间可以建立联系,这就使得类可以有某种关系 类之间的关系 has-A:包含关系,一个类使用另一个已经定义好的类的数据 uses-A:使用关系,友元或者对象参数传递 is-A:是的关系,这就是继承,具 ...

  8. VS2017:"64位调试操作花费的时间比预期要长",无法运行调试解决办法

    关于这个问题网上搜了好久,参考http://www.yishimei123.com/network/685.html这篇文章,最后终于解决了,在此表示非常感谢! 我的环境是:win10+VS2017 ...

  9. java.io.IOException 断开的管道 解决方法 ClientAbortException: java.io.IOException: Broken pipe

    今天公司技术支持的童鞋报告一个客户的服务不工作了,紧急求助,于是远程登陆上服务器排查问题. 查看采集数据的tomcat日志,习惯性的先翻到日志的最后去查看有没有异常的打印,果然发现了好几种异常信息,但 ...

  10. Elasticsearch-日期类型

    Elasticsearch-日期类型 date类型用于存储日期和时间.它是这样运作的:通常提供一个表示日期的字符串,例如2019-06-25T22:47.然后,ES解析这个字符串,然后将其作为long ...