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. bzoj hash+map+set

    先对原串分组hash,查询就是看某一区间内是否出现某值. 可以每个值存一个集合,保存这个值出现的位置.(也可以建可持久化值域线段树) map<int,set<int> >很省事 ...

  2. Python168的学习笔记7

    关于多线程操作. 对于IO操作,如访问网站,写入磁盘这种需要时间等待响应的操作,多个cpu也几乎不能提高效率. 对于CPU密集型操作,如这个格式转换,可以通过多个cpu同时去进行. 但是对于pytho ...

  3. Loj10094 消息的传递

    题目描述 我们的郭嘉大大在曹操这过得逍遥自在,但是有一天曹操给了他一个任务,在建邺城内有 NNN 个袁绍的奸细,将他们从 111 到 NNN 进行编号,同时他们之间存在一种传递关系,即若Ci,j=1C ...

  4. HDU 5694 BD String 迭代

    BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...

  5. 彻底解决每次打开visio都提示windows正在配置visio的问题

    出现这个提示windows正在配置XXX软件的问题,是由于在安装一个新的版本时,之前那个版本的office没有完全卸载,注册表内有残留. 最简单的方式,并不是 把HKEY_CURRENT_USER\S ...

  6. Apache 如何反向代理tomcat并且实现Session保持

    简介 LAMT=Linux+Apache+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下 ...

  7. SmartProg2 Universal, ISP capable programmer

    http://www.elnec.com/products/universal-programmers/smartprog2/ 40 powerful TTL pindrivers provide H ...

  8. C#程序集系列05,让程序集包含多个module

    本篇体验在一个程序集中包含多个module. □ 创建3个module →删除F盘as文件夹中的一些文件,只剩下如下3个文件→用记事本打开MyFirstModule.cs文件,修改如下,并保存 usi ...

  9. MVC文件上传09-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹,并在其中创建分类子文件夹

    为用户创建专属上传文件夹后,如果想在其中再创建分类子文件夹,该怎么做?可以在提交文件的视图中再添加一个隐藏域,并设置 name="uploadContext". 相关兄弟篇: MV ...

  10. Arcgis Runtime for andriod 100 加载TPK

    private void LoadTPK() { YLPub.pContext = this; String path = YLPub.getMapData() + "/gismap/map ...