BZOJ2038差不多。。复习一下。

 #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int block;
struct Query{
int i,l,r;
bool operator<(const Query &q)const{
if(l/block==q.l/block) return r<q.r;
return l/block<q.l/block;
}
}query[];
int cnt[];
long long res,ans[];
void insert(long long x){
res-=x*cnt[x]*cnt[x];
++cnt[x];
res+=x*cnt[x]*cnt[x];
}
void remove(long long x){
res-=x*cnt[x]*cnt[x];
--cnt[x];
res+=x*cnt[x]*cnt[x];
}
int a[];
int main(){
int n,t;
scanf("%d%d",&n,&t);
for(int i=; i<=n; ++i) scanf("%d",a+i);
block=(int)sqrt(n);
for(int i=; i<t; ++i){
query[i].i=i;
scanf("%d%d",&query[i].l,&query[i].r);
}
sort(query,query+t);
int l=,r=; cnt[a[]]=; res=a[];
for(int i=; i<t; ++i){
while(l<query[i].l) remove(a[l++]);
while(l>query[i].l) insert(a[--l]);
while(r>query[i].r) remove(a[r--]);
while(r<query[i].r) insert(a[++r]);
ans[query[i].i]=res;
}
for(int i=; i<t; ++i){
printf("%I64d\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 (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  3. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  4. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

  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 D. Powerful array(莫队算法)

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

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

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  8. Codeforces 86D Powerful array (莫队)

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

  9. codeforces 86D,Powerful array 莫队

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

  10. codeforces 86D : Powerful array

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

随机推荐

  1. Centos6.5更新e1000网卡驱动

    导读 在工作过程中经常遇到linux的操作系统网络不正常的情况,以前没有注意到,今天查看系统日志发现原来是网络驱动的问题.索性直接更新系统,更新网卡 问题:linux系统经常出现断网的情况,重启之后系 ...

  2. CSS3弹性盒模型flexbox完整版教程

    http://caibaojian.com/flexbox-guide.html 来自CSS Tricks上的一个教程,原文为:A Complete Guide to Flexbox.文中详细的介绍了 ...

  3. dhcp原理、安装、相关命令、疑惑

    转自: http://blog.sina.com.cn/s/blog_642e41c20101tct3.html

  4. eclipse加速之禁用JS、jsp等文件的语法验证,eclipsejs

    eclipse加速之禁用JS.jsp等文件的语法验证 去除eclipse的JS验证:将windows->preference->Java Script->Validator-> ...

  5. 【转】 Mybatis/Ibatis,数据库操作的返回值

    该问题,我百度了下,根本没发现什么有价值的文章:还是看源代码(详见最后附录)中的注释,最有效了!insert,返回值是:新插入行的主键(primary key):需要包含<selectKey&g ...

  6. 那些臭名昭著的sql

    两个或多个表关联,没写where条件,大量的笛卡尔值,严重时会导致数据库有问题. select * from a, b left join c on b.id = c.id left join d o ...

  7. eclipse4.4 tomcat jrebel使用

    1.下载jrebel破解包 2.去eclipse -->Help -->EclipseMarketplace 下载eclipse jrebel 也可以下载离线安装包,然后eclipse - ...

  8. LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件

    //file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...

  9. Rap 安装和配置

    本机环境 系统:CentOS 6.7 64 位 MySQL 5.6 JDK 1.8 Tomcat 8 Redis 3.0.7 Rap 0.14.1 Rap 说明 官网:https://github.c ...

  10. zsh(yum装包的时候,有时候会不行)

    [root@GIT ~]# yum search zsh =============================== N/S Matched: zsh ====================== ...