题目链接:http://codeforces.com/problemset/problem/86/D

题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求该区间内所有的cnt[i]^2*i。

解题思路:莫队模板题,改一下add,remove就好了。

 #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=2e5+;
const int MAX=1e6+;
int cnt[MAX],arr[N],unit;
ll power,res[N]; struct node{
int l,r;
int id;
}q[N]; void add(int pos){
power=power+(ll)arr[pos]*(cnt[arr[pos]]<<|);//(a+1)*(a+1)=a*a+2*a+1;
cnt[arr[pos]]++;
} void remove(int pos){
power=power-(ll)arr[pos]*(cnt[arr[pos]]<<-);
cnt[arr[pos]]--;
} bool cmp(node a,node b){
return a.l/unit!=b.l/unit?a.l/unit<b.l/unit:a.r<b.r;
} int main(){
int n,m;
scanf("%d%d",&n,&m);
unit=sqrt(n);
for(int i=;i<=n;i++){
scanf("%d",&arr[i]);
}
for(int i=;i<=m;i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+,q++m,cmp); int L=q[].l,R=L-;
for(int i=;i<=m;i++){
while(L>q[i].l)
add(--L);
while(L<q[i].l)
remove(L++);
while(R<q[i].r)
add(++R);
while(R>q[i].r)
remove(R--);
res[q[i].id]=power;
}
for(int i=;i<=m;i++){
printf("%I64d\n",res[i]);
}
}

Codeforces 86D - Powerful array(莫队算法)的更多相关文章

  1. CodeForces - 86D Powerful array (莫队)

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

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

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

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

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

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

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

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

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

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

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

  7. Codeforces 86D Powerful array (莫队)

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

  8. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间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. MyBatis openSession(),close(),和commit() 底层代码剖析

    一:MyBatis工具类 中openSession到底做了什么? Mybatis工具类 private static final String RESOURCE = "mybatis-con ...

  2. bzoj2956: 模积和(数论)

    先算出无限制的情况,再减去i==j的情况. 无限制的情况很好算,有限制的情况需要将式子拆开. 注意最后的地方要用平方和公式,模数+1是6的倍数,于是逆元就是(模数+1)/6 #include<i ...

  3. Spring Swagger URL传参问题(转)

    代码例子: @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息") @ApiImplicitPar ...

  4. Jenkins CI Pipeline scripting

    Jenkins pipeline is a suite of Jenkins plugins. Pipelines can be seen as a sequence of stages to per ...

  5. java中16进制转换10进制

    java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...

  6. gcc和MinGW的异同

    cygwin/gcc和MinGW都是gcc在windows下的编译环境,但是它们有什么区别,在实际工作中如何选择这两种编译器. cygwin/gcc完全可以和在linux下的gcc化做等号,这个可以从 ...

  7. string的内存本质

    虽然没有研究过string的源代码,不过可以确定的是string的内存空间是在堆上开辟的,它自己负责释放空间,不用我们关系. 我们用一个动态分配的字符串指针初始化一个string对象retStr,它会 ...

  8. Packet Tracer 5.0 构建CCNA实验(3)—— 路由器实现vlan间通信

    --交换机配置 --交换机和路由器连接的接口 switchport mode trunk --路由器配置 enable conf t int fa0/0.1 --路由器接口的子接口 encapsula ...

  9. AJAX流程

    创建一个XHR对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari ...

  10. ClassNotFoundException:com.sun.xml.bind.v2.ContextFactory

    项目中引入hive-jdbc-1.2.1-standalone.jar包之后,报错如下: Caused by: javax.xml.bind.JAXBException: Provider com.s ...