Codeforces 86D - Powerful array(莫队算法)
题目链接: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(莫队算法)的更多相关文章
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
随机推荐
- [POJ1094] Sorting It All Out
link 题目大意 给出$m$个不等式关系,问可以从第几个开始确定所有之间的大小关系.若无解请输出是无法确定还是与已知矛盾. 试题分析 这题是真的是坑啊,尽然放在$floyd$传到闭包上面,还用二分, ...
- 在 Xamarin.Forms 实现页面全屏显示
NavigationPage.SetHasNavigationBar(this, false); 或者 <ContentPage ... NavigationPage.HasNavigation ...
- Linux 进程的 Uninterruptible sleep(D) 状态
首先,说一下产生D状态的原因. 上图阐释了一个进程运行的情况,首先,运行的时候,进程会向内核请求一些服务,内核就会将程序挂起进程,并将进程放到parked队列,通常这些进程只会在parked队列中停留 ...
- poj3648 Wedding
Wedding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10975 Accepted: 3355 Specia ...
- struts的status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- ZABBIX 3.0 配置监控NGINX性能【OK】
1.在agent端查看配置: nginx -V //查看编辑时是否加入状态监控模块:--with-http_stub_status_module --with-http_gzip_static_mod ...
- is_int is_float 判断数值是否为整数
is_float — 检测变量是否是浮点型 http://cn.php.net/manual/zh/function.is-float.php is_int — 检测变量是否是整数 http://cn ...
- swiper.js的使用
点击api文档地址, (1)图片轮播banner <script src="js/jquery-2.1.4.min.js"></script> <sc ...
- DES加密解密类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...