题意翻译

题意:给出一个n个数组成的数列a,有t次询问,每次询问为一个[l,r]的区间,求区间内每种数字出现次数的平方×数字的值 的和。

输入:第一行2个正整数n,t。

接下来一行n个正整数,表示数列a1​~an的值。

接下来t行,每行两个正整数l,r,为一次询问。

输出:t行,分别为每次询问的答案。

数据范围:1≤n,t≤2∗105,1≤ai≤106,1≤l,r≤n

题目描述

An array of positive integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1​,a2​,...,an​ is given. Let us consider its arbitrary subarray al,al+1...,ar a_{l},a_{l+1}...,a_{r} al​,al+1​...,ar​ , where 1<=l<=r<=n 1<=l<=r<=n 1<=l<=r<=n . For every positive integer s s s denote by Ks K_{s} Ks​ the number of occurrences of s s s into the subarray. We call the power of the subarray the sum of products Ks⋅Ks⋅s K_{s}·K_{s}·s Ks​⋅Ks​⋅s for every positive integer s s 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 t t given subarrays.

输入输出格式

输入格式:

First line contains two integers n n n and t t t ( 1<=n,t<=200000 1<=n,t<=200000 1<=n,t<=200000 ) — the array length and the number of queries correspondingly.

Second line contains n n n positive integers ai a_{i} ai​ ( 1<=ai<=106 1<=a_{i}<=10^{6} 1<=ai​<=106 ) — the elements of the array.

Next t t t lines contain two positive integers l l l , r r r ( 1<=l<=r<=n 1<=l<=r<=n 1<=l<=r<=n ) each — the indices of the left and the right ends of the corresponding subarray.

输出格式:

Output t t t lines, the i i i -th line of the output should contain single positive integer — the power of the i i 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).

输入输出样例

输入样例#1:

3 2
1 2 1
1 2
1 3
输出样例#1:

3
6
输入样例#2:

8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
输出样例#2:

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.

Solution:

  本题莫队水题。

  维护一下区间内每个数的出现次数,每次左右移动指针的同时,更新下数的次数以及平方和就好了。

代码:

/*Code by 520 -- 10.19*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
#define calc(x) (1ll*x*x)
using namespace std;
const int N=;
int n,m,a[N],c[N],bl[N];
ll ans[N],tot;
struct node{
int l,r,id;
bool operator < (const node &a) const {return bl[l]==bl[a.l]?r<a.r:l<a.l;}
}t[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void add(int x){tot-=calc(c[a[x]])*a[x],c[a[x]]++,tot+=calc(c[a[x]])*a[x];} il void del(int x){tot-=calc(c[a[x]])*a[x],c[a[x]]--,tot+=calc(c[a[x]])*a[x];} int main(){
n=gi(),m=gi(); int blo=sqrt(n);
For(i,,n) a[i]=gi(),bl[i]=(i-)/blo+;
For(i,,m) t[i]=node{gi(),gi(),i};
sort(t+,t+m+);
for(RE int i=,l=,r=;i<=m;i++){
while(l<t[i].l) del(l),l++;
while(l>t[i].l) --l,add(l);
while(r<t[i].r) ++r,add(r);
while(r>t[i].r) del(r),r--;
ans[t[i].id]=tot;
}
For(i,,m) printf("%lld\n",ans[i]);
return ;
}
 
 
 
 
 
 

CF86D Powerful array的更多相关文章

  1. 【题解】Luogu CF86D Powerful array

    原题传送门 裸的莫队啊,我博客里有对莫队较详细的介绍 这道题很简单,可以说是裸的模板 但是如何在已有的值上进行操作? 小学生应该都知道 那么转移就超级简单了qaq inline void add(re ...

  2. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  3. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  4. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  5. D. Powerful array

    D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i] ...

  6. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

  7. D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

    D. Powerful array time limit per test seconds memory limit per test megabytes input standard input o ...

  8. codeforces 86D D. Powerful array(莫队算法)

    题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  9. CodeForces - 86D D. Powerful array —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...

随机推荐

  1. js 深复制一个对象

    自定义 cloneObj 方法 //深复制对象 var cloneObj = function (obj) { var newObj = {}; if (obj instanceof Array) { ...

  2. 【甘道夫】CDH5.2的Maven依赖

    之前一直结合Maven开发Hadoop2.2.0的程序.环境换成CDH5.2后报错,发现是Maven依赖库的问题. 之前一直使用 http://mvnrepository.com/ 查找maven依赖 ...

  3. 20155204 王昊《网络对抗技术》EXP1 PC平台逆向破解

    20155204 王昊<网络对抗技术>EXP1 PC平台逆向破解 (一)实验内容 一.掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即"空指令&qu ...

  4. 20155223 Exp8 WEB基础实践

    20155223 Exp8 WEB基础实践 基础问题回答 什么是表单? 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输入信息的元素. 表单使 ...

  5. 20155234《网路对抗》Exp9 WEB安全基础

    20155234 Exp9 Web安全基础 基础问答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意 ...

  6. vue build,本地正常访问,服务器上,网页一刷新是404,解决办法

    服务器报错如下图: 此原因,是服务器配置的原因,跟build代码本身无关 以ftp为例,在/etc/nginx/conf.d文件夹下,找到xxx.conf,修改成自己需要的路径即可 位置如下两张图:

  7. ES6 之reduce的高级技巧

    reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值.reduce() 方法接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 ...

  8. SpringBoot日记——Docker的使用

    跟进互联网的浪潮有时候也挺难的,还没学完就出现新技术了…… 今天来说说,如何使用docker吧~ docker的安装配置 Docker是一个容器,我们怎么理解这个概念.我们做windows系统的时候会 ...

  9. python中字符串的常见操作方法

    1. 字符串概念,字符串是一个容器,包含若干个字符并按照一定的顺序组织成一个整体.字符串支持索引操作. 2. 创建字符串基本语法 变量名 = "字符串信息" 变量名 = '字符串信 ...

  10. OD之绕过序列号验证(二)

    上次是修改程序的标题,大家应该感觉这只是一个起点而已,接下来我们可以尝试绕过序列号验证,这种技术应用在很多软件中,比如淘宝上要买什么的软件,商家都会发给`你一个用户名和密码,而且还有试用期什么的,这确 ...