CF 86D Powerful array 【分块算法,n*sqrt(n)】
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数。
t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i])
离线+分块
将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*5], 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]]<<1)+1)*a[i];
cnt[a[i]]++;
}
for (int i=L; i<x; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=y+1; i<=R; i++) {
cnt[a[i]]--;
res -= ((cnt[a[i]]<<1)+1)*a[i];
}
for (int i=R+1; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
} } else {
for (int i=x; i<=y; i++) {
res += ((cnt[a[i]]<<1)+1)*a[i];
cnt[a[i]]++;
}
}
L = x, R = y;
}
int main() {
int n, t; scanf("%d%d", &n, &t);
for (int i=1; i<=n; i++) scanf("%I64d", &a[i]);
int block_size = sqrt(n); for (int i=0; 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, 0, sizeof(cnt)); res = 0;
for (int i=0; i<t; i++) {
query(q[i].x, q[i].y, i);
ans[q[i].p] = res;
} for (int i=0; i<t; i++) printf("%I64d\n", ans[i]); return 0;
}
CF 86D Powerful array 【分块算法,n*sqrt(n)】的更多相关文章
- [置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)
简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度, 其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n)) 题意:给定一个数列:a[i] (1<= i <= ...
- CF 86D Powerful array
离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号 ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=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 ...
- Codeforces#86D Powerful array(分块暴力)
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- Codeforces 86D Powerful array(莫队算法)
和BZOJ2038差不多..复习一下. #include<cstdio> #include<cmath> #include<algorithm> using nam ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
随机推荐
- BZOJ 1827 奶牛大集会
树型DP. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- [转载] ubuntu Authentication failure
ubuntu的root用户默认是禁止的,需要手动打开才行.事实上ubuntu下的所有操作都用不到root用户,由于sudo的合理使用,避免了root用户下误操作而产生的毁灭性问题.root账号启用方法 ...
- Java中SynchronizedMap与ConcurrentHashMap的对比
如何使用 概述 ConcurrentHashMap: 线程安全: 其将整个Hash桶进行了分段segment,也就是将这个大的数组分成了几个小的片段segment,而且每个小的片段segment上面都 ...
- org.hibernate.MappingException: duplicate import异常
在开发hibernate时,一起多谢ORM类和映射文件时,报出:org.hibernate.MappingException: duplicate import com.XXX异常 解决方案: 检查你 ...
- linux apache 配置fastcgi
Redhat 上 FastCGI 安装与配置 软件包 相关软件包: httpd 2.2.14 //注意版本 这个版本不会出问题 注:apache httpd安装 fcgi-2.4.0.t ...
- js的预解析和代码执行相关规则
JavaScript解析过程分为两个阶段,一个是编译阶段,另外一个就是执行阶段. 1.编译阶段:又称为预解析阶段,在这个阶段JavaScript解释器将完成把JavaScript脚本代码转换到字节码. ...
- EFSQLserver
1.增加一条烽据 FLYNEWQKEntities dataContext = new FLYNEWQKEntities(); Log log = new Log(); log.Data1 = &qu ...
- dword word byte 相互转换 .xml
pre{ line-height:1; color:#800080; background-color:#d2c39b; font-size:16px;}.sysFunc{color:#627cf6; ...
- ISO-8859-1
ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号 ...
- bzoj3064 CPU监控
今天终于写了一道正常的题 思路是这样的: 1.普通线段树add,set不变,并改为下放标记版本 2.past_addv 记录一个区间内可能的addv值的最大值 3.past_setv 记录一个区间被s ...