CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和。
分析:虽然是道莫队裸体,但是姿势不对就会超时。答案可能爆int,所以要开long long 存答案。一开始的维护操作,我先在res里减掉了a[pos]*cnt[a[pos]]*cnt[a[pos]],将cnt[a[pos]]+1,再将乘积加回。其实根据数学原理,K^2和(K+1)^2差值是2K+1,那么其实每次更新时只要加上或减去a[pos]*(2*cnt[pos]+1)即可,这样更高效。
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn =2e5+;
const int maxv = 1e6+;
int block;
int a[maxn];
int cnt[maxv];
int pos[maxn];
LL ans[maxn],res;
struct Query{
int L,R,id;
bool operator <(const Query &p) {
if(pos[L]==pos[p.L]) return R<p.R;
return pos[L]<pos[p.L];
}
}Q[maxn]; void add(int pos)
{
res+= (LL)(cnt[a[pos]]*+)*a[pos];
cnt[a[pos]]++;
} void del(int pos)
{
cnt[a[pos]]--;
res-= (LL)(cnt[a[pos]]*+)*a[pos];
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,K,u,v,k;
while(scanf("%d%d",&N,&M)==){
block = ceil(sqrt(1.0*N));
memset(cnt,,sizeof(cnt));
for(int i=;i<=N;++i){
scanf("%d",&a[i]);
pos[i]= i /block;
}
for(int i=;i<=M;++i){
scanf("%d%d",&Q[i].L,&Q[i].R);
Q[i].id = i;
}
sort(Q+,Q+M+);
res=;
int curL=,curR=;
for(int i=;i<=M;++i){
while(curL>Q[i].L) add(--curL);
while(curR<Q[i].R) add(++curR);
while(curL<Q[i].L) del(curL++);
while(curR>Q[i].R) del(curR--);
ans[Q[i].id]= res;
}
for(int i=;i<=M;++i)
printf("%lld\n",ans[i]);
}
return ;
}
CodeForces - 86D Powerful array (莫队)的更多相关文章
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字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(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
随机推荐
- datagrid后台分页js
$(function () { gridbind(); bindData(); }); //表格绑定function gridbind() { $('#dg').datagrid({ title: ' ...
- 【BZOJ】1026: [SCOI2009]windy数(数位dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1026 我果然很弱啊... 考虑数位dp.枚举每一位,然后限制下一位即可. 一定要注意啊!在dfs的时 ...
- 面试题思考:java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...
- iOS 修改状态栏颜色
1.首先,苹果把UIViewControllerBasedStatusBarAppearance默认的值设为YES,是有他的道理的,新系统下,苹果希望我们的viewcontroller去控制statu ...
- SpringMVC如何接收json数据
请求头:Content-Type=application/json数据如: {"mobile":"12345678912","smsContent&q ...
- mysql查询某天是本年第几周
SELECT WEEK(date_add('2012-12-31',interval 6 day),2);
- 使用Docker创建Elasticsearch服务
一.Docker是什么? Docker是一个开源工具,能将一个WEB应用封装在一个轻量级,便携且独立的容器里,然后可以运行在几乎任何服务环境下.Docker的容器能使应用跑在任何服务器上并且表现一致. ...
- 在MyEclipse中改了项目名称,到tomcat的webroot下发现,部署的名称还是原来的。
在MyEclipse中改了项目名称,到tomcat的webroot下发现,部署的名称还是原来的. 解决方法:选中项目名称右键properties,展开Myeclipse,选择web,web Conte ...
- 洛谷 [BJOI2012]最多的方案
洛谷 这题是旁边同学介绍的,听他说记忆化搜索可以过... 不过我还是老老实实的想\(dp\)吧- 先看看数据范围,\(n\leq10^{18}\)相当于\(n \leq fib[86]\). 以前打\ ...
- WSGI基础知识(转)
add by zhj: WSGI全称Web Server Gateway Interface,即Web网关接口.其实它并不是OSI七层协议中的协议,它就是一个接口(即函数)而已,而WSGI规定了该接口 ...