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

题意: 
一个数列,问[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))。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 200100
typedef long long ll;
ll a[N], cnt[N*], ans[N], res;
int L, R; struct node {
int x, y, l, p;
} q[N];
bool cmp(const node &x, const node &y) {
if (x.l == y.l) return x.y < y.y;
return x.l < y.l;
}
void query(int x, int y, int flag) {
if (flag) {
for (int i=x; i<L; i++) {
res += ((cnt[a[i]]<<)+)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<)+)*a[i];
}
for (int i=y+; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<)+)*a[i];
}
for (int i=R+; i<=y; i++) {
res += ((cnt[a[i]]<<)+)*a[i];
cnt[a[i]]++;
} } else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<)+)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t; scanf("%d%d", &n, &t);
for (int i=; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n); for (int i=; i<t; i++) {
scanf("%d%d", &q[i].x, &q[i].y);
q[i].l = q[i].x / block_size;
q[i].p = i;
} sort(q, q+t, cmp); memset(cnt, , sizeof(cnt)); res = ;
for (int i=; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
} for (int i=; i<t; i++) printf("%I64d\n", ans[i]); return ;
}

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. SAS学习笔记之《SAS编程与数据挖掘商业案例》(2)数据获取与数据集操作

    SAS学习笔记之<SAS编程与数据挖掘商业案例>(2)数据获取与数据集操作 1. SET/SET效率高,建立的主表和建表索引的查询表一般不排序, 2. BY语句,DATA步中,BY语句规定 ...

  2. Android java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230

    近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...

  3. android黑科技系列——解析公众号文章消息和链接文章消息自动打开原理

    一.辅助功能方案分析 关于WX的各种功能插件已经非常普遍了,而现在的插件都是依赖于Xposed框架进行的,所以个人觉得WX应该在这方便应对Xposed框架的使用防护,防止插件满天飞的现象,本文来介绍一 ...

  4. Python基础——列表、元组操作

    列表.元组操作 列表: 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0 ...

  5. postfix 邮件中继配置

    Postfix 配置邮件中继 A 邮件发送服务器B 邮件中继服务器 A. 配置发件服务器 # 开启转发规则 [root@Postfix ~]# vi /etc/postfix/main.cf tran ...

  6. (转)Hibernate快速入门

    http://blog.csdn.net/yerenyuan_pku/article/details/64209343 Hibernate框架介绍 什么是Hibernate 我们可以从度娘上摘抄这样有 ...

  7. UIResponder详解

    UIResponder Class Reference Managing the Responder Chain 1.- (UIResponder *)nextResponder 返回接收者的下一个相 ...

  8. Java切换JDK版本的方法及技巧

    由于项目的不同安排,之前项目开发时,使用的jdk版本为1.8,现临时接手一以前项目,需要更换jdk版本. 安装 不再赘述,去Oracle网站(https://www.oracle.com/techne ...

  9. resize监听div的size变化

    具体实现分两类, ie9-10 默认支持div的resize事件,可以直接通过div.attachEvent('onresize', handler);的方式实现 其它浏览器 通过在div中添加一个内 ...

  10. 使用redis和简单token机制校验身份的思路

    1. 登录时生成token, 以token为键,以用户信息为值,存储在redis中,设置key过期时间 2. 需要身份验证的接口,带上token 3. 接口校验redis中token是否存在 4. 存 ...