Codeforces 86D Powerful array (莫队)
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.
题意:
给你一个序列an 。t次询问,问在[L, R] 的区间里面的值是多少。公式是:ai的个数的平方 * ai 的求和。具体看Note。
题解:
这一题用莫队算法。如果做过小Z的袜子的话,这一题很简单,小Z的袜子是 ai个数的平方求和,这里就乘多了一个ai。
巨坑:num[] 数组要用int 开,用long long开的话会被卡TLE。(很绝望,被卡了10次罚时,最后用输入挂才过的)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +; template<class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(), c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c = getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(), c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
inline void out(LL x)
{
if(x>) out(x/);
putchar(x%+'');
} int a[maxn];
int unit, n, t;
LL ans[maxn];
int num[maxn]; struct node
{
int l, r, id;
}que[maxn];
void init() {
ms(num, );
}
bool cmp(node x1, node x2)
{
if(x1.l/unit != x2.l/unit){
return x1.l/unit < x2.l/unit;
}
else{
return x1.r < x2.r;
}
}
void work()
{
sort(que, que+t, cmp);
LL temp = ;
int l, r;
l = , r = ;
for(int i =;i<t;i++){
while(r<que[i].r){
r++;
temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
num[a[r]]++;
temp += 1LL*num[a[r]]*num[a[r]]*a[r];
}
while(r>que[i].r){
temp -= 1LL*num[a[r]]*num[a[r]]*a[r];
num[a[r]]--;
temp += 1LL*num[a[r]]*num[a[r]]*a[r];
r--;
}
while(l>que[i].l){
l--;
temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
num[a[l]]++;
temp += 1LL*num[a[l]]*num[a[l]]*a[l];
}
while(l<que[i].l){
temp -= 1LL*num[a[l]]*num[a[l]]*a[l];
num[a[l]]--;
temp += 1LL*num[a[l]]*num[a[l]]*a[l];
l++;
}
ans[que[i].id] = temp;
}
}
void solve() {
// scanf("%d%d", &n, &t);
scan_d(n);scan_d(t);
for(int i = ;i<=n;i++)
scan_d(a[i]);
unit = (int)sqrt(n);
for(int i = ;i<t;i++){
que[i].id = i;
// scanf("%d%d", &que[i].l, &que[i].r);
scan_d(que[i].l);
scan_d(que[i].r);
}
work();
for(int i = ;i<t;i++){
// cout << ans[i] << endl;
// printf("%I64d\n", ans[i]);
out(ans[i]);
puts("");
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// ios::sync_with_stdio(0);
// cin.tie(0);
init();
solve();
return ;
}
Codeforces 86D Powerful array (莫队)的更多相关文章
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
随机推荐
- Makefile之patsubst
经常要手写项目的Makefile,或者看其他项目的遗留项目的Makefile,有些makefile内置函数常用, 却用完就忘记了,最近项目中使用patsubst,感觉挺好用的 格式:$(pa ...
- day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...
- js json中的时间转换格式
//根据json中的日期格式,转换成yyyy-mm-dd HH:mm:ss function ChangeDateFormat(cellval) { var date = new Date(parse ...
- SpringBoot 使用logback
1.添加pom引用 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback ...
- [Python3] 009 字符串:给你们看看我的内置方法 第一弹
目录 前言 如何查看 python3 中和 str 有关的方法 字符串方法 1. capitalize() 2. casefold() 3. center(width) 4. count(sub[, ...
- SSI框架【Struts、Spring、iBatis、Hibernate】
1.B/S架构的JavaEE开发设计模式,JavaEE架构分成三个层次即表现层.业务逻辑层.数据持久层:而这三层分别通过Struts.Spring.iBatis开源的框架紧密组合在一起的. Strut ...
- git日常开发中的使用
作者:python技术人 博客:https://www.cnblogs.com/lpdeboke 1.在远程新建一个仓库,可以使github.gitlib或者bitbucket,这里以bitbucke ...
- Forbidden (CSRF token missing or incorrect.):错误解决办法
在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置, ...
- Window Operations
Window Operations 有点类似于Storm中的State,可以设置窗口的大小和滑动窗口的间隔来动态的获取当前Steaming的允许状态,可以对一段时间的数据进行处理. 如图window ...
- ASE Alpha Sprint - backend scrum 9
本次scrum于2019.11.14再sky garden进行,持续15分钟. 参与人: Xin Kang, Zhikai Chen, Jia Ning, Hao Wang 请假: Lihao Ran ...