codeforces 86D : Powerful array
Description
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 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.
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
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
const int size = ;
int n,m,a[MAXN];
int cnt[size];
int kuai;
int nowl,nowr;
LL ans;
LL out[MAXN]; struct wen{
int l,r,id,belong;
}q[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(wen p,wen pp){ if(pp.belong==p.belong) return p.r<pp.r; return p.belong<pp.belong; } inline bool ccmp(wen p,wen pp){ return p.id<pp.id; } inline void add(int x,int type){
if(type==) {
ans+=(LL)(cnt[a[x]]*+)*a[x];
cnt[a[x]]++;
}
else{
cnt[a[x]]--;
ans-=(LL)(cnt[a[x]]*+)*a[x];
}
} inline void work(){
n=getint(); m=getint(); kuai=sqrt(n);
for(int i=;i<=n;i++) a[i]=getint();
for(int i=;i<=m;i++) q[i].l=getint(),q[i].r=getint(),q[i].id=i,q[i].belong=(q[i].l-)/kuai+;
sort(q+,q+m+,cmp);
nowl=q[].l; nowr=q[].r;
for(int i=q[].l;i<=q[].r;i++) {
//ans-=(LL)cnt[a[i]]*cnt[a[i]]*a[i];
ans+=(LL)(cnt[a[i]]*+)*a[i];//简化
cnt[a[i]]++;
//ans+=(LL)cnt[a[i]]*cnt[a[i]]*a[i];
}
out[q[].id]=ans;
//注意nowl、nowr是当前位置,处理完毕的状态
for(int i=;i<=m;i++) {
while(nowl<q[i].l) add(nowl,-),nowl++;
while(nowl>q[i].l) add(nowl-,),nowl--;//nowl已经插入,需要插入nowl-1
while(nowr<q[i].r) add(nowr+,),nowr++;
while(nowr>q[i].r) add(nowr,-),nowr--;
out[q[i].id]=ans;
}
for(int i=;i<=m;i++) cout<<out[i]<<endl;
} int main()
{
work();
return ;
}
codeforces 86D : Powerful array的更多相关文章
- 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 ...
- Codeforces#86D Powerful array(分块暴力)
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- CF 86D Powerful array 【分块算法,n*sqrt(n)】
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数. t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i]) 离线+分块 将n个数分成sqrt(n ...
随机推荐
- WIN7系统自带截图工具SnippingTool
在无网络的情况下,无QQ情况下,如何截图?以及如何设置快捷键? 方法 首先,我们在“开始”菜单最上面找到它,如图: 找不到也没关系 C:\Windows\system32\SnippingTool.e ...
- 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
2016-03-23 10:52:26,860 DEBUG org.apache.commons.beanutils.MethodUtils - Matching name=getEPort on c ...
- Android开发eclipse运行程序时报timeout的解决方法
eclipse开发Android程序时,忽然莫名其妙报这个错,之前还好好的.忽然就不行了. Failed to install xxx.apk on device~~~: timeout 尝试过手机里 ...
- 完成一个MVC+Nhibernate+Jquery-EasyUI信息发布系统
一.最近学习了Jquery-EasyUI框架,结合之前用过的MVC3+Nhibernate做一个信息发布系统,对工作一年半的自己做一个总结吧!(也正好 供初学者学习!) 二.先上截图(系统简介),让大 ...
- C++的CreateThread实例
function CreateThread( lpThreadAttributes: Pointer; {安全设置} dwStackSize: DWORD; ...
- php基础32:正则匹配-修饰符
<?php //正则表达式--修饰符一般放在//的外面 //1. i 表示不区分大小写 $model = "/php/"; $string = "php" ...
- 去他的效应(what-the-hell effect)与自我放纵
去他的 效应(what-the-hell effect)与自我放纵 为什么写这篇文章: 对于我来说,但我感到疲惫——"无意拿起"手机,对自己说"随便看看"——但 ...
- 再次遇到\r\n转\r问题
帮助小伙伴做jenkins的环境搭建.以为5分钟的事情,但是发现了一个诡异的问题.总是提示SVN的url不合法“URL '%s' is not properly URI-encoded”. 由于选择了 ...
- 报错:'Could not load NIB in bundle: 'NSBundle解决办法
1.首先检查拼写是否正确: 2.断开连线,重新连接view与files' owner; 3.规避敏感View名.Xcode中有许多名字是系统预留的.你如果用了也会报这个错误.
- 破解windows server 2008 的登录密码。有效的
今天拿到一块以前服务器上替换下来的老盘,里面还有系统.挂载到另外一台闲置服务器,发现密码忘记了, 结果拿出pe和以前修改xp和2003的系统那样去修改发现不行,不知道为什么,修改SAM文件明明提示成功 ...