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 ...
随机推荐
- golang垃圾回收
常见GC算法 我总结了一下常见的 GC 算法.分别是:引用计数法.Mark-Sweep法.三色标记法.分代收集法. 1. 引用计数法 原理是在每个对象内部维护一个整数值,叫做这个对象的引用计数,当对象 ...
- UITextField增加textDidChange回调功能
在使用UITextField来判断登陆按钮状态时只有 shouldChangeCharactersInRange函数,是在文件还没有改变前就调用了,而不是在改变后调用,要想实现改变后调用的功能,导致登 ...
- 7zip File: How to Uncompress 7z files on Ubuntu, Debian, Fedora
转:http://www.thegeekstuff.com/2010/04/7z-7zip-7za-file-compression/ Question: How do I uncompress a ...
- 使用强大的可视化工具redislive来监控我们的redis
原文:http://www.cnblogs.com/huangxincheng/archive/2016/06/08/5571185.html 作为玩windows的码农,在centos上面装点东西, ...
- 【jQuery】:not选择器的说明和:checked选择器的使用
1.:not选择器的说明使用 先给出一下例子: $(".form1 :not(input[name='category'])") 这个 能实现 获取到from1表单中除了input ...
- github清理,记录一些有趣的项目
1. rhino 一种java做的开源javascript引擎 https://github.com/mozilla/rhino 2. jeewx 国人写的公众号管理后台,集成度有些高,不好剥离.还是 ...
- iOS:地图笔记
地图笔记 01. CLLocation -------------------------------------------------------- CLLocationManager 定位管理者 ...
- JS使用cookie实现DIV提示框只显示一次的方法
本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法.分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第 ...
- RocketMQ的异步调用
这个异步调用方法中传入一个final 回调对象. public void invokeAsyncImpl(final Channel channel, final RemotingCommand re ...
- 学习ajax总结
之前公司的ajax学习分享,做一点总结,加深记忆 什么是ajax? 异步的的js和xml,用js异步形式操作xml,工作主要是数据交互 借阅用户操作时间,减少数据请求,可以无刷新请求数据 创建一个对象 ...