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
output
standard output An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + ..., ar, where ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite. You should calculate the power of t given subarrays.
Input First line contains two integers n and t ( ≤ n, t ≤ ) — the array length and the number of queries correspondingly. Second line contains n positive integers ai ( ≤ ai ≤ ) — the elements of the array. Next t lines contain two positive integers l, r ( ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.
Output Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray. Please, do not use %lld specificator to read or write -bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
Examples
Input Output Input Output Note Consider the following array (see the second sample) and its [, ] subarray (elements of the subarray are colored):
Then K1 = , K2 = , K3 = , so the power is equal to · + · + · = . /**
题目:D. Powerful array
链接:http://codeforces.com/problemset/problem/86/D
题意:给定n个数,m次查询;每次查询[l,r]的权值;
权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x;
所有贡献和即为该区间的值;
思路:由于静态区间,所以离线+莫队算法; 然后(cnt+1)*(cnt+1)*x-cnt*cnt*x=(2*cnt+1)*x;
来优化计算;常规暴力计算超时; */
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps=1e-;
const int maxn = 2e5+;
const ll mod = 1e9+;
ll num[], c[maxn], pos[maxn];
ll ans;
int n , m;
struct node
{
ll l, r;
ll ans;
int id; }t[maxn];
int cmp_id(node a,node b)
{
return a.id<b.id;
}
int cmp(node a,node b)
{
if(pos[a.l]==pos[b.l]) return a.r<b.r;
return pos[a.l]<pos[b.l];
}
void update(int place,int add)
{
ll v = c[place];
if(add==){
ans += v*(*num[v]+);
num[v]++;
}else
{
num[v]--;
ans -= v*(*num[v]+);
}
}
void solve()
{
memset(num, , sizeof num);
ans = ;
for(int i = t[].l; i <= t[].r; i++){
update(i,);
}
t[].ans = ans;
for(int i = ; i <= m; i++){
for(int j = t[i-].l; j < t[i].l; j++) update(j,-);//减得时候,自身开始;
for(int j = t[i-].l; j > t[i].l; j--) update(j-,);//增的时候,不包括自身;
for(int j = t[i-].r; j < t[i].r; j++) update(j+,);
for(int j = t[i-].r; j > t[i].r; j--) update(j,-);
t[i].ans = ans;
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
for(int i = ; i <= n; i++) scanf("%I64d",&c[i]); for(int i = ; i <= m; i++){
scanf("%I64d%I64d",&t[i].l,&t[i].r);
t[i].id = i;
} int N = int(sqrt(n));
for(int i = ; i <= n; i++){
pos[i] = i/N+;
} sort(t+,t++m,cmp);
solve();
sort(t+,t++m,cmp_id);
for(int i = ; i <= m; i++){
printf("%I64d\n",t[i].ans);
}
}
return ;
}
D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;的更多相关文章
- 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(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- [BZOJ3781]:小B的询问(离线莫队算法)
题目传送门 题目描述 小B有一个序列,包含$N$个$1~K$之间的整数.他一共有$M$个询问,每个询问给定一个区间$[L...R]$,求$\sum \limits_{i=1}^{K}c(i)^2$的值 ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- C++ 莫队算法(转)
胡小兔的良心莫队教程:莫队.带修改莫队.树上莫队 在开始学习莫队之前,照例先甩一道例题:BZOJ 1878 HH的项链. 题意:求区间内数的个数,相同的数只算一次. 在我关于这道题的上一篇题解中, ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
随机推荐
- 动态RIP配置路由表
动态RIP配置路由表 以Router11为例子: (1)配置端口ip(两个端口需要设置两个ip) Router(config)#inter f0/0 Router(config-if)#ip add ...
- React Native学习之自定义Navigator
Navigator还是最常用的组件, 所以自己封装了一个, 使用起来也比较简单, 如下: 首先导入组件 var MLNavigator = require('../Lib/MLNavigator'); ...
- chrome 技巧 记录一些以前不太熟悉的
chrome已经不知道用了多少年了,但是还是有些技巧不熟悉,记录下有用的和自己不熟悉的 如何查看dom的绑定事件(查看jquery的绑定事件) 新版本的network的类型选项哪去了? 在Source ...
- win10下安装mysql5.6 zip形式步骤
1. 解压之后可以将该文件夹改名,放到合适的位置,个人建议把文件夹改名为MySQL Server 5.6,放到C:\Program Files\MySQL路径中. 2. 添加环境变量.path中添加C ...
- java 的 &和&&的区别
public class Test { public static void main(String[] args) { String str = null; if(str != null & ...
- apache poi合并单元格设置边框
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...
- 【AS3 Coder】任务八:没剧情还玩毛RPG
使用框架:AS3任务描述:了解RPG游戏中剧情播放器的制作原理及流程难度系数:3(了解原理,能根据XML文件播放剧情) / 5(会制作剧情编辑器) 本章源码下载:http://www.iamseven ...
- Spark(一)-- Standalone HA的部署
首先交代一下集群的规模和配置 集群有六台机器,均是VM虚拟机,每台256M的内存(原谅楼主物理机硬件不太给力) 主机名分别是cloud1~cloud6 每台机器上都装有jdk6,和hadoop-2.2 ...
- Node.js meitulu图片批量下载爬虫1.03版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- Centos 7 修改yum源为阿里源
因为官方的yum源在国内访问效果不佳, 需要改为国内比较好的阿里云或者网易的yum源, 具体修改步骤如下: cd /etc/yum.repos.d # 备份旧的配置文件 mv CentOS-Base. ...