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. [Effective JavaScript 笔记]第54条:将undefined看做“没有值”

    undefined值很特殊,每当js无法提供具体的值时,就会产生undefined. undefined值场景 未赋值的变量的初始值即为undefined. var x; x;//undefined ...

  2. UIImageView 动画 / UIImage 方向

    UIImage 方向 UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向 如果本身是横屏, 照片也是横屏的话, 方向是正方向 BOOL b1 = (originalIma ...

  3. DNS原理及其解析过程【精彩剖析】(转)

      2012-03-21 17:23:10 标签:dig wireshark bind nslookup dns 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...

  4. 发现Select等注入语句自动跳转Code

    CODE区域: <?php $str = $_GET["keyword"]; $str00 = strtolower($str); //strtolower 变为小写函数 $ ...

  5. Android自动登录与记住密码

    // 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...

  6. iPhone取消软件更新上边的1

    去除设置的更新+1小红点提示主要分为越狱和非越狱设备两种方法. 越狱状态下方法: 首先将你的设备进行越狱: 越狱后安装ifile(这个自行搜索安装): 用ifile打开/System/Library/ ...

  7. 【Python】Python AES 对称加密示例

    代码: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex AES_SECRET_KEY = ...

  8. iOS 查看系统字体效果的网页

    常常需要查看字体的样式,这里推荐一个网页http://iosfonts.com,方便查阅.

  9. DP:Corn Fields(POJ 3254)

    北大教你如何高效养牛(误)(点我查看)  2015-08-21: 问题的大意就是有一片稻田,里面有很多坑,你要在上面种稻谷,然后呢田里面还会养牛,牛不喜欢扎堆吃饭,所以呢你种的稻谷要间隔种在坑里面,所 ...

  10. tomcat启动,输出system.out.println()

    tomcat6.0在使用System.out.println("aa")的时候,用cmd启动startup.bat,弹出的那个cmd窗口看到 还可以看logs/catalina.o ...