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 ...
随机推荐
- spring boot教程(一):入门篇(非原创,总结笔记性质)
一,什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- Feign详细使用-Spring Cloud学习第四天(非原创)
文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章 一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...
- spring-cloud - 基础环境搭建
spring-cloud中文文档:https://springcloud.cc/ spring-cloud中文导航:http://springcloud.fun/ 文章纯属用于个人学习的一个归纳,哪里 ...
- Maven自动部署war到Tomcat8
原文:http://www.cnblogs.com/yucongblog/p/5392932.html 我使用的环境是:Eclipse Java EE IDE for Web Developers(V ...
- 移除array中重复的item
//move the repeated item NSInteger index = [orignalArray count] - 1; for (id o ...
- 【前台 ajax】前台ajax请求,地址正确,但是报错不进入后台
前台ajax请求,地址正确,但是报错不进入后台 出现上述问题,可能的情况是 1.ajax用的post,而后台限定用get,或者所有的post请求都被拦截,所以不能正常进入并且报错403 @Reques ...
- nopCommerce从无到有01-初探nopCommerce
nopCommerce框架的基本结构: 该结构可以参考DDD(领域驱动设计)模式. (注:上图源自他人文章,具体出处不祥,在此引用,感谢原创) nopcommerce官方地址:http://www.n ...
- [C++设计模式] proxy 代理模式
代理模式:为其它对象提供一种代理以控制对这个对象的訪问. Proxy: 保存一个引用使得代理能够訪问实体.若RealSubject和Subject的接口同样,Proxy会引用Subject,就相当于在 ...
- hibernate session缓存
Session 概述 Session 接口是 Hibernate 向应用程序提供的操纵数据库的最基本的接口, 它提供了基本的保存, 更新, 删除和载入 Java 对象的方法. Session 具有一个 ...
- (二)MVVMLight 关联View和ViewModel
在我们按照(一)中的步骤,安装好MMVLight的环境后, 会多出一个文件夹ViewModel,里面有两个.cs文件MainViewModel.cs和ViewModelLocator.cs MainV ...