题目描述:

Problem Description

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 l, r (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.

思路:

题目是要求给定一个区间,分别统计出区间不同数字的个数,对每个不同的数,求出现次数的平方与该数字的乘积的和。

因为给了好几组区间,很容易想到是一种直接暴力的算法,每个询问都统计一次然后算出一个ans来,但很显然这个算法会超时,因为对于不同的询问区间可能会有一部分重合的以求得的信息,每次暴力计算并没有利用这些已知信息。那么如何使用呢?想到既然是区间问题那不如用线段树来维护一个区间,但是,对于蒟蒻来说,我会做的线段树只能维护一些简单的信息,不会维护这种复杂的统计信息(我还是太菜了),怎么办呢?要知道,维护区间信息问题还有一种重要且巧妙的方法——莫队算法。

哈哈哈,知道了这个算法,虽然也是暴力算法,但是它通过分块+排序预处理能把复杂度降到O(n*sqrt(n)),就可以做了。注意在处理ans是改动一下就好

小心的是,如果用的分组方法是这种:就千万小心把belong数组开大一点,(两倍就行),因为它是刚好分成size*bulk,这么多元素,如果测试点是2e5(对这道题而言),那么最后belong原来的空间用完后会继续往下面的位置赋值,导致越界。(我在这卡了好久qwq)。还有cnt统计个数数组不能开long long。

  1. for(int i = ;i<=bulk;i++)
  2. {
  3. for(int j = (i-)*size+;j<=i*size;j++)
  4. {
  5. belong[j] = i;
  6. }
  7. }

(bulk是块数,size是每块的大小)

当然,下面这种分组方式就不存在问题,因为他刚好分n个元素。

  1. for(int i = ;i<=n;i++)
  2. scanf("%d",&col[i]),Be[i]=i/unit+;

卡常技巧:

cmp函数改为莫队玄学奇偶性排序(代码中的cmp2),实际上可以帮你每个点平均优化200ms(可怕)

如果允许,吸氧也是极好的#pragma GCC optimize(2)

优化结果:

第一个为什么都不做(超时)

第二个为改cmp

第三个为改cmp+开optimize(2)

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #define max_n 200005
  5. using namespace std;
  6. int a[max_n];
  7. int cnt[];
  8. int belong[max_n*];
  9. int bulk;
  10. int size;
  11. long long ans = ;
  12. long long sum[max_n];
  13. int n;
  14. int m;
  15. struct node
  16. {
  17. int r;
  18. int l;
  19. int id;
  20. }q[max_n];
  21. int cmp(node a,node b)
  22. {
  23. return (belong[a.l]==belong[b.l])?a.r<b.r:a.l<b.l;
  24. }
  25. int cmp2(node a,node b)
  26. {
  27. return (belong[a.l]^belong[b.l])?(a.l<b.l):(belong[a.l]&)?a.r<b.r:a.r>b.r;
  28. }
  29.  
  30. void add(int pos)
  31. {
  32. ans -= (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
  33. /*if(cnt[a[pos]]==0)
  34. {
  35. ans++;
  36. }*/
  37. cnt[a[pos]]++;
  38. ans += (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
  39. }
  40. void del(int pos)
  41. {
  42. ans -= (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
  43. cnt[a[pos]]--;
  44. /*if(cnt[a[pos]]==0)
  45. {
  46. ans--;
  47. }*/
  48. ans += (long long)a[pos]*cnt[a[pos]]*cnt[a[pos]];
  49. }
  50. #pragma GCC optimize(2)
  51. int main()
  52. {
  53. cin >> n >> m;
  54. size = sqrt((double)n);
  55. bulk = ceil((double)n/size);
  56. for(int i = ;i<=bulk;i++)
  57. {
  58. for(int j = (i-)*size;j<=i*size;j++)
  59. {
  60. belong[j] = i;
  61. }
  62. }
  63. for(int i = ;i<=n;i++)
  64. {
  65. cin >> a[i];
  66. }
  67.  
  68. for(int i = ;i<=m;i++)
  69. {
  70. cin >> q[i].l >> q[i].r;
  71. q[i].id = i;
  72. }
  73. sort(q+,q+m+,cmp2);
  74. /*for(int i = 0;i<n;i++)
  75. {
  76. cout << belong[i] << " ";
  77. }
  78. cout << endl;*/
  79. int l = ;
  80. int r = ;
  81. for(int i = ;i<=m;i++)
  82. {
  83. int nl = q[i].l;
  84. int nr = q[i].r;
  85. while(l>nl) add(--l);
  86. while(r<nr) add(++r);
  87. while(l<nl) del(l++);
  88. while(r>nr) del(r--);
  89. //cout << q[i].id << endl;
  90. sum[q[i].id] = ans;
  91. }
  92. for(int i = ;i<=m;i++)
  93. {
  94. cout << sum[i] << endl;
  95. }
  96. return ;
  97. }

参考文章:

hzwer,「分块」数列分块入门1 – 9 by hzwer,http://hzwer.com/8053.html(分块算法)

WAMonster,莫队算法——从入门到黑题,https://www.cnblogs.com/WAMonster/p/10118934.html(莫队算法良心讲解)

大米饼,莫队算法,https://www.cnblogs.com/Paul-Guderian/p/6933799.html(莫队算法清晰简明讲解)

(两篇结合互补食用效果为佳)

Codeforces D. Powerful array(莫队)的更多相关文章

  1. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  2. CodeForces - 86D Powerful array (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  3. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

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

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

  5. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

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

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

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

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

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

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

  9. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

  10. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

随机推荐

  1. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  2. hexo 博客如何更换电脑

    如何在更换电脑后继续使用Hexo部署博客 重要目录 _config.yml package.json scaffolds/ source/ themes/ 在新电脑上配置hexo环境:安装node.j ...

  3. javadoc 抽出代码中的注释

    背景: 在已经有的项目里面利用javadoc来抽出代码中每个类,每个方法的注释部分,形成一个文档. 方法1 利用eslipse来实现 之后会在指定的目录下生成html文档 方法2 利用命令来执行 先把 ...

  4. mysql高并发配置

    mysql高并发配置 要在mysqld下设置1 修改back_log参数值:由默认的50修改为500.(每个连接256kb,占用:125M)back_log=500<pre> back_l ...

  5. Redhat7.6Linux本地的yum源配置

    安装好虚拟机,配置Ip地址,见博客https://www.cnblogs.com/xuzhaoyang/p/11264573.html 我是使用的Xshell在外部链接进行的操作,比较简单,见博客ht ...

  6. [Oracle] Io Error: The Network Adapter could not establish the connection 解决方案

    Io 异常: The Network Adapter could not establish the connection这个异常的出现一般与数据库和你的PC的设置有关 这种异常的出现大致上有下面几种 ...

  7. RDP Error: The Identity Of The Remote Computer Cannot Be Verified

    As i always need to remote to 20 servers at the same time, so i use a tool called Remote Desktop Con ...

  8. WPF内嵌网页的两种方式

    在wpf程序中,有时会内嵌网页.内嵌网页有两种方法,一种是使用wpf自带WebBrowser控件来调用IE内核,另一种是使用CefSharp包来调用chrom内核. 一.第一种使用自带WebBrows ...

  9. Linux用户组笔记整理

    一.Linux用户组概念 Linux用户组(group)就是具有相同操作权限范围的Linux用户管理起来: 比如有时我们要让同一类用户具有相同的权限,比如查看.修改某一文件或执行某个命令, 这时我们需 ...

  10. 并发编程之Java锁

    一.重入锁 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized(重量级) 和 ReentrantLock(轻量级)等等 ) .这些已经写好提供的锁为我们开发提 ...