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 ...
随机推荐
- [Unity2D]2D Mobile Joystick
效果预览 操作步骤 1.下载素材 http://pan.bai du.com/s/1gdkQz8v 2.新建一个GUITexture(Joystick)及一个Sprite(Nyan) 3.添加背景 ...
- NGUI学习笔记汇总
NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...
- Flex xml编辑器(老外写的)
github上的一个项目老外写的xml编辑器,灵活利用了Tree的labelFunction实现节点运行时展现.开源地址是 https://github.com/softinsure/XML-Edit ...
- 【从0到1】android网络框架的选型参考
项目会使用到 socket tcp 级的网络访问,想选取一个使用较成熟异步网络框架, 提到的网络框架: 1. volley, 2. xutils. 3. android 4. netty, 5. mi ...
- javascript获取当前的时间戳
JavaScript 获取当前时间戳:第一种方法: var timestamp = Date.parse(new Date()); 结果:1280977330000第二种方法: var timesta ...
- 在collection view中加入 NavigationController问题
在开发过程中用collectionView集合视图的时候,用navgationController跳转会出现导航栏掩盖部分内容现象, 这时候需要在viewDidLoad里面填写 self.edgesF ...
- 1445 送Q币
1445 送Q币 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 一次在玩网络游戏的过程中,在 ...
- Xcode7 真机调试步骤以及遇到的问题解决办法
打开Xcode7,打开preference 添加自己的apple ID登陆上去 打开一个自己的想要运行在真机上的项目 插上自己的iPhone真机(真机没必要是最新的系统,没必要升级,我刚开始报错以为是 ...
- .net mvc Bundle 问题解决方案
使用.net MVC4 开发Web项目时,可以利用"Bundle"对Css.JS文件进行压缩打包,一方面可以减少数据加载的次数,另一方面可以减少数据传输量,但在实际使用中却遇到了问 ...
- C语言 文件操作7--文件错误处理
//文件错误处理 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include&l ...