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; 所有贡献和即为该区间的值;的更多相关文章

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

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

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

    和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  3. Codeforces 86D Powerful array (莫队)

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

  4. Codeforces D. Powerful array(莫队)

    题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...

  5. [BZOJ3781]:小B的询问(离线莫队算法)

    题目传送门 题目描述 小B有一个序列,包含$N$个$1~K$之间的整数.他一共有$M$个询问,每个询问给定一个区间$[L...R]$,求$\sum \limits_{i=1}^{K}c(i)^2$的值 ...

  6. CodeForces 86D Powerful array(莫队+优化)

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

  7. C++ 莫队算法(转)

    胡小兔的良心莫队教程:莫队.带修改莫队.树上莫队   在开始学习莫队之前,照例先甩一道例题:BZOJ 1878 HH的项链. 题意:求区间内数的个数,相同的数只算一次. 在我关于这道题的上一篇题解中, ...

  8. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

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

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

随机推荐

  1. 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException

    场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...

  2. Easyui的numberbox无法输入以0开头的数字编号(转载)

    1.问题 项目中碰到这样一个问题,Easyui的numberbox在输入数字编号的时候不能以0开头 在我输入以0开头的数字编号后,离开输入框的时候,那个前缀0就自动去掉了. 接下来,我们查看API说明 ...

  3. String的解析

    String作为Java中最常用的引用类型,相对来说基本上都比较熟悉,无论在平时的编码过程中还是在笔试面试中,String都很受到青睐,然而,在使用String过程中,又有较多需要注意的细节之处. 1 ...

  4. windows server 2012将计算机图标添加到桌面(图文教程)(转)

    windows server 2012系统安装完以后桌面默认只有回收站一个图标,如何将window常用的图标(计算机.控制面板.网络.用户文件)的图标添加到桌面呢,下面为作者本人亲测.操作简单至极. ...

  5. c++之list学习

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <list> using namespace std; ...

  6. window安装mysql5.7.11

    1.到mysql官网(http://dev.mysql.com/downloads/mysql/)下载压缩包,我的是win7 64位的,根据自己的系统进行下载 2.解压到自己的目录,我的是 E:\so ...

  7. 【云计算】mesos+marathon 服务发现、负载均衡、监控告警方案

    Mesos-dns 和 Marathon-lb 是mesosphere 官网提供的两种服务发现和负载均衡工具.官方的文档主要针对DCOS,针对其它系统的相关中文文档不多,下面是我在Centos7上的安 ...

  8. angular—— Dynamic Templates

    原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...

  9. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之项目设计

    前言:这个项目是毕设时候做的,我负责后台数据操作部分,已经很久了,这次回顾这部分,是为了复习PHP和MySQL的知识,正好现在在公司也负责的是后台管理系统的业务.第一篇[项目概况]附上毕业论文部分节选 ...

  10. ParallelProgramming-多消费者,多生产者同时运行并行

    在上一篇文章演示了并行的流水线操作(生产者和消费者并行同时执行),C#是通过BlockingCollection这个线程安全的对象作为Buffer,并且结合Task来实现的.但是上一篇文章有个缺陷,在 ...