题目链接:

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. ASP.NET动态网站制作(19)-- C#(2)

    前言:C#的第二次课,依旧讲解C#的基础知识. 内容: 1.GC:垃圾回收机制,可以回收托管模块中的垃圾. 2.值类型和引用类型:  (1)值类型:所有的数值类型都是值类型,如int,byte,sho ...

  2. element开源框架

    vue-element-admin:https://gitee.com/accest/bod-element   https://gitee.com/liuyuantao/vue-element-ad ...

  3. smarty模板 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面<{config_load file='fo.conf'}> 2,在需要用到配置的地方加<{#si ...

  4. 【BZOJ4894】天赋 有向图生成树计数

    [BZOJ4894]天赋 Description 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有一些天赋必须是要有前置天赋才能够学习得到的.也就是 ...

  5. 【HTML5开发系列】CSS3

    选择器 属性 背景和边框 盒模型 布局 文本 动画 其他

  6. Hibernate 表连接hql语句

    现有两个表 user 表 和 VIPcard 表 UserVo  user VIPcardVo 中含有 UserVo user select v from VIPCardVo v left join ...

  7. Zookeeper数据与存储

    一.前言 前面分析了Zookeeper对请求的处理,本篇博文接着分析Zookeeper中如何对底层数据进行存储,数据存储被分为内存数据存储于磁盘数据存储. 二.数据与存储 2.1 内存数据 Zooke ...

  8. Windows下oracle-win-64-11g安装步骤

    一. Oracle 下载 官方下地址 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.htm ...

  9. 九度OJ 1205:N阶楼梯上楼问题 (斐波那契数列)

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3739 解决:1470 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入: 输入包括一个整 ...

  10. [postfix]添加黑名单

    最近公司员工的邮箱总是收到twoomail.com的邀请邮件,邮箱服务器还没有添加过黑名单呢,就拿它开刀吧. 在主配置文件中添加如下配置 #vi /etc/postfix/main.cf #black ...