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

题目链接:点击传送
D. Powerful array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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 lr (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.

Output

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).

Examples
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note

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+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
///   数组大小
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=1;i<=n;i++)scanf("%d",&a[i]),pos[i]=(i-1)/k+1;for(int i=1;i<=q;i++)scanf("%d%d",&s[i].l,&s[i].r),s[i].p=i;sort(s+1,s+1+q);int L=1,R=0;for(int i=1;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=1;i<=q;i++)printf("%lld\n",out[i]);return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/6662190.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/355887.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html 形状div,div+css实现各种形状(精心整理)

1.正方形.div {width: 100px;height: 100px;background: red;}2.矩形.div {width: 200px;height: 100px;background: red;}3.圆形.div {width: 100px;height: 100px;background: red;-moz-border-radius: 50px;-webkit-border-radius: 50px;border-radius: 50px;}4.椭圆.div {w…

4.1.5事件

用事件有一种监听的思想 一个事件对应一个委托 事件是委托类型 一个事件对应一个委托&#xff0c;委托可以对应多个方法 如气球爆炸了 气球的反应 小孩的反应 事件源有代码自动激发事件 扔出事件 —————————————— 3.只能在类的内部触发事件 4.可以在内部…

485不用双绞线可以吗_现在在上海,挡风玻璃可以一个标志都不用贴吗?

问近日&#xff0c;有市民咨询&#xff1a;现在在上海&#xff0c;车上挡风玻璃可以一个标志都不用贴吗&#xff1f;答可以一个标志都不贴。自2020年3月1日起&#xff0c;本市机动车在本市办理注册、变更、转移登记和核发检验标志、补换检验标志业务时&#xff0c;将自动生成检…

Java和Python中类似Kotlin的生成器,续:附加参数

介绍 在今天的文章中&#xff0c;我们将继续上周的文章&#xff0c;内容涉及用Java和Python制作类似于Kotlin的构建器&#xff0c;扩展构建器API以采用一些可选参数来提高灵活性。 我们继续我们HTML示例&#xff0c;尝试添加标记属性&#xff0c;例如类&#xff0c;id和样式。 …

vs2015 html页面没有试图,vs2015 web设计视图假死,求帮助

你好&#xff0c;首先&#xff0c;请在发送一个请求去验证你的MSDN账号。然后&#xff0c;从你的描述来看&#xff0c;即使创建新项目&#xff0c;VS的design界面出现不了还是你的VS安装有问题。我们可以从两方面考虑&#xff1a;1.系统环境建议你检查一下Windows系统更新&…

nodejs 之 nvm和pm2

说道 node不得不提到nodejs的版本管理nvm和Node应用的进程管理器pm2。 当然&#xff0c;关于这两个的介绍的文章那么多&#xff0c;随意baidu,bing,google就可以。 我这里是给自己打一个标签&#xff0c;方便日后 查询&#xff0c; 二是方便一些和我一样的小白&#xff0c;能速…

2021上饶市高考中考成绩查询,2021年上饶中考成绩公布查询时间 上饶中考成绩查询方式入口...

2018年上饶中考成绩公布查询时间 上饶中考成绩查询方式入口2018年上饶高考结束了&#xff0c;可是上饶中考又来了!6月注定是一个不平淡的月份。又一大批孩子朝着青春出发踏着坚定的脚步&#xff0c;行走在如火的六月。宁静的清晨&#xff0c;静谧的夜&#xff0c;那条反反复复走…

vue 实例化几种方式_vue注册组件的几种方式总结

vue注册组件的几种方式总结 1、全局注册(这种方式注册组件必须在vue实例化之前声明) Vue.component(tag-name,{}) 2、局部注册 var Child = {template: A custom component! } new Vue({// ... components: {// 将只在父模板可用 my-component: Child } }) 3、扩展实例 // 定…

gson json和类转换_带有GSON和抽象类的JSON

gson json和类转换经过多年使用org.json库在Java中支持JSON数据交换格式后&#xff0c;我已切换到Google Gson 。 org.json是一个较低层的库&#xff0c;因此您必须创建JSONObject&#xff0c;JSONArray&#xff0c;JSONString等…并执行其他低层工作。 Gson简化了这项工作。 它…

自考18年4月计算机应用基础,2019年自学考试计算机应用基础试题(18)

2019年4月自考报名已经拉开序幕&#xff0c;同学们可以开始准备自考复习&#xff0c;下面是2019年自学考试计算机应用基础试题(18)&#xff0c;了解更多自学考试资讯请锁定自学考试网&#xff0c;更有海量模拟试题&#xff0c;精品复习资料等你来下载!1.Windows 中删除文件夹的…

git中文件的三种状态

用xcode的时候&#xff0c;左侧栏文件的邮右边时不时会看到M&#xff0c;A这一类的字母。当然&#xff0c;这些以后再写上。先说一下git里文件的三种状态 已提交&#xff08;committed&#xff09; 已经提交的本地仓库&#xff08;repository&#xff09;&#xff0c;需要手动…

创建一个带参数的formgoup_gin框架如何获取带文件的formData请求?

**前端代码如下**function upFile() {var formData new FormDatavar file document.getElementById("upload").files[0]var text document.getElementById("text").valueformData.append("upload",file)formData.append("text",tex…

如何从URL查询字符串获得/附加视图参数(示例)?

让我们剖析几个简单的用例&#xff0c;并查看视图参数的工作原理&#xff08;视图参数名称不是强制性的&#xff0c;以匹配通过URL查询字符串传递的请求参数&#xff0c;但在本文中&#xff0c;我们将重点讨论这种情况&#xff09;&#xff1a; 情况1 在index.xhtml页面中&…

桌面怎么设置 计算机 网络,Win10 10130桌面电脑网络图标怎么设置?

越来越多朋友的计算机安装Windows10系统&#xff0c;现在Windows10系统已经升级到10130版&#xff0c;我的计算机升级到了10130版。安装Windows10的10130版系统后&#xff0c;桌面只有回收站和软媒的图标&#xff0c;如何把电脑、网络、用户的文件、控制面板等图标设置到桌面上…

swift 打开第三方应用_iOS卡通人物帧动画入门9(大结局):番外篇-扩展第三方类

扩展第三方类细心的朋友可能会发现&#xff0c;我们前面计算主角的大小用的总是同一方向第一帧纹理的大小&#xff0c;如果纹理大小有出入的话&#xff0c;会产生较大的偏差&#xff0c;最好的方法是取当前动画帧纹理的大小。不过这有些难度&#xff0c;所以我们退之求其次&…

awk说明书(转)

ref&#xff1a;http://blog.chinaunix.net/uid-429659-id-122573.html awk使用手册 作者&#xff1a;awk使用手册什么是awk? 你可能对UNIX比较熟悉&#xff0c;但你可能对awk很陌生&#xff0c;这一点也不奇怪&#xff0c;的确&#xff0c;与其优秀的功能相比&#xff0c;awk…

jpa jdbc jndi_没有J2EE容器的JNDI和JPA

jpa jdbc jndi我们希望通过尽可能简单的设置来测试一些JPA代码。 计划仅使用Java和Maven&#xff0c;不使用应用程序服务器或其他J2EE容器。 我们的JPA配置需要两件事才能成功运行&#xff1a; 数据库来存储数据&#xff0c; JNDI访问数据库。 这篇文章分为两个部分。 第一部…

本科计算机应用水平,湖南工程学院全日制非计算机专业本科生参加湖南省计算机应用水平等级考试的规定...

根据省教育厅《关于印发湖南省普通高等学校非计算机专业学生计算机应用水平等级考试大纲的通知》(湘教发〔2012〕58号)精神&#xff0c;经研究&#xff0c;现对我校全日制非计算机专业本科生参加计算机等级考试有关事项作如下规定&#xff1a;1&#xff0e;凡我校全日制非计算机…

Linux umask限制导致php的mkdir 0777无效

原因:mkdir权限受当前linux umask限制 解决方法: 1 $oldmask umask(0); 2 mkdir("test", 0777); 3 umask($oldmask); 参考SO:http://stackoverflow.com/a/3997671/4009148转载于:https://www.cnblogs.com/gy1010/p/6674243.html

cstringw转lpctstr_新手必看:CString 和 LPCTSTR等之间的转换大全 | 求索阁

CString 和 LPCTSTR 可以说通用。 原因在于CString定义的自动类型转换&#xff0c;没什么奇特的&#xff0c;最简单的C操作符重载而已。常量字符串ansi和unicode的区分是由宏_T来决定的。但是用_T( "abcd ")时&#xff0c; 字符串 "abcd "就会根…