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 ...
随机推荐
- BZOJ 4197 NOI 2015 寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...
- 升级Tornado到4后weibo oauth登录不了
把 Tornado 升级到4后,发现正常运行的微博登录不可以了. 原因是4已经移除 RequestHandler.async_callback and WebSocketHandler.async_c ...
- Caffe2(1)----Ubantu14.04安装
英文好的请直接参考官方安装文档:Ubantu14.04下的源码编译. Caffe2的安装相比以前Caffe一代的安装,简直有点一键装机的感觉,下面简单总结下Caffe2的安装. 环境:Ubantu14 ...
- ROS知识(6)----卸载ROS系统
步骤方法: 1.首先卸载包 sudo apt-get purge ros-* 2.然后卸载依赖包 sudo apt-get autoremove
- 小程序swiper 快速滑动闪屏
bindchange: function(e){ if(e.detail.source == "touch") { this.setData({ current: current ...
- vue各生命周期适合做的业务逻辑
一.实际项目中使用最多的Vue生命周期大概是 created mounted updated 二.各自适合做的业务逻辑 1. created 相当于是页面刚开始加载的状态,此时不能操作实例的 ...
- 技能树升级——Chrome Headless模式 - 全栈客栈 - SegmentFault
技能树升级--Chrome Headless模式 - 全栈客栈 - SegmentFault TNPM
- WCF实现将服务器端的错误信息返回到客户端
转载:http://www.cnblogs.com/zeroone/articles/2299001.html http://www.it165.net/pro/html/201403/11033.h ...
- 报错:未能加载文件或程序集“WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
□ 背景 通过NuGet安装某程序包后,运行程序出现如上错误. □ 分析 可能是程序集版本不兼容引起的,可以通过NuGet先把程序包删除,然后再安装最新或某个版本的程序包. □ 解决方法 通过 ...
- iOS 下 Podfile 使用方法
配置 Podlist Pod 是 iOS 下包管理工具,类似于 JavaScript 里的 npm 或 yarn. 创建 Podfile 创建 Podfile 有两种方式: 打开 Terminal,在 ...