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. Problem F: 铺地砖

    Description 元旦过去了,新年大酬宾活动也已经告一段落了.陈盖历望着堆在仓库的瓷砖,很无聊的他把这些瓷砖裁成很多1X1 1X2 1X3的小瓷砖,然后他把这些小瓷砖排在地上画的一个1*n的长方 ...

  2. Python168的学习笔记8

    #coding:utf8 #斐波那契数列,第三项起,每一项都等于前两项之和 def memo(func): cache = {}#闭包 def wrap(*args): if args not in ...

  3. MYSQL-5.5.37-win32.msi 这个版本得程序包谁有吗 可以给我一下吗?

    之前下载了这个版本得mysql   但是跟服务器链接不上   后来我就卸载了  但由于卸载不干净  现在又删了注册表   好像把这个程序包得什么文件删除了  现在提示配置文件错误  所以有这个程序包得 ...

  4. 使用NFS启动Tiny4412开发板根文件系统

      1.Ubuntu14.04上搭建NFS服务 1.1.安装NFS服务 $ sudo apt-get install nfs-kernel-server    //安装NFS服务 1.2 创建Tiny ...

  5. Windows Azure 系列-- Azure Queue的操作

    - Storage Account. 和之前介绍的Azure Table和AzureBlob一样.你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了, ...

  6. mysql安装三 linux源码安装mysql5.6.22

    http://blog.csdn.net/beiigang/article/details/43053803

  7. Qt on Android: Android SDK安装

    之前我在 <Windows下Qt 5.2 for Android开发入门>一文中介绍了 Windows 下 Qt on Android 开发环境的搭建,略过了 Android SDK 的安 ...

  8. MVC扩展Filter, 通过继承AuthorizationAttribute限制IP

    为什么需要AuthorizationAttribute 在没有Authorization系统属性之前,我们可能这样判断:Request.IsAuthenticated && User. ...

  9. [翻译] 如何学习Cocoa

    [文章原地址]http://funwithobjc.tumblr.com/个人翻译,难免会有错误,请各位看官海涵,翻译在末尾本人将在文章的部分地方添加注释,并根据需求增减文章内容,在此对原作者辛勤劳作 ...

  10. apache基金会项目及甲骨文项目汇总

    Apache软件基金会 顶级项目 ▪ ActiveMQ ▪ Ant ▪ Apache HTTP Server ▪ APR ▪ Beehive ▪ Camel ▪ Cassandra ▪ Cayenne ...