CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D
5 seconds
256 megabytes
standard input
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.
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 l, r (1 ≤ l ≤ r ≤ n)
each — the indices of the left and the right ends of the corresponding subarray.
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).
3 2
1 2 1
1 2
1 3
3
6
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
20
20
20
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 —— 莫队算法的更多相关文章
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- codeforces 86D D. Powerful array
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
随机推荐
- BZOJ1009GT考试 DP + KMP + 矩陣快速冪
@[DP, KMP, 矩陣快速冪] Description 阿申准备报名参加GT考试,准考证号为\(N\)位数\(X_1 X_2 .. X_n(0 <= X_i <= 9)\),他不希望准 ...
- 为VLC增加在线字幕插件VLSub
VLC的在在线字幕插件VLSub,官网:https://github.com/exebetche/vlsub. 原理是通过搜索全球最大的字幕网站https://www.opensubtitles.or ...
- javascript --- 移除DOM节点
在IE中移除容器类节点,会引起内存泄露,最好是创建一个新的节点,比如div,然后将要删除的节点放入这个div中,再将div的innerHTML清空.其它的直接removeChild就可以了. var ...
- OpenLayers3 动画
参考文章 openlayers3中三种动画实现
- Python基础语法03-控制流
Python 条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和 ...
- vscode格式化代码无效--可能的解决方法
因为vscode默认启用了根据文件类型自动设置tabsize的选项,因此,可以通过关闭自动设置选项,防止格式覆盖.在用户设置里添加如下配置即可: "editor.detectIndentat ...
- nyoj 1077 小博弈 【另类巴什博奕】
分析:分析当整除(a+b)的时候肯定是后者胜利,假设余数不等于0的时候.假设余数大于b肯定是前者胜利,否则后者胜利. 代码: import java.math.*; import java.util. ...
- 网站无法显示logo?
那是因为你没有配置favicon.ico,每个网站根目录都会有一个favicon.ico,因为每个服务器都会请求根目录下的它.
- 使用Lua 局部变量来优化性能,同一时候比較局部变量和全局变量
在竞争激烈的游戏行业中,尤其页游,面对策划复杂和频繁的需求,使用脚本能够减少难度和成本.在使用Lua的过程中,会常常訪问全局变量来作为配置文件. 在訪问全局变量时,能够通过局部变量引用全局变量来优化. ...
- Android——滑动事件冲突解决
android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件. android系统中的每个View的子类都具有下面三个与TouchEvent处理密切相关的方法: (1) ...