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

Sample test(s)
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

这题也是用莫队算法,类型和前面小Z的袜子基本一样。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; struct Query
{
int id, l, r;
long long ans;
}; const int MAXN = 200010;
const int MAXNUM = 1000010;
int n, m, sqrtn;
int c[MAXN], num[MAXNUM];
Query q[MAXN]; int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
} bool cmplr(const Query &a, const Query &b)
{
if (a.l / sqrtn == b.l / sqrtn) return a.r < b.r;
else return a.l < b.l;
} bool cmpid(const Query &a, const Query &b)
{
return a.id < b.id;
} int main()
{
scanf("%d%d", &n, &m);
sqrtn = (int)sqrt(n);
memset(num, 0, sizeof(num));
for (int i = 1; i <= n; i++)
scanf("%d", &c[i]);
for (int i = 0; i < m; i++)
{
q[i].id = i;
scanf("%d%d", &q[i].l, &q[i].r);
}
sort(q, q + m, cmplr);
int l = 1, r = 1;
long long ans = c[1];
num[c[1]]++;
for (int i = 0; i < m; i++)
{
while (r < q[i].r)
{
r++;
ans -= (long long)num[c[r]] * num[c[r]] * c[r];
num[c[r]]++;
ans += (long long)num[c[r]] * num[c[r]] * c[r];
}
while (l < q[i].l)
{
ans -= (long long)num[c[l]] * num[c[l]] * c[l];
num[c[l]]--;
ans += (long long)num[c[l]] * num[c[l]] * c[l];
l++;
}
while (l > q[i].l)
{
l--;
ans -= (long long)num[c[l]] * num[c[l]] * c[l];
num[c[l]]++;
ans += (long long)num[c[l]] * num[c[l]] * c[l];
}
while (r > q[i].r)
{
ans -= (long long)num[c[r]] * num[c[r]] * c[r];
num[c[r]]--;
ans += (long long)num[c[r]] * num[c[r]] * c[r];
r--;
}
q[i].ans = ans;
}
sort(q, q + m, cmpid);
for (int i = 0; i < m; i++)
cout << q[i].ans << "\n";
return 0;
}

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 D. Powerful array(莫队算法)

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

  3. codeforces 86D,Powerful array 莫队

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

  4. CodeForces 86 D Powerful array 莫队

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

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

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

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

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

  7. Codeforces 86D Powerful array (莫队)

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

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

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

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

随机推荐

  1. 比较Power BI和Tableau,好比用奔驰对比奥迪

    经常会有人问Power BI和Tableau的区别,好吧,为了非IT专业的能看懂,咱们就用车,奔驰和奥迪来对比一下.因为他们确实有太多相似之处. 所以Power BI VS Tableau,就相当于国 ...

  2. Openstack neutron 网络服务 (七)

    引用: https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/common/get-started-networking.html neut ...

  3. 爬虫系列 | 6、详解爬虫中BeautifulSoup4的用法

    bs4,全称BeautifulSoup 4 , 它是Python独有的一种解析方式.也就是说只有Python语言才可以通过这种方式去解析数据. BeautifulSoup 3 只支持Python2,所 ...

  4. 【老孟Flutter】如何提高Flutter应用程序的性能

    首先 Flutter 是一个非常高性能的框架,因此大多时候不需要开发者做出特殊的处理,只需要避免常见的性能问题即可获得高性能的应用程序. 重建最小化原则 在调用 setState() 方法重建组件时, ...

  5. python3.8.1安装cx_Freeze

    按照官网的提示命令python -m pip install cx_Freeze --upgrade安装,不成功,报了一个错误,说cx_Freeze找不到需要的版本,还有一些警告说PIP需要升级,没理 ...

  6. pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作

    1)指定行索引和列索引标签 index 属性可以指定 DataFrame 结构中的索引数组,  columns 属性可以指定包含列名称的行, 而使用 name 属性,通过对一个 DataFrame 实 ...

  7. 1.5V转5V的最少电路的芯片电路图

    PW5100满足1.5V转5V的很简洁芯片电路,同时达到了最少的元件即可组成DC-DC电路1.5V转5V的升压转换器系统. PW5100在1.5V转5V输出无负载时,输入效率电流极低,典型值10uA. ...

  8. js千分位分隔,数字货币化方法学习记录

    js千分位分隔,数字货币化-4种方法(含正则) 方法1-整数货币化 // 整数货币化 function intCurrency(num) { var reg = new RegExp("^[ ...

  9. 解决Ajax同源政策的方法【JSONP + CORS + 服务器端解决方案】

    解决Ajax同源政策的方法 使用JSONP解决同源限制问题 jsonp是json with padding的缩写,它不属于Ajax请求,但它可以模以Ajax请求.\ 步骤 1.将不同源的服务器端请求地 ...

  10. .net core 不同地区时间相互转换

    .net core 不同地区时间相互转换 //韩国时间转换成当前时间 //value=需要转换的时间 //Korea Standard Tim 韩国时间 //China Standard Time 中 ...