题目链接:http://codeforces.com/problemset/problem/86/D

D. Powerful array
time limit per test

5 seconds

memory limit per test

256 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 + 1..., ar,
where 1 ≤ 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 (1 ≤ n, t ≤ 200000)
— the array length and the number of queries correspondingly.

Second line contains n positive integers ai (1 ≤ ai ≤ 106)
— the elements of the array.

Next t lines contain two positive integers lr (1 ≤ 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 64-bit integers in C++. It is preferred to use cout stream
(also you may use %I64d).

Examples
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note

Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

题解:

现场做的题,结果出现了三个错误:

1.模板抄错了,原因是抄了一部分,另一部分类似,所以就复制,然后再修改,结果漏了一个地方没改,又硬是发现不了,结果卡了n久。所以以后还是老老实实地一个字符一个字符地打模板。

2.有个数组开小了,要常常注意n的范围和ai的范围。

3.把变量全部定为long long,结果超时了,而用int就过了。听说是long long的字节比较长,处理起来就相对慢了。所以以后在int范围内的变量就尽量用int定义了, 如果计算超出范围,加个:1LL*


代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; int n,m;
struct Query
{
int L, R, id;
}node[maxn]; LL ans[maxn], tmp; int a[maxn], num[maxn*], unit; //注意num[]下标的最大值为1e6
bool cmp(Query a, Query b)
{
if(a.L/unit != b.L/unit) return a.L/unit <b.L/unit;
else return a.R<b.R;
} void add(int val)
{
tmp -= 1LL*num[val]*num[val]*val;
num[val]++;
tmp += 1LL*num[val]*num[val]*val;
} void del(int val)
{
tmp -= 1LL*num[val]*num[val]*val;
num[val]--;
tmp += 1LL*num[val]*num[val]*val;
} void work()
{
tmp = ;
ms(num,);
int L = ;
int R = ;
for(int i = ; i<m; i++)
{
while(R<node[i].R) R++, add(a[R]);
while(R>node[i].R) del(a[R]), R--;
while(L<node[i].L) del(a[L]), L++;
while(L>node[i].L) L--, add(a[L]);
ans[node[i].id] = tmp;
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i = ; i<=n; i++)
scanf("%d",&a[i]);
for(int i = ; i<m; i++)
{
node[i].id = i;
scanf("%d%d",&node[i].L, &node[i].R);
}
unit = (int)sqrt(n);
sort(node,node+m,cmp);
work();
for(int i = ; i<m; i++)
printf("%I64d\n",ans[i]);
}

CodeForces - 86D D. Powerful array —— 莫队算法的更多相关文章

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

    题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  2. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

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

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

  4. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

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

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  6. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  7. codeforces 86D D. Powerful array

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

  8. CodeForces - 86D Powerful array (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  9. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. SpringBoot+Mybatis增删改查实战

    简介 SpringBoot和Mybatis是啥请自行百度,作者这里也是花了几天时间入门了这个框架用来完成任务,并且也算符合要求的完成了任务,期间也各种百度但是没找到自己想要的那种简单易懂的教程,所以踩 ...

  2. python--文本处理1

    1.字符和字符值之间的转换 内建函数:ord(),chr() >>> print ord("a") 97 >>> print chr(97) a ...

  3. tomcat启动文件

    home目录下创建run.bat文件,文件内容如下: @echo off set JAVA_OPTS=-server -Xms1024m -Xmx1024m ^-XX:+UseG1GC ^-XX:Ma ...

  4. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  5. Cocos2d-x 3.0 打造一个全平台概念文件夹

    Cocos2d-x 3.0 打造一个全平台概念文件夹http:// www.eoeandroid.com/thread-328055-1-1.html

  6. 【分布式计算】DFS &amp;&amp; BigTable

    1.背景 分布式计算的发迹应该是google在2003年发表的三篇paper.各自是GFS.MapReduce.BigTable. 当中MapReduce大家都非常熟悉了.不懂的同学也能够看看我之前写 ...

  7. JavaScript插件编写指南

    在编写插件之前,大家要先了解做插件的几个要点: 插件需要满足的条件 一个可复用的插件需要满足以下条件: 插件自身的作用域与用户当前的作用域相互独立,也就是插件内部的私有变量不能影响使用者的环境变量: ...

  8. HDMI

    HDMI,全称为(High Definition Multimedia Interface)高清多媒体接口,主要用于传输高清音视频信号. 现在是HDMI2.0.新一代的HDMI2.0较上一代最大的提升 ...

  9. win7自带照片查看器

    win10如何找回自带的照片查看器   方法/步骤   1 首先,我们打开一个记事本,可以点击win+r打开运行框,然后在运行框中输入notepad.或者在桌面右键点击里面的新建,然后在新建中找到文本 ...

  10. 算法排序-lowB三人组

    冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇