题意:查询的是区间内每个数出现次数的平方×该数值的和。

分析:虽然是道莫队裸体,但是姿势不对就会超时。答案可能爆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 (莫队)的更多相关文章

  1. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字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(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  5. CodeForces - 86D D. Powerful array —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...

  6. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

  7. codeforces 86D D. Powerful array(莫队算法)

    题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  8. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  9. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

随机推荐

  1. php 计算时间添加

    $Date_1=date("Y-m-d");$Date_2="2015-10-11";$d1=strtotime($Date_1);$d2=strtotime( ...

  2. nodejs 聊天室简单实现

    前言 博客园的样式真心不会用啊,看着大大们的博客各种好看,心里无奈啊,只能慢慢摸索了. 最近的项目nodejs+wcf+app,app直接从wcf服务获取数据,nodejs作为单独的服务器为app提供 ...

  3. String类和StringBuffer类

    位于java.lang包中,这个包中的类使用时不用导入 String类一旦初始化就不可以改变,而stringbuffer则可以.它用于封装内容可变的字符串.它可以使用tostring()转换成stri ...

  4. Java1.0的AWT(旧AWT)和Java1.1以后的AWT(新AWT)有着很大的区别

    Java1.0的AWT(旧AWT)和Java1.1以后的AWT(新AWT)有着很大的区别,新的AWT克服了旧AWT的很多缺点,在设计上有较大改进,使用也更方便,这里主要介绍新的AWT, 但在Java1 ...

  5. ashx一般处理程序

    说明:    虽然通过标准的方式可以创建处理程序,但是实现的步骤比较复杂,为了方便网站开发中对处理程序的应用,从Asp.net 2.0开始,asp.net提供了称为一般处理程序的处理程序,允许我们使用 ...

  6. Petrozavodsk Summer-2015. Ivan Smirnov Contest 1 B Bloom

    http://opentrains.snarknews.info/~ejudge/team.cgi?contest_id=001463 题目大意:给出$n$个$x$,$m$个$y$,问有多少个hash ...

  7. LeetCode 第 231 题 (Power of Two)

    LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...

  8. mui 单页面下拉刷新

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. ChemDraw的“键”工具的作用是什么

    ChemDraw的“键”工具在化学研究中能够发挥重大作用,如果你也是一名化学相关工作者就千万不要错过这篇教程,下文详细盘点“键”工具究竟能干些什么. ChemDraw化学软件免费获取地址:http:/ ...

  10. 源码分析——Action代理类的工作

     Action代理类的新建 通过<Struts2 源码分析——调结者(Dispatcher)之执行action>章节我们知道执行action请求,最后会落到Dispatcher类的serv ...