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

题意:

  给你一个序列an 。t次询问,问在[L, R] 的区间里面的值是多少。公式是:ai的个数的平方 * ai 的求和。具体看Note。

题解:

  这一题用莫队算法。如果做过小Z的袜子的话,这一题很简单,小Z的袜子是 ai个数的平方求和,这里就乘多了一个ai。

  巨坑:num[] 数组要用int 开,用long long开的话会被卡TLE。(很绝望,被卡了10次罚时,最后用输入挂才过的)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +; template<class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(), c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c = getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(), c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
inline void out(LL x)
{
if(x>) out(x/);
putchar(x%+'');
} int a[maxn];
int unit, n, t;
LL ans[maxn];
int num[maxn]; struct node
{
int l, r, id;
}que[maxn];
void init() {
ms(num, );
}
bool cmp(node x1, node x2)
{
if(x1.l/unit != x2.l/unit){
return x1.l/unit < x2.l/unit;
}
else{
return x1.r < x2.r;
}
}
void work()
{
sort(que, que+t, cmp);
LL temp = ;
int l, r;
l = , r = ;
for(int i =;i<t;i++){
while(r<que[i].r){
r++;
temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
num[a[r]]++;
temp += 1LL*num[a[r]]*num[a[r]]*a[r];
}
while(r>que[i].r){
temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
num[a[r]]--;
temp += 1LL*num[a[r]]*num[a[r]]*a[r];
r--;
}
while(l>que[i].l){
l--;
temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
num[a[l]]++;
temp += 1LL*num[a[l]]*num[a[l]]*a[l];
}
while(l<que[i].l){
temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
num[a[l]]--;
temp += 1LL*num[a[l]]*num[a[l]]*a[l];
l++;
}
ans[que[i].id] = temp;
}
}
void solve() {
// scanf("%d%d", &n, &t);
scan_d(n);scan_d(t);
for(int i = ;i<=n;i++)
scan_d(a[i]);
unit = (int)sqrt(n);
for(int i = ;i<t;i++){
que[i].id = i;
// scanf("%d%d", &que[i].l, &que[i].r);
scan_d(que[i].l);
scan_d(que[i].r);
}
work();
for(int i = ;i<t;i++){
// cout << ans[i] << endl;
// printf("%I64d\n", ans[i]);
out(ans[i]);
puts("");
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// ios::sync_with_stdio(0);
// cin.tie(0);
init();
solve();
return ;
}

Codeforces 86D Powerful array (莫队)的更多相关文章

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

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

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

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

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

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

  4. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

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

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

  6. codeforces 86D,Powerful array 莫队

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

  7. codeforces 86D D. Powerful array(莫队算法)

    题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  8. CodeForces 86 D Powerful array 莫队

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

  9. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

随机推荐

  1. Ecshop二次开发必备基础

    EcShop二次开发学习方法 近年来,随着互联网的发展,电子商务也跟着一起成长,B2B,C2C,B2C的电子商务模式也不断的成熟.这时催生出了众多电子商务相关的PHP开源产品.B2C方面有Ecshop ...

  2. idea 2017 快捷键

    Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关闭文件 C ...

  3. string类find_first_not_of ()方法

    string类find_first_not_of ()方法 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xfqxj.blog. ...

  4. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  5. locale报错,显示中文乱码

    locale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAG ...

  6. 【sql server复制】不重新初始化快照的情况下新增表/存储过程/函数等

    转发自:https://www.cnblogs.com/datazhang/p/5498789.html sqlserver同步后在不重新初始化快照的情况下新增表        在已有事务复制中,时长 ...

  7. Git-第N篇碰见的一些问题

    1.关于windows平台自动换行问题 warning: LF will be replaced by CRLF in readme.txt. The file will have its origi ...

  8. HDFS数据流——读数据流程

    HDFS读数据流程 假设客户端请求下载文件/user/atguigu/ss.avi,HDFS读数据流程如下: 1)客户端向namenode请求下载文件,namenode通过查询元数据,找到文件所有文件 ...

  9. java_第一年_JDBC(5)

    事务概念:事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功: 开始事务:start transaction 提交事务:commit 回滚事务:rollback 事务的四大特 ...

  10. Python时间模块datetime用法

    时间模块datetime是python内置模块,datetime是Python处理日期和时间的标准库. 1,导入时间模块 from datetime import datetime 2,实例 from ...