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.

Input

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 lr (1 ≤ 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 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).

Sample Input

Input
3 2
1 2 1
1 2
1 3
Output
3
6
Input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
Output
20
20
20

题意:就问问你统计[L,R]中x1,x2,,xi出现次数a1,a2,,ai的ai*ai*xi和。

题解:分块暴力。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef __int64 LL;
const int INF = 0x3f3f3f3f;
const int maxn=200000+100;
LL up[maxn];
int col[maxn],sum[maxn*10],n,m,k;
LL ans;
struct node{
int l,r;
int id;
}q[maxn];
int cmp(node l1,node l2)
{
if(l1.l/k==l2.l/k)
return l1.r<l2.r;
return l1.l<l2.l;
}
void update(int x,int val)
{
ans-=(LL)col[x]*sum[col[x]]*sum[col[x]];
sum[col[x]]+=val;
ans+=(LL)col[x]*sum[col[x]]*sum[col[x]];
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
CLEAR(sum,0);
k=sqrt(n*1.0+0.5);
REPF(i,1,n)
scanf("%d",&col[i]);
REP(i,m)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q,q+m,cmp);
int L=0,R=0;
ans=0;
for(int i=0;i<m;i++)
{
int id=q[i].id;
int l=q[i].l;
int r=q[i].r;
while(L < l)
{
update(L,-1);
L++;
}
while(R > r)
{
update(R,-1);
R--;
}
while(L > l)
{
L--;
update(L,1);
}
while(R < r)
{
R++;
update(R,1);
}
up[id]=ans;
}
for(int i=0;i<m;i++)
printf("%I64d\n",up[i]);
}
return 0;
}

Codeforces#86D Powerful array(分块暴力)的更多相关文章

  1. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  3. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  4. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  5. [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)

    简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i]    (1<= i <= ...

  6. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  7. Codeforces 86D Powerful array(莫队算法)

    和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  8. CodeForces - 86D Powerful array (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  9. 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 ...

随机推荐

  1. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

  2. 【BZOJ】2730: [HNOI2012]矿场搭建【Tarjan找割点】【分联通块割点个数】

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3230  Solved: 1540[Submit][Stat ...

  3. ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

    ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  4. Codeforces Round #293 (Div. 2) D. Ilya and Escalator 概率DP

    D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. DOM-XML(解析与创建)

    /** * 读取(解析)xml文件 * @author Administrator * */ public class DOMRead { public static void main(String ...

  6. HTML5学习笔记3

    7.文档元素 文档元素的主要作用是划分各个不同的内容,让整个页面布局清晰明快,让整个布局具有语义,进一步替代div.基本上没有什么实际作用效果,主要目的是在页面布局时区分各个主题和概念. h1~h6 ...

  7. 数据是企业的无价財富——爱数备份存储柜server的初体验(图文)

    非常早就像上这样一套数据备份系统,每天採用原来的软件备份加手动备份的方式,总有些不是太方便的地方. 加上企业规模的不断扩大,系统的增多,业务数据也日显重要.容不得半点中断和数据丢失.这不,出于对系统数 ...

  8. [转载] 关于matlab GUI的一点心得

    转载自 落落轻尘 [Fig文件方式,即使用菜单File->New->GUI来设计界面] 首先值得注意的是,在低版本matlab上制作的含GUI的m文件一般不能在高版本的matlab上面运行 ...

  9. Windows 8 Metro 应用开发入门(一):开发环境介绍

    摘 要 Windows8已经发布,随之而来的基于WinRT的Metro应用也正向我们走来,正像它所宣传的:光滑.快.现代.看习惯了玻璃.立体风格的应用,或许Metro的简洁能给你留下不一样的体验.Vi ...

  10. 《精通Ext JS 》

    <精通Ext JS > 基本信息 原书名:Mastering Ext JS 作者: (巴西)Loiane Groner 译者: 卢俊祥 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ...