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 ...
随机推荐
- nginx 403 forbidden 二种原因
nginx 403 forbidden 二种原因 引起nginx 403 forbidden有二种原因,一是缺少索引文件,二权限问题.今天又遇到 了,顺便总结一下. 1,缺少index.html或者i ...
- C语言 百炼成钢26
/* 题目62: 有一下特征字符串"eerrrrqqAB33333ABa333333ABjsfdsfdsa" 编写一个业务函数, 实现功能1:实现按照子串"AB" ...
- 完成blog后台一枚
技术实现:纯jfinal+AmazeUI
- 第二百零七节,jQuery EasyUI,MenuButton(菜单按钮)组件
jQuery EasyUI,MenuButton(菜单按钮)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 MenuButton(菜单按钮)组件的使用方法 ...
- 对hibernate的延迟加载如何理解,在实际应用中,延迟加载与session关闭的矛盾是如何处理的?
对hibernate的延迟加载如何理解,在实际应用中,延迟加载与session关闭的矛盾是如何处理的? 解答:延迟加载就是并不是在读取的时候就把数据加载进来,而是等到使用时再加载.那么Hibernat ...
- 虚拟机(VMware Workstation)安装Ubuntu简易安装
1.安装虚拟机 这里我安装的是:VMware Workstation v12.1.0 官方简体中文版 地址:http://www.3987.com/xiazai/1/12/37116.html#dow ...
- redis 命令行 操作
redis目前提供四种数据类型:string,list,set及zset(sorted set). * string是最简单的类型,你可以理解成与Memcached一模一个的类型,一个key对应一个v ...
- requestAnimationFrame 实现JS动画
requestAnimationFrame会随着浏览器绘制窗口的频率来绘制动画以达到最好的用户体验 // let raf = (function(){ // return window.request ...
- hdu3665-Seaside(SPFA,dijkstra,floyd)
Seaside Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 再谈iOS 7的手势滑动返回功能
本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147 之前随手写过一篇<使用UIScreenEdgePanGestureR ...