CF 86D 莫队(卡常数)

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
  1. 3 2
    1 2 1
    1 2
    1 3
output
  1. 3
    6
input
  1. 8 3
    1 1 2 2 1 3 1 1
    2 7
    1 6
    2 7
output
  1. 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.

题意: 
一个数列,问[L,R]区间内(每个数字的个数的平方*数字的大小)的和。

思路: 
莫队模板。

离线+分块

将n个数分成sqrt(n)块。

对所有询问进行排序,排序标准:

1. Q[i].left /block_size < Q[j].left / block_size (块号优先排序)

2. 如果1相同,则 Q[i].right < Q[j].right (按照查询的右边界排序)

问题求解:

从上一个查询后的结果推出当前查询的结果。(这个看程序中query的部分)

如果一个数已经出现了x次,那么需要累加(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i],x^2*a[i]是出现x次的结果,(x+1)^2 * a[i]是出现x+1次的结果。

时间复杂度分析:

排完序后,对于相邻的两个查询,left值之间的差最大为sqrt(n),则相邻两个查询左端点移动的次数<=sqrt(n),总共有t个查询,则复杂度为O(t*sqrt(n))。

又对于相同块内的查询,right端点单调上升,每一块所有操作,右端点最多移动O(n)次,总块数位sqrt(n),则复杂度为O(sqrt(n)*n)。

right和left的复杂度是独立的,因此总的时间复杂度为O(t*sqrt(n)  +  n*sqrt(n))。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cmath>
  6. using namespace std;
  7. #define N 200100
  8. typedef long long ll;
  9. ll a[N], cnt[N*], ans[N], res;
  10. int L, R;
  11.  
  12. struct node {
  13. int x, y, l, p;
  14. } q[N];
  15. bool cmp(const node &x, const node &y) {
  16. if (x.l == y.l) return x.y < y.y;
  17. return x.l < y.l;
  18. }
  19. void query(int x, int y, int flag) {
  20. if (flag) {
  21. for (int i=x; i<L; i++) {
  22. res += ((cnt[a[i]]<<)+)*a[i];
  23. cnt[a[i]]++;
  24. }
  25. for (int i=L; i<x; i++) {
  26. cnt[a[i]]--;
  27. res -= ((cnt[a[i]]<<)+)*a[i];
  28. }
  29. for (int i=y+; i<=R; i++) {
  30. cnt[a[i]]--;
  31. res -= ((cnt[a[i]]<<)+)*a[i];
  32. }
  33. for (int i=R+; i<=y; i++) {
  34. res += ((cnt[a[i]]<<)+)*a[i];
  35. cnt[a[i]]++;
  36. }
  37.  
  38. } else {
  39. for (int i=x; i<=y; i++) {
  40. res += ((cnt[a[i]]<<)+)*a[i];
  41. cnt[a[i]]++;
  42. }
  43. }
  44. L = x, R = y;
  45. }
  46. int main() {
  47. int n, t;
  48.  
  49. scanf("%d%d", &n, &t);
  50. for (int i=; i<=n; i++) scanf("%I64d", &a[i]);
  51. int block_size = sqrt(n);
  52.  
  53. for (int i=; i<t; i++) {
  54. scanf("%d%d", &q[i].x, &q[i].y);
  55. q[i].l = q[i].x / block_size;
  56. q[i].p = i;
  57. }
  58.  
  59. sort(q, q+t, cmp);
  60.  
  61. memset(cnt, , sizeof(cnt));
  62.  
  63. res = ;
  64. for (int i=; i<t; i++) {
  65. query(q[i].x, q[i].y, i);
  66. ans[q[i].p] = res;
  67. }
  68.  
  69. for (int i=; i<t; i++) printf("%I64d\n", ans[i]);
  70.  
  71. return ;
  72. }

CF 86D 莫队(卡常数)的更多相关文章

  1. BZOJ1878 [SDOI2009] HH的项链 [莫队,卡常]

    BZOJ传送门,洛谷传送门 HH的项链 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义. ...

  2. 清橙A1206.小Z的袜子 && CF 86D(莫队两题)

    清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...

  3. [luogu1972][bzoj1878][SDOI2009]HH的项链【莫队+玄学卡常】

    题目大意 静态区间查询不同数的个数. 分析 好了,成功被这道题目拉低了AC率... 打了莫队T飞掉了,真的是飞掉了QwQ. 蒟蒻想不出主席树的做法,就换成了莫队... 很多人都不知道莫队是什么... ...

  4. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  5. CF 617E【莫队求区间异或和】

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. Codeforces 86D Powerful array (莫队)

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

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

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

  8. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  9. 【Cf #502 H】The Films(莫队)

    题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...

随机推荐

  1. JS——隐式全局变量

    在函数中,var声明的是局部变量,不带var的是隐式全局变量 <script> function fn() { var a = b = c = 0;//a是局部变量,b.c是全局变量 va ...

  2. 遍历select搜索结果,只取数字标key值,防止重复

    //遍历select搜索结果,只取数字标key值,防止重复 foreach ($row as $key => $value) { if (is_int($key)) { echo $value; ...

  3. Node.js概述

    Node.js最重要的特性:通过单线程实现异步处理环境 Node.js解决的问题: Node.js修改了客户端连接服务器端的连接方法,不需要为每个客户端连接创建一个新的线程,而是为每个客户端连接触发一 ...

  4. 新人转型学习C#

    毕业3年,终于在4个多月前,下定决心辞职了.一直以来都想从事软件开发的工作,也觉得自己更加适合这方面的工作.自己如果这一次还是没能往这方面发展的话,感觉以后也不会有机会了. 于是,想着工作先不找了,买 ...

  5. 项目关联到svn

    最近因为要升级项目,改用64位的eclipse,原先已经被svn管理的项目需要被复制到另一个工作空间,只需要导入并复制项目到新的工作空间即可 右键,点import 选择已经存在的项目导入工作空间 选择 ...

  6. img、a标签的使用

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  7. python实战教程之自动扫雷

    1.找到游戏窗口与坐标 #扫雷游戏窗口class_name = "TMain"title_name = "Minesweeper Arbiter "hwnd = ...

  8. Android—修改button属性

    一般安卓里的普通按钮控件灰灰的,比较单调,我们可以给按钮加上背景图片,或者自定义按钮的圆角,颜色等属性. 下面用代码举例: <Button android:id="@+id/reset ...

  9. Lua之尾调函数的用法

    Lua之尾调函数的用法 --当函数的最后返回结果调用另一个函数,称之为尾调函数 function f(x) return g(x) end --由于“尾调用”不会耗费栈空间,所以一个程序可以拥有无数嵌 ...

  10. list.h在用户态下的应用

    一.背景 list.h文件位于linux/include下,内核中链表的操作函数都在其中.它有许多关于链表的操作函数,所以我们可以尝试将list.h拉到用户态中来使用,这样,我们在用户态中若要用到链表 ...