题目链接:

D. Powerful array

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 productsKs·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 lr (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.

AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,t;
long long a[N],num[N],ans[N];
struct node
{
/* friend bool operator< ()
{ }*/
int l,r,id,pos;
};
node qu[N];
int cmp(node x,node y)
{
if(x.pos==y.pos)return x.r<y.r;
return x.l<y.l;
}
void solve()
{
long long temp=;
int le=,ri=;
for(int i=;i<=t;i++)
{
while(ri<qu[i].r)
{
ri++;
temp+=((num[a[ri]]<<)+)*a[ri];
num[a[ri]]++;
}
while(ri>qu[i].r)
{
num[a[ri]]--;
temp-=((num[a[ri]]<<)+)*a[ri];
ri--;
}
while(le<qu[i].l)
{
num[a[le]]--;
temp-=((num[a[le]]<<)+)*a[le];
le++;
}
while(le>qu[i].l)
{
le--;
temp+=((num[a[le]]<<)+)*a[le];
num[a[le]]++;
}
ans[qu[i].id]=temp;
}
} int main()
{
scanf("%d%d",&n,&t);
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
int sq=sqrt(n);
for(int i=;i<=t;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
qu[i].pos=qu[i].l/sq;
}
sort(qu+,qu+t+,cmp);
solve();
for(int i=;i<=t;i++)
{
printf("%I64d\n",ans[i]);
}
return ;
}

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

  1. CodeForces - 86D D. Powerful array —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...

  2. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

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

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

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

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

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

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

  6. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  7. codeforces 86D D. Powerful array

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

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

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

  9. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. python实现身份证识别

    github: 人脸联合语音身份认证:https://github.com/tsstss123/faceUnionVoiceRecognition 身份证识别简易版:https://github.co ...

  2. WebApp 开发中常用的代码片段

    其实这里面的多数都是 iOS 上面的代码.其他平台的就没有去验证了. HTML, 从HTML文档的开始到结束排列: <meta name=”viewport” content=”width=de ...

  3. 使用onepage-scroll全屏滚动插件时的注意事项

    如果项目需要在移动端访问时需要设置responsiveFallback属性,并且在此之前还需要检测浏览器的级别(引入modernizr.js文件) var $responsiveFallback = ...

  4. c/c++的一些小知识点3

    ---恢复内容开始--- ---恢复内容结束---

  5. 【BZOJ3309】DZY Loves Math 莫比乌斯反演+线性筛(好题)

    [BZOJ3309]DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10 ...

  6. CMD命令获取电脑所有连接过的WiFi密码

    cmd中输入命令:for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo ...

  7. IntelliJ IDEA(2017)安装和破解(转发)

    IntelliJ IDEA(2017)安装和破解 IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手 ...

  8. Django——自定义分页(可调用)

    1.view from django.shortcuts import render,HttpResponse # Create your views here. from app01.models ...

  9. Linux基础系列:常用命令(6)_nfs服务与nginx服务

    NFS介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统 ...

  10. 牛客小白月赛1 B 简单题2 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/85/B 思路 这个 也是 E AC代码 #include <cstdio> #include < ...