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 ...
随机推荐
- 嗯,python
总觉得在这么个地方已经没有在碰blog的可能了...但是... 突然说要用python来配置环境...好歹也是这个专业的啊...还是 看看吧... 然后 百度一搜,看到一个 好的 网站,不知道 我一旦 ...
- VC调试方法大全
VC调试方法大全 一.调试基础 调试快捷键 F5: 开始调试 Shift+F5: 停止调试 F10: 调试到下一句,这里是单步跟踪 F11: 调试到下一句,跟进函数内部 Shift+F11: ...
- Tesseract5.0训练字库,提高OCR特殊场景识别率(一)
0.目标 很多特殊场景,原生的字库识别率不高,这时候就需要根据需求自己训练字库生成traineddata文件. 一.前期准备工作 1.安装jdk 用于运行jTessBoxEditor 2.安装jT ...
- dp(动态规划之最佳路径+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1078 FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Ot ...
- Kubeadm安装Kubernetes 1.15.1
一.实验环境准备 服务器虚拟机准备 IP CPU 内存 hostname 192.168.198.200 >=2c >=2G master 192.168.198.201 >=2c ...
- 51nod - 1586 - 约数和 - 打表 - 思维
https://www.51nod.com/Challenge/Problem.html#problemId=1586 一眼看过去居然一点思路都没有的,一言不合就打表,打贡献表. #include & ...
- VS2015-MFC基础教程-应用程序工程中文件的组成结构
VS2015应用程序向导生成框架程序后,我们可以在之前设置的Location下看到此文件夹中包含了几个文件和一个以工程名命名的子文件夹,这个子文件夹中又包含了若干个文件和一个res文件夹,创建工程时的 ...
- append动态生成的元素,无法触发事件的原因及解决方案
今天笔者在实现一个简单的动态生成元素功能的时候,发现了一个问题: 使用append动态生成的元素事件绑定失效了. 查阅资料后发现: click(fn)当选中的选择器被点击时触发回调函数fn.只针对与页 ...
- linux查看 rsync 服务状态
[root@rsync-server-1 /]# lsof -i tcp:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync ...
- device eth0/1 does not seem to be present, delaying initialization
vmlite虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟硬盘使用之前的,启动系统后不能上网,通过ifconfig查看网卡没启动,遂启动网卡服务,但是出错,就是:device eth0 doe ...