codeforces 86D D. Powerful array
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.
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 l, r (1 ≤ l ≤ r ≤ n)
each — the indices of the left and the right ends of the corresponding subarray.
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).
3 2
1 2 1
1 2
1 3
3
6
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
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的更多相关文章
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
随机推荐
- python3实现计算器
实验内容 1.简单计算器的设计 请设计简单的"加减乘除"计算器并从键盘上输入数据进行计算 数字的加减乘除,input返回的结果是str类型的,通过截取字符串中的运算符,来提取数字, ...
- 没搞清楚网络I/O模型?那怎么入门Netty
微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 本文是Netty系列笔记第二篇 Netty是网络应用框架,所以从最本质的角度来看,是对网络I/O模型的封装使用. ...
- (十五)xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要 ...
- Centos6.9安装ACFS
安装完oracle 11GR2的RAC后,使用asmca打开图形化界面后,发现Volumes和ASM Cluster File System两个选项卡不能用 原因是因为ACFS不支持CentOS 解决 ...
- django url别名和反向解析 命名空间
url别名和反向解析 我们平时写的url名字都是死的,如果项目过大,需要项目中某个文件名改动一下,那么改动起来就不是一般的麻烦了,所以我们就在定义的时候给url起一个别名,以后不管哪个文件中运用都是用 ...
- 基于源码分析Vue的nextTick
摘要:本文通过结合官方文档.源码和其他文章整理后,对Vue的nextTick做深入解析.理解本文最好有浏览器事件循环的基础,建议先阅读上文<事件循环Event loop到底是什么>. 一. ...
- 解决PHP无法监听9000端口问题/502错误解决办法
问题背景 配置nginx+php服务的时候,发现网站能打开html,打开php文件就显示502,一般这个是php没启动啊啥的导致不能正常解析php文件. 原因分析 因为nginx解析php文件是交给f ...
- 解决键冲突 — Redis 设计与实现 https://redisbook.com/preview/dict/collision_resolution.html
解决键冲突 - Redis 设计与实现 https://redisbook.com/preview/dict/collision_resolution.html
- SSL_ERROR_WANT_READ
``` 47757 2020/05/07 06:36:04 [debug] 19413#19413: *23421 event timer: 11, old: 15581551413, new: 15 ...
- spring restTemplate 进行http请求的工具类封装
本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...