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. python基础-3 集合 三元运算 深浅拷贝 函数 Python作用域

    上节课总结 1 运算符 in 字符串 判断  : “hello” in "asdasfhelloasdfsadf" 列表元素判断:"li" in ['li', ...

  2. [19/05/07-星期二] JDBC(Java DataBase Connectivity)_CLOB(存储大量的文本数据)与BLOB(存储大量的二进制数据)

    一. CLOB(Character Large Object ) – 用于存储大量的文本数据 – 大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的.而非一般的字段,一次 ...

  3. python之callable

    callback是python的内置函数 英文说明: callable(object) Return True If the object argument appears callable,Fals ...

  4. 总结:String类型与Int类型的转换【实现插入操作主键自增】

    1.String类型(此类型是数字格式的字符串类型)转换成Int类型 String str = "10000"; 转换成Int类型: int num = Integer.parse ...

  5. java高级开发面试总结

    Java高级工程师面试题总结及参考答案 (转载)博客原文链接:https://www.cnblogs.com/java1024/p/8594784.html 一.面试题基础总结 1. JVM结构原理. ...

  6. 8、前端知识点--关于Set用法的详解【ES6】

    ES6提供了新的数据结构Set,它类似于数组,但是成员的值是唯一的,没有重复的值(对于基本类型来说).Set本身是一个构造函数,用来生成Set数据结构. 1.声明 let set = new Set( ...

  7. jQuery中$.get()和$.post()的异同点

    相同点:两者都是向服务器异步请求数据的. 不同点: 1.$.get() 方法使用GET方法来进行异步请求的,$.post() 方法使用POST方法来进行异步请求的. 2.如果前端使用$.get() 方 ...

  8. 你的package包名有问题!

    今天在Eclipse中运行我的Java程序中,就弹出了以下消息的窗口: 注意窗口的名字为Java Virtual Machine Launcher . Error : A JNI error has ...

  9. ios UIWebView加载HTMLStr图文,关于图片宽高设置,webView内容实际高度的踩坑问题

    一.关于UIWebView 与 WKWebView 选取问题 从发布时间看: 2008年7月11日,在新一代iPhone3G正式发售当天,iPhone OS 2.0(iOS 2.0)推出,这时候就有U ...

  10. Elasticsearch Java Low Level REST Client(嗅探器)

    https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...