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 ...
随机推荐
- 【JavaWeb】jQuery 基础
jQuery 基础 介绍 顾名思义,它是 JavaScript 和 查询,是辅助 JavaScript 开发的类库. 它的核心思想是 write less, do more. 所以它实现了很多浏览器的 ...
- 【Java】网络编程之NIO
简单记录 慕课网-解锁网络编程之NIO的前世今生 & 一站式学习Java网络编程 全面理解BIO/NIO/AIO 内容概览 文章目录 1.[了解] NIO网络编程模型 1.1.NIO简介 1. ...
- 如何实现微信小程序动画?添加到我的小程序动画实现详细讲解,轻松学会动画开发!附壁纸小程序源码下载链接
为了让用户能尽可能多地使用小程序,也算是沉淀用户,现在很多小程序中,都有引导用户"添加到我的小程序"的操作提示,而且大多都是有动画效果.在高清壁纸推荐小程序首页,用户每次进入,都会 ...
- Mac中安装Git
Mac 安装git 打开Mac终端输入git命令 如果出现以下代码说明已经安装 usage: git [--version] [--help] [-C <path>] [-c <na ...
- MYSQL基础知识的复习1
数据库(是存放数据的仓库) 1.根据存储量以及安全性上来划分: 大型数据库:DB2 Oracle(毕业) Hbase 银行 公安局(不加班 没网) 移动 中型数据库:mysql sqlserver(. ...
- Redis 实战 —— 06. 持久化选项
持久化选项简介 P61 Redis 提供了两种不同的持久化方法来将数据存储到硬盘里面. RDB(redis database):可以将某一时刻的所有数据都写入硬盘里面.(保存的是数据本身) AOF(a ...
- jackson学习之四:WRAP_ROOT_VALUE(root对象)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 为什么MySQL索引使用B+树
为什么MySQL索引使用B+树 聚簇索引与非聚簇索引 不同的存储引擎,数据文件和索引文件位置是不同的,但是都是在磁盘上而不是内存上,根据索引文件.数据文件是否放在一起而有了分类: 聚簇索引:数据文件和 ...
- Spring框架入门浅析
一.Spring Bean的配置 在需要被Spring框架创建对象的实体类的类声明前面加注解:```@component```.这样在Spring扫描的时候,看到该注解就会在容器中创建该实体类的对象. ...
- (转载)微软数据挖掘算法:Microsoft 决策树分析算法(1)
微软数据挖掘算法:Microsoft 目录篇 介绍: Microsoft 决策树算法是分类和回归算法,用于对离散和连续属性进行预测性建模. 对于离散属性,该算法根据数据集中输入列之间的关系进行预测. ...