[Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x
给定一个序列,以及一些区间询问,求每个区间的贡献
算是莫队算法膜版题,不带修改的
Code
#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 200010
#define ll long long
using namespace std; int n,m,A[N],bl[N],k[N*5];
ll sum,Ans[N];
struct info{
int l,r,id;
friend bool operator <(info a,info b){
return (bl[a.l]==bl[b.l])?a.r<b.r:a.l<b.l;
}
}q[N]; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void upd(int x,int d){
sum-=k[A[x]]*1ll*k[A[x]]*A[x];
k[A[x]]+=d;
sum+=k[A[x]]*1ll*k[A[x]]*A[x];
} int main(){
n=read(),m=read();int blo=sqrt(n);
for(int i=1;i<=n;++i) A[i]=read(),bl[i]=i/blo+1;
for(int i=1;i<=m;++i) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+m+1);
for(int i=1,l=1,r=0;i<=m;++i){
for(;l<q[i].l;++l) upd(l,-1);
for(;l>q[i].l;--l) upd(l-1,1);
for(;r<q[i].r;++r) upd(r+1,1);
for(;r>q[i].r;--r) upd(r,-1);
Ans[q[i].id]=sum;
}
for(int i=1;i<=m;printf("%lld\n",Ans[i++]));
return 0;
}
[Codeforces86D]Powerful array(莫队算法)的更多相关文章
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;
D. Powerful array time limit per test seconds memory limit per test megabytes input standard input o ...
随机推荐
- Assembly测试
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Reflecti ...
- python模块详解 logging
打印日志的五个级别: import logging logging.debug('test debug') logging.info('test info') logging.warning('tes ...
- sudo使用
/etc/sudo.conf /etc/sudoers /etc/sudoers.d/ /etc/sudo-ldap.conf /etc/sudoer sudo安全策略配置文件 Defaults re ...
- http相关文章目录
四种常见的 POST 提交数据方式 https://imququ.com/post/four-ways-to-post-data-in-http.html
- 硬件-ESP32S模块资料
1.产品概述 ESP-32S WiFi 模块是由安信可科技自主设计研发,该模块核心处理器 ESP32提供了一套完整的802.11 b/g/n/e/i 无线局域网(WLAN)和蓝牙4.2解决方案,具有最 ...
- IOS 制作常用代码的快捷方式
输入可以变的对象或类型,要用<#name#>
- andriod给ListView中的TextView增加跑马灯效果
正常情况下跑马灯效果只需要在TextView中添加android:ellipsize="marquee" android:singleLine="true" a ...
- LA 2957 最大流,最短时间,输出路径
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...
- JS显示上一周
<html> <head> <script> var currDT; var aryDay = new Array("日","一&qu ...
- sql插入临时表数据的方法
方法有两种,主要看需求. 方法1:定义好临时表的字段和类型.插入对应的值 create table #Tmp --创建临时表#Tmp ( City varchar(), -- Country varc ...