[置顶] CF 86D Powerful array 分块算法入门,n*sqrt(n)
简介:分块算法主要是把区间划分成sqrt(n)块,从而降低暴力的复杂度,
其实这算是一种优化的暴力吧,复杂度O(n*sqrt(n))
题意:给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数。
有t个查询,每个查询l,r,对区间内所有a[i],求sigma(K[a[i]]^2*a[i])
思路:离线+分块处理
分块和离线处理:
将n个数分成sqrt(n)块,设每块有bsize个数, 并且我们计算出每个询问的左端点所在的块号(q[i].b = q[i].l/ bsize)。
对所有询问进行排序:
先按块号排序(块号小的在前),如果块号相等就要右端点排序(右端点小的在前)
解法:每次跟上次的询问区间比较,把多出来的减掉,把少的加上去。 当然第一个询问直接算。
如果一个数已经出现了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次的结果。就是暴力的处理。
复杂度分析:
处理左端点的复杂度:
对于相邻询问左端点的差值不会超过sqrt(n), 所以t个询问的总体复杂度为O(t*sqrt(n))。
处理右端点的复杂度:
对于每个块内的几个查询,因为right是单调递增的,所以极限复杂度为O(n), 而且一共有sqrt(n)个块
所以总体复杂度位O(n*sqrt(n));
因此总的时间复杂度为O(t*sqrt(n) + n*sqrt(n))。
为什么选择sqrt(n)分块:
我们从上面复杂度分析中可以得知, 左右断点的复杂度是独立的,
当块数少了,左边复杂度加大,右边复杂度减少,
反之 当块数多了,左边复杂度减少,右边复杂度加大,
块数选择sqrt(n)是为了总体复杂度的最小。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 200005;
typedef long long LL;
LL a[maxn], cnt[maxn * 5], ans[maxn], res;
int L, R; struct node {
int l, r, b, id;
bool operator <(const node &t) const {
if (b == t.b)
return r < t.r;
return b < t.b;
}
} q[maxn]; LL 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;
return res;
}
int n, t;
int main() {
int i; scanf("%d%d", &n, &t);
for (i = 1; i <= n; i++)
scanf("%I64d", &a[i]);
int bsize = sqrt(n + 0.5); for (i = 0; i < t; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].b = q[i].l / bsize;
q[i].id = i;
} sort(q, q + t);
memset(cnt, 0, sizeof(cnt));
res = 0;
for (i = 0; i < t; i++)
ans[q[i].id] = query(q[i].l, q[i].r, i); for (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)】
给定一个数列:A1, A2,……,An,定义Ks为区间(l,r)中s出现的次数. t个查询,每个查询l,r,对区间内所有a[i],求sigma(K^2*a[i]) 离线+分块 将n个数分成sqrt(n ...
- 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 ...
- [置顶] hdu2815 扩展Baby step,Giant step入门
题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...
- 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在该区间内的出现次数,求 ...
随机推荐
- iOS 项目中的NSNotification简单使用
iOS中NSNotification的简单使用 好久没有写过博客了,总是遇到问题查一下,今天查的又是一个老问题,想了想,还是记录一下!今天在项目开发中遇到一个配置及时性处理的问题,想了想之后决定用通知 ...
- Smarterer Test
1.Using xp can swap two characters 2.In normal mode, use /abc/+4 can find the string "abc" ...
- mysql性能调优与架构设计(一)商业需求与系统架构对性能的影响
这里我们就拿一个看上去很简单的功能来分析一下. 需求:一个论坛帖子总量的统计附加要求:实时更新 在很多人看来,这个功能非常容易实现,不就是执行一条SELECT COUNT(*)的Query 就可以得到 ...
- dialog组件的jquery封装实现
(function($){ $.extend({ Dialog : function(id, options){ var option = $.extend({}, options); option. ...
- HTML5 按字母顺序排列的标签列表 new : HTML5 中的新标签。
标签 描述 <!--...--> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义超链接. <abbr> 定义缩写. <acron ...
- poj 1204
http://poj.org/problem?id=1204 大意:给出一个棋盘puzzle,,和w个模式串,,在棋盘中寻找模式串..棋盘中任意一格有8个方向可以走.. 解题思路: AC自动机 + 枚 ...
- notify()、notifyAll()和wait()
看到一道面试题,写一个多线程程序,交替输出1.2.1.2…… 先写下程序: /** * Created by Andrew on 2015/10/28. */ public class OutputT ...
- eclipse @ 注释为何一写就报错
以前一直奇怪,为什么eclipse自动生成的的代码中的@注释不会报错,而我直接写@就会报错 原因其实很简单: eclipse会检查@注释的位置 举个例子:写@Override,直接写会报错,但如果你继 ...
- Unity3D游戏开发之小地图的实现
今天我们来讲一下在Unity3D中怎样实现小地图.首先,我们来一起看看终于实现的效果: 要实现小地图效果,须要实现对于小地图和角色的绘制,所以这里须要用到OnGUI()方法,我们一起来看代码: [cs ...
- SEOR要懂得如何建立完善的seo运营团队
网站运营要想能做大做全面,完善的seo运营团队是不可缺少的,跟企业管理一样的道理,seo运营的成功也在于对团队的合理利用,发挥团队中每个成员的优势才能运营好网站.网站的运营CEO要懂得如何建立完善的s ...