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. Online Object Tracking: A Benchmark 论文笔记(转)

    转自:http://blog.csdn.net/lanbing510/article/details/40411877 有博主翻译了这篇论文:http://blog.csdn.net/roamer_n ...

  2. raw格式镜像文件压缩并转换为qcow2格式

    raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...

  3. UML基础:统一建模语言简介

    目录 背景知识 用例图 类图 序列图 状态图 活动图 组件图 部署图 结束语 英文原文:UML basics: An introduction to the Unified Modeling Lang ...

  4. codeforces B. Valera and Contest 解题报告

    题目链接:http://codeforces.com/problemset/problem/369/B 题目意思:给出6个整数, n, k, l, r, sall, sk ,需要找出一个满足下列条件的 ...

  5. Java中的内存分配机制

    Java的内存分为两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型变量和对象的引用都在函数的栈内存中分配.当在一个代码块中定义一个变量的时候,java就在栈中为其分配内存,当超过作用域的 ...

  6. ubuntu修改文件访问权限

    遇到“bash .....权限不够”的问题时, 从控制台进入到那个文件夹 chmod 777 * -R 全部子目录及文件权限改为 777

  7. android 中如何分析内存泄漏

    转载:http://blog.csdn.net/fulinwsuafcie/article/details/8363218 前提条件: 1,电脑安装了java 运行环境 2,手机端开启了 USB 调试 ...

  8. MVC登录案例

    1.在Controllers文件夹里面新建一个控制器HomeController;2.在默认的Index方法里面添加一个视图,名字跟Controller中的方法名一样叫Index,添加后的视图文件会在 ...

  9. Json简介与转换数据例子

    Json是什么,Json就是javascript对象或者数组格式的字符串,Http协议不能传递JavaScript对象,所以要转换为字符串进行传输.AJAX传递复杂数据如果自己进行格式定义的话会经历组 ...

  10. BST树

    http://www.cnblogs.com/bizhu/archive/2012/08/19/2646328.html 4. 二叉查找树(BST) Technorati 标记: 二叉查找树,BST, ...