CF 86D 莫队(卡常数)
CF 86D 莫队(卡常数)
5 seconds
256 megabytes
standard input
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.
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 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).
3 2
1 2 1
1 2
1 3
3
6
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
20
20
20
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 莫队(卡常数)的更多相关文章
- BZOJ1878 [SDOI2009] HH的项链 [莫队,卡常]
BZOJ传送门,洛谷传送门 HH的项链 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义. ...
- 清橙A1206.小Z的袜子 && CF 86D(莫队两题)
清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...
- [luogu1972][bzoj1878][SDOI2009]HH的项链【莫队+玄学卡常】
题目大意 静态区间查询不同数的个数. 分析 好了,成功被这道题目拉低了AC率... 打了莫队T飞掉了,真的是飞掉了QwQ. 蒟蒻想不出主席树的做法,就换成了莫队... 很多人都不知道莫队是什么... ...
- 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, ...
- CF 617E【莫队求区间异或和】
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- 【Cf #502 H】The Films(莫队)
题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...
随机推荐
- Android studio 中R.menu的创建
对于Android开发中的menu没有声明的情况: 首先,将鼠标定位到红色的menu上面, 然后,Alt+enter组合键,建立文件menu, 然后将以下代码复制进去: <item androi ...
- CSS——img
img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.
- 【转载】HTTP 请求头与请求体
原文地址: https://segmentfault.com/a/1190000006689767 HTTP Request HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典 ...
- 9-4前端vue面试的问题
就没有什么顺序了,肖师傅的一些提问: 1- 配置文件中proxyTable的作用 2-@import '~styles/mixins.styl' ~的作用 3-vue模拟的本地中访问地址的url带有 ...
- Vue中this.$router.push参数获取(通过路由传参)【路由跳转的方法】
传递参数的方法: 1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用nam ...
- 云计算时代,你为什么一定要学Linux?
云计算早已不是什么稀奇的概念,它的火爆让Linux运维工程师这个职业越来越重要.在当今各类云平台提供的系统中,Linux系统几乎毫无争议的独占鳌头,市场份额进一步扩张. 这也让Linux运维工程师职位 ...
- hdu 4018 Parsing URL(字符串截取)
题目 以下引用自百度百科: sscanf 的相关用法 头文件:#include<stdio.h> 1. 常见用法. 1 2 3 charbuf[512]; sscanf(" ...
- web前端学习总结--JQuery
jQuery 什么是jQuery jQuery是一个优秀的JavaScript框架,一个轻量级的JS库. 它封装了JS.CSS.DOM,提供了一致的.简洁的API. 兼容CSS3,及各种浏览器 使用户 ...
- PHP websocket之聊天室实现
PHP部分 <?php error_reporting(E_ALL); set_time_limit(0);// 设置超时时间为无限,防止超时 date_default_timezone_set ...
- elasticsearch match和match_phrase匹配原则(十)
分词测试 分词后倒排索引结果:可以通过http://127.0.0.1:9200/_analyze 测试 { "analyzer":"ik_max_word", ...