热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Codeforces86DPowerfularray(莫队)

题目链接:http:codeforces.comproblemsetproblem86D题目:Anarrayofpositiveintegers a1,?a2,?,?an i

题目链接:http://codeforces.com/problemset/problem/86/D

题目:

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
Copy
3 2
1 2 1
1 2
1 3
output
3
6
input
Copy
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 3^2·1 + 2^2·2 + 1^2·3 = 20.

题意:给出一个由n个正整数形成的数组,t次询问,每次询问一个区间[l,r]内所有 K^2*a的和,K为数a在区间内出现的次数。

题解:更改下add和del即可。add的话(k+1)^2-k^2=2k+1,增加2k+1个a[i];del的话k^2-(k-1)^2=2k-1,减少2k-1个a[i]。

 1 #include 
 2 #include 
 3 #include 
 4 using namespace std;
 5 
 6 typedef long long LL;
 7 const int N=1e6+10;
 8 struct node{
 9     int l,r,id;
10 }Q[N];
11 
12 LL a[N],ans[N],cnt[N];
13 int BLOCK;
14 bool cmp(node x,node y){
15     if(x.l/BLOCK==y.l/BLOCK) return x.r<y.r;
16     return x.l/BLOCKBLOCK;
17 }
18 
19 int n,m;
20 LL Ans=0;
21 
22 void add(int x){
23     Ans+=a[x]*(cnt[a[x]]*2+1);
24     cnt[a[x]]++;
25 }
26 
27 void del(int x){
28     Ans-=a[x]*(cnt[a[x]]*2-1);
29     cnt[a[x]]--;
30 }
31 
32 int main(){
33     scanf("%d%d",&n,&m);
34     BLOCK=sqrt(n);
35     for(int i=1;i<=n;i++){
36         scanf("%lld",&a[i]);
37     }
38     for(int i=1;i<=m;i++){
39         scanf("%d%d",&Q[i].l,&Q[i].r);
40         Q[i].id=i;
41     }
42     sort(Q+1,Q+1+m,cmp);
43     int L=1,R=0;
44     for(int i=1;i<=m;i++){
45         while(L<Q[i].l){
46             del(L);
47             L++;
48         }
49         while(L>Q[i].l){
50             L--;
51             add(L);
52         }
53         while(R<Q[i].r){
54             R++;
55             add(R);
56         }
57         while(R>Q[i].r){
58             del(R);
59             R--;
60         }
61         ans[Q[i].id]=Ans;
62     }
63     for(int i=1;i<=m;i++)
64     printf("%lld\n",ans[i]);
65     return 0;
66 }

Codeforces 86D Powerful array(莫队)


推荐阅读
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
  • 本文介绍如何在应用程序中使用文本输入框创建密码输入框,并通过设置掩码来隐藏用户输入的内容。我们将详细解释代码实现,并提供专业的补充说明。 ... [详细]
  • 本文介绍如何通过SQL查询从JDE(JD Edwards)系统中提取所有字典数据,涵盖关键表的关联和字段选择。具体包括F0004和F0005系列表的数据提取方法。 ... [详细]
  • 本文详细介绍了如何通过命令行启动MySQL服务,包括打开命令提示符窗口、进入MySQL的bin目录、输入正确的连接命令以及注意事项。文中还提供了更多相关命令的资源链接。 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
author-avatar
520sweet跃_322
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有