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. IOS model的getter和setter方法

    总结: 当使用 self.str1 = @"xxx";时, 系统自动调用 setter方法 param_str = self.str1; 自动调用getter方法注意: 只在对象点 ...

  2. Ubuntu 16.04 安装 VMware-Workstation-12

    以前一直使用 Ubuntu + Virtaulbox ,最近测试了 VMware-Workstation-9,性能超过 Virtaulbox-4.2.x,下面是详细步骤: 1 首先准备一个Ubuntu ...

  3. hrb 2134 素数

    分拆素数和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 176(99 users) Total Accepted: 106(93 u ...

  4. 浅谈B树

    B树即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如:    ...

  5. 坚持不懈之linux haproxy 配置文件 详情

    ####################全局配置信息######################## #######参数是进程级的,通常和操作系统(OS)相关######### global maxc ...

  6. Textview下划线注册用户跳转实现

    在xml中: <TextView android:id="@+id/textView_regtext" android:layout_width="wrap_con ...

  7. python 之验证码

    验证码原理在于后台自动创建一张带有随机内容的图片,然后将内容通过img标签输出到页面. 安装图像处理模块: pip3 install pillow

  8. jqGrid 各种参数 详解

    JQGrid JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面 ...

  9. poj 1328

    http://poj.org/problem?id=1328 题意:题目大概意思就是有一群孤岛,想要用雷达来监视这些岛屿,但雷达的范围是有限的,所以需要多个雷达,题目就是要你解决最少需要几个雷达,注意 ...

  10. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...