题目链接: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. ros pub sub ("~")

    base_velocity_smoother.cpp                        remap ("~")    订阅: "cmd_vel"   ...

  2. python学习笔记1-numpy/enumerate

    1. np.size和np.prod import numpy as np x = np.zeros((3, 5, 2), dtype=np.complex128) # ndarray.size is ...

  3. javascript好文 --- 深入理解可视区尺寸client

    可视区大小 可视区大小client又称为可见大小或客户区大小,指的是元素内容及其内边距所占据的空间大小 clientHeight clientHeight属性返回元素节点的可见高度 clientHei ...

  4. Don't Panic! KRACK 没你想象的那么糟

    上海交通大学密码与计算机安全实验室(LoCCS)软件安全小组(GoSSIP)版权所有,转载请与作者取得联系! 著名的计算机学术安全会议CCS在2017年录用了一篇名为Key Reinstallatio ...

  5. openERP server action,最强大的功能,没有之一

    Jeffery9@gmail.com 出品 @jeffery-陈帆 原理 ations OE定义了ir.actions.actions,并从中派生了众多的子类 ir.actions.client ir ...

  6. Resharper 8.2的“安装”问题

    概述 完美解决Resharper 8.2的“安装”问题和VS2012写Javascript语句无法智能提示的问题: 目录 引言——Resharper 简介——安装——VS2012智能提示测试 引言 最 ...

  7. python(15)- 装饰器及装饰器的使用

    装饰器 1.无参数 2.函数有参数 3.函数动态参数 4.装饰器参数 装饰器的应用 下面题目同http://www.cnblogs.com/xuyaping/p/6679305.html,只不过加了装 ...

  8. 编码知识 (Unicode、UTF-8、ANSI)

    1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...

  9. Day1[下] - Python基础1 基本语法、流程控制

    一.变量\字符编码 Variables are used to store information to be referenced and manipulated in a computer pro ...

  10. Fedora21 安装视频播放解码器

    12 12月 2014年12月12日 Posted by 涛儿 2 首先启用RPM Fusion软件源: sudo rpm -ivh http://download1.rpmfusion.org/fr ...