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.
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 l, r (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
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
题意:就问问你统计[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(分块暴力)的更多相关文章
- 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 ...
- [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)
简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i] (1<= i <= ...
- 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 ...
- 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 ...
随机推荐
- Python学习笔记(六)—元组的操作
元祖也是一个列表,它和list的区别是元祖里面的元素无法修改: 如果元祖里面只有一个元素的话,那么你必须在这个元素后边加上逗号,这样才是元祖的类型:否则类型会显示其他类型 元组的定义: 元祖中的方法: ...
- Python学习笔记(五)—列表的学习
总结内容: 1.list的定义 2.list的取值 3.list数据的增加 4.list数据的删除 5.list数据的修改 6.list数据的查询 7.list方法的介绍 8.list的合并 9.多维 ...
- webpack vuejs 和 vue-router 如何使用?
读本文之前,建议对webpack和vuejs有初步的了解,通过webpack的官网和vuejs的中文官网了解即可 网站主要目录://某些文件不一定全部罗列出来,注意观察 vue-wepack -src ...
- 知识共享 - creative commons
Creative Commons,简称CC,中国大陆正式名称为知识共享,台湾正式名称为创用CC. 是一个非营利组织,也是一种创作的授权方式. 此组织的主要宗旨是增加创意作品的流通可及性,作为其他人据以 ...
- HUST 1017 Exact cover(DLX精确覆盖)
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- 使用Facade模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送
Facade模式对外提供了统一的接口,而隐藏了内部细节.在网上购物的场景中,当点击提交订单按钮,与此订单相关的库存.订单确认.折扣.确认支付.完成支付.物流配送等都要做相应的动作.本篇尝试使用Faca ...
- PhotoShop CS6 在2K屏幕下标题菜单等字体太小
对于此类问题,我更喜欢直接了当,不作解释,解决方法如下(大面积参考互联网内容): (1)Win+R按键打开运行对话框, 输入regedit,打开注册表. (2)展开HKEY_LOCAL_MACHINE ...
- NativeXml
NativeXml GITHUB: https://github.com/kattunga/NativeXml THIS IS A FORK WITH SOME FIXES AND IMPROVEME ...
- 学习node js 之微信公众帐号接口开发 准备工作之三
app.js文件介绍,因为也是初学,以下的内容是个人的理解,有些不正确的地方请评论中指证:以注解的形式说明. //依赖组件[模块]导入 var express = require('express') ...
- javax.mail 遇到501 mail from address must be same as authorization user 的問題
使用不同的兩個帳戶发送email时,第一个账户可以发送成功,但到第二个账户的时候就报出了501 mail from address must be same as authorization user ...