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
output
standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + ..., ar, where ≤ 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 ( ≤ n, t ≤ ) — the array length and the number of queries correspondingly. Second line contains n positive integers ai ( ≤ ai ≤ ) — the elements of the array. Next t lines contain two positive integers l, r ( ≤ 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 -bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
Examples
Input Output Input Output Note Consider the following array (see the second sample) and its [, ] subarray (elements of the subarray are colored):
Then K1 = , K2 = , K3 = , so the power is equal to · + · + · = . /**
题目:D. Powerful array
链接:http://codeforces.com/problemset/problem/86/D
题意:给定n个数,m次查询;每次查询[l,r]的权值;
权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x;
所有贡献和即为该区间的值;
思路:由于静态区间,所以离线+莫队算法; 然后(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x;
来优化计算;常规暴力计算超时; */
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps=1e-;
const int maxn = 2e5+;
const ll mod = 1e9+;
ll num[], c[maxn], pos[maxn];
ll ans;
int n , m;
struct node
{
ll l, r;
ll ans;
int id; }t[maxn];
int cmp_id(node a,node b)
{
return a.id<b.id;
}
int cmp(node a,node b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void update(int place,int add)
{
ll v = c[place];
if(add==){
ans += v*(*num[v]+);
num[v]++;
}else
{
num[v]--;
ans -= v*(*num[v]+);
}
}
void solve()
{
memset(num, , sizeof num);
ans = ;
for(int i = t[].l; i <= t[].r; i++){
update(i,);
}
t[].ans = ans;
for(int i = ; i <= m; i++){
for(int j = t[i-].l; j < t[i].l; j++) update(j,-);//减得时候,自身开始;
for(int j = t[i-].l; j > t[i].l; j--) update(j-,);//增的时候,不包括自身;
for(int j = t[i-].r; j < t[i].r; j++) update(j+,);
for(int j = t[i-].r; j > t[i].r; j--) update(j,-);
t[i].ans = ans;
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
for(int i = ; i <= n; i++) scanf("%I64d",&c[i]); for(int i = ; i <= m; i++){
scanf("%I64d%I64d",&t[i].l,&t[i].r);
t[i].id = i;
} int N = int(sqrt(n));
for(int i = ; i <= n; i++){
pos[i] = i/N+;
} sort(t+,t++m,cmp);
solve();
sort(t+,t++m,cmp_id);
for(int i = ; i <= m; i++){
printf("%I64d\n",t[i].ans);
}
}
return ;
}
D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;的更多相关文章
- 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(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- [BZOJ3781]:小B的询问(离线莫队算法)
题目传送门 题目描述 小B有一个序列,包含$N$个$1~K$之间的整数.他一共有$M$个询问,每个询问给定一个区间$[L...R]$,求$\sum \limits_{i=1}^{K}c(i)^2$的值 ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- C++ 莫队算法(转)
胡小兔的良心莫队教程:莫队.带修改莫队.树上莫队 在开始学习莫队之前,照例先甩一道例题:BZOJ 1878 HH的项链. 题意:求区间内数的个数,相同的数只算一次. 在我关于这道题的上一篇题解中, ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
随机推荐
- RocketMQ(7)——通信协议
RocketMQ(7)——通信协议 RocketMQ的通信协议其实很简单,但是无论是官方的用户手册,还是网上的博客,并没有很清晰简单地把其中所有的内容和原理讲明白. 对于需要扩展其他语言SDK的开发来 ...
- 【MyEcplise】导入项目后,会定时弹出一下错误MyEcplise tern was unable to complete your request in time.This couble happen if your project contains several large javaScript libraies.
Myecplise弹出错误如下: 错误代码: MyEcplise tern was unable to complete your request in time.This couble happen ...
- Coherence对象压缩以及对象大小计算
1.通过util.zip带的gzip压缩程序 Coherence对象压缩程序如下 package coherencetest; import com.tangosol.net.CacheFactor ...
- virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性
[sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...
- Ubuntu16.04安装Pytorch
一.安装 1. 官方github:https://github.com/pytorch/pytorch Install optional dependencies //安装依赖项 On Linux e ...
- ISP图像调试工程师——3D和2D降噪(熟悉图像预处理和后处理技术)
2D降噪:只在2维空间域上进行降噪处理.基本方法:对一个像素将其与周围像素平均,平均后噪声降低,但缺点是会造成画面模糊,特别是物体边缘部分.因此对这种算法的改进主要是进行边缘检测,边缘部分的像素不用来 ...
- MAC 使用技巧及常用软件备忘
公司转向MAC快一年, 换了MAC PRO半年时间,MAC这东西除了颜值和性能,软件真是不如WINDOWS啊,不是没有,只是好多都收费! 先介绍几个跨平台的. WIN+MAC 通用: 浏览器: CHR ...
- hbase集群安装和shell操作
1.上传hbase安装包 2.解压 3.配置hbase集群,要修改3个文件(首先zk集群已经安装好了) 注意:要把hadoop的hdfs-site.xml和core-site.xml 放到hbase/ ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- python 页面信息抓取
1. 特点 在python 解析html这篇文章中已经做了初步的介绍,接下来再坐进一步的说明.python抓取页面信息有下面两个特点: 依赖于HTML的架构. 微小的变化可能会导致抓取失败,这取决于你 ...