Yandex.Algorithm 2011 Round 2 D. 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.
莫队板子题;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=1e9+;
/// 数组大小
int pos[N],k,a[N],ji[M];
struct is
{
int l,r,p;
bool operator <(const is &b)const
{
if(pos[l]==pos[b.l])
return r<b.r;
return pos[l]<pos[b.l];
}
}s[N];
ll ans;
void add(int x)
{
ans-=1LL*ji[a[x]]*ji[a[x]]*a[x];
ji[a[x]]++;
ans+=1LL*ji[a[x]]*ji[a[x]]*a[x];
}
void del(int x)
{
ans-=1LL*ji[a[x]]*ji[a[x]]*a[x];
ji[a[x]]--;
ans+=1LL*ji[a[x]]*ji[a[x]]*a[x];
}
ll out[N];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
k=sqrt(n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),pos[i]=(i-)/k+;
for(int i=;i<=q;i++)
scanf("%d%d",&s[i].l,&s[i].r),s[i].p=i;
sort(s+,s++q);
int L=,R=;
for(int i=;i<=q;i++)
{
while(L<s[i].l)
{
del(L);
L++;
}
while(L>s[i].l)
{
L--;
add(L);
}
while(R>s[i].r)
{
del(R);
R--;
}
while(R<s[i].r)
{
R++;
add(R);
}
out[s[i].p]=ans;
}
for(int i=;i<=q;i++)
printf("%lld\n",out[i]);
return ;
}
Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队的更多相关文章
- CodeForces 86D(Yandex.Algorithm 2011 Round 2)
思路:莫队算法,离线操作,将所有询问的左端点进行分块(分成sqrt(n) 块每块sqrt(n)个),用左端点的块号进行排序小的在前,块号相等的,右端点小的在前面. 这样要是两个相邻的查询在同一块内左端 ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- 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(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
随机推荐
- apply、map、applymap、Dropna
DataFrame常用易混淆方法 apply && map && applymap 1.apply():作用在一维的向量上时,可以使用apply来完成,如下所示 2.m ...
- Docker Quickstart Terminal: exit status 255 解决办法
原文地址:https://www.jianshu.com/p/061f1ae69937 初识docker,还有先拿Windows 7 尝试下,官方提供了docker toolbox,下载后一键安装,桌 ...
- liferay中数据库表的解析未完
页面布局 1:表layout 主要的字段有: 字段 privateLayout 0表示的是公开的页面 字段 layoutId 如果在同一个社区中有很多的界面,layoutId表示各个界面,按照顺序排列 ...
- 【Java】系统漏洞:关于用户登录后操作的注意事项
项目背景: SpringMVC + Mybatis + MySql数据库(javaWeb项目开发) 相关模块:登录,个人详细信息修改,订单详情查询 相关漏洞介绍: 1.登录的验证码:登录的验证码一定 ...
- c语言的字符串拷贝函数的精简
#include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){ while ((*to ...
- Integer类之成员变量
一.一共11个成员变量. 二.详情介绍. 1.value值.这个是Integer类的唯一标志.最重要的实例属性. 2.最小值和最大值常量.注意,计算机里面是以补码形式保存的,因此用十六进制时,给的数据 ...
- SV中的数据类型
Verilog-1995中规定的数据类型有:变量(reg), 线网(wire), 32位有符号数(integer), 64位无符号数(time), 浮点数(real). SV扩展了reg类型为logi ...
- 5Lambda表达式
C++11中的Lambda表达式用于定义并创建匿名的函数对象,以简化编程工作.首先看一下Lambda表达式的基本构成: [函数对象参数](操作符重载函数参数)mutable或exception -&g ...
- Yii2 配置 Nginx 伪静态
主要检查以下代码: location / { # Redirect everything that isn't a real file to index.php try_files $uri $uri ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON MirrorImage2
zw版[转发·台湾nvp系列Delphi例程]HALCON MirrorImage2 procedure TForm1.Button1Click(Sender: TObject);var op: HO ...