题目链接: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. 【NOIP考试范围】

    ※号为可能考察的算法[历年有出现过,但概率小,但最好掌握] [本图片仅作参考] 梦想总是要有的,万一实现了呢?

  2. mysql主从复制 master和slave配置的参数大全

    master所有参数1 log-bin=mysql-bin 1.控制master的是否开启binlog记录功能: 2.二进制文件最好放在单独的目录下,这不但方便优化.更方便维护. 3.重新命名二进制日 ...

  3. VC 生成后事件 Post-Build Event

    原文链接地址:https://blog.csdn.net/jfkidear/article/details/27313643.https://blog.csdn.net/kevindr/article ...

  4. 【Python3的命名空间与作用域,闭包函数,装饰器】

    一.命名空间与作用域 命名空间是名字和对象的映射,就像是字典,key是变量名,value是变量的值 1.命名空间的定义 name='egon' #定义变量 def func(): #定义函数 pass ...

  5. Codeforces 932.D Tree

    D. Tree time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...

  6. 手脱ACProtect v1.35(无Stolen Code)

    1.载入PEID ACProtect v1.35 -> risco software Inc. & Anticrack Soft 2.载入OD,需要注意的是,异常选项除了[内存访问异常] ...

  7. Java的StringAPI的小练习

    //-------------String类-------------- //求两个字符串的最大相同子串 /* 思路: 1.找出较短的那个字符串 2.找出短串的所有子串,使用contains函数判断是 ...

  8. java多线程机制1(线程创建的两种方式)

    进程:正在运行的程序.(即程序在内存中开辟了一片空间) 线程:是进程的执行单元. 一个进程至少包含了一个多个线程. 多线程是不是可以提高效率:多线程可以合理的利用系统的资源,提高效率是相对的.因为cp ...

  9. 【BZOJ】2406 矩阵

    [算法]二分+有源汇上下界可行流 [题解]上下界 题解参考:[BZOJ2406]矩阵(二分+有源汇有上下界的可行流) #include<cstdio> #include<algori ...

  10. 【BZOJ】1036 [ZJOI2008]树的统计Count

    [算法]树链剖分+线段树 [题解]模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include& ...