【OI】简单的分块
介绍下简单的分块:
当我们遇到区间类问题的时候,如何保证我们快速而高效地完成操作?
答案是线段树分块。
所谓分块,就是把一个序列分成许多块分别维护。是不是想起了树状数组 这样能大大提高效率:
例如,我们需要查询l,r中所有元素的和
利用分块,我们可以把1 2 3 4 5 6 7 8 9 10
分为
[1 2 3] [4 5 6] [7 8 9] [10]
比如,我想要3~10的元素和。这是,我们拿出3~10的区间:
...[3] [4 5 6] [7 8 9] [10]
而我们已经预处理了 [4 5 6]与 [7 8 9]的区间和,我们只需要算出两端的和就可以。这样,就可以大大提高查询的速度。
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int MaxN = 100010, MaxB = 1010; int n, B, q, Cnt;
int a[MaxN], Left[MaxB], Right[MaxB], Pos[MaxN];
long long sum[MaxN]; int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
// 分块预处理
B = max((int)sqrt(n), 1);
for (int i = 1; i <= n; i++)
{
if (i % B == 1 % B) Cnt++; // 每块块首 Cnt++
Pos[i] = Cnt; // i 处在哪一块
if (Left[Cnt] == 0) Left[Cnt] = i; // 第Cnt块的左边
Right[Cnt] = i; // 第Cnt块的右边
} // for (int i = 1; i <= n; i++) printf("%d ", Pos[i]); puts("");
// for (int i = 1; i <= Cnt; i++) printf("%d %d\n", Left[i], Right[i]); for (int i = 1; i <= Cnt; i++)
for (int j = Left[i]; j <= Right[i]; j++)
sum[i] += a[j]; // sum[i] 表示的是第i块的和 // 处理询问
scanf("%d", &q);
// a[l] + a[l + 1] + ... + a[r] 变成 sum[r] - sum[l - 1]
// 这样我们可以只用求 l=1 的情况
// 但是我现在不这样做
for (int i = 1; i <= q; i++)
{
int l, r;
scanf("%d %d", &l, &r);
long long ans = 0;
if (Pos[l] == Pos[r])
{
// 1 l,r 在同一个块内
// [ l r ]
for (int j = l; j <= r; j++) ans += a[j];
}
else
{
// 2 l,r 不在同一个块内
// [ l ] [ ] [ ] [r ]
for (int j = l; j <= Right[Pos[l]]; j++) ans += a[j];
for (int j = Right[Pos[l]] + 1; j <= Left[Pos[r]] - 1; j++) ans += sum[j];
for (int j = Left[Pos[r]]; j <= r; j++) ans += a[j];
}
printf("%lld\n", ans);
}
return 0;
}
(不是我写的,但是的确十分简洁易懂对吧)
例题:luogu P3203,LCT板子题可是可以用分块来过
#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<map>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
typedef long long ll;
const ll INF=;
const int MaxN = ;
int a[MaxN];
struct nd{
int t,to;//弹几次出此块
int from;//属于第几个块 }t[MaxN];
int n,m,I,J;
int sqr,cnt = -;
int Left[MaxN],Right[MaxN]; void findT(int x,int right,int &t,int &to){
int re = ;
int i = x;
for(;i <= right;){
int ad = a[i];
i += ad;
re++; }
t = re;
to = i;
//printf("需要%d步跳出,跳到%d\n",t,to); } inline int Max(int a,int b){
if(a>b) return a;
return b; } int main()
{
//freopen("testdata.in","r",stdin);
//freopen("testdata.out","w",stdout);
scanf("%d",&n);
for(int i = ;i < n; i++){
scanf("%d",&a[i]); } sqr = Max(sqrt(n),); /*for(int i = 0;i < n; i++){
if(i%sqr == 0%sqr) {
cnt++;
Left[cnt-1] = i;
//printf("第%d个块左边是%d\n",cnt-1,i);
}
Right[cnt-1] = i;
t[i].from = cnt-1;
//printf("右边界:%d\n",sqr*cnt-1);
findT(i,sqr*cnt-1,t[i].t,t[i].to); }*/ for(int i = ;i < n; i++){
if(i%sqr == %sqr){
Left[++cnt] = i; }
t[i].from = cnt;
Right[cnt] = i;
} for(int i = n-;i >= ;i--){
t[i].to = i+a[i];
if(t[i].to >= Right[t[i].from]) t[i].t = ;
else t[i].t=t[t[i].to].t+,t[i].to=t[t[i].to].to;
//printf("%d\n",t[i].to);
}
/*
for(int i = 0;i < n; i++){
printf("第%d个元素:跳%d次跳出此块(%d~%d)到%d,属于第%d个块\n",i,t[i].t,Left[t[i].from],Right[t[i].from],t[i].to,t[i].from);
}*/
scanf("%d",&m);
for(int i = ;i < m; i++){
scanf("%d%d",&I,&J);
int k;
if(I == ){ scanf("%d",&k);
a[J] = k;
for(int j = Right[t[J].from];j >= Left[t[J].from]; j--){
t[j].to = j+a[j];
if(t[j].to >= Right[t[J].from]) t[j].t = ;
else t[j].t=t[t[j].to].t+,t[j].to=t[t[j].to].to; }
}
else{
int ans = ;
while(J < n){
ans += t[J].t;
J = t[J].to; }
printf("%d\n",ans); } } return ;
}
P3203
利用分块思想,每一个元素维护跳几次跳出这个块以及跳到哪里。
这样,就可以大大提高查询的速度,但是问题在于如何O(n)的预处理。
预处理:从后往前枚举,就好像穿起一条链。比如:
设t[i]为需要跳t[i]次跳出某个块,to[i]为跳出某个块到to[i]。
假设这个序列为:
... 1 1
第n个元素,处于第x块,则需要1次跳出块到n+a[i]。(t[i] = 1,to[i] = 4)
第n-1个元素,处于第x块(假设处于同一个块),则需要2次跳出块到4。(t[i] = t[to[i]]+1 = 2,to[i] = to[to[i]],相信不难理解)
以此类推。
这样,只要在修改的时候对于修改元素所在的块进行我们写好的预处理。
【OI】简单的分块的更多相关文章
- HYSBZ 2957 分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 题意:中文题面 思路: 来自此博客 首先明确问题,对于每栋楼房的斜率K=H/X,问题 ...
- [BZOJ 2957]楼房重建(THU2013集训)(分块思想)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 首先明确问题,对于每栋楼房的斜率K=H/X,问题就是问有多少个楼房的K比前面所有 ...
- P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?LCT?...FAQ orz
好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...
- P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?
好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...
- 分块基础练习 UESTC 1324
http://acm.uestc.edu.cn/#/problem/show/1324 思路:基础分块,这个是一个特别简单的分块,就当做是一个练习了.然后这题也是很简单的单点线段树更新. //看看会不 ...
- [luogu3203 HNOI2010] 弹飞绵羊 (分块)
传送门 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- NLP(六) 分块、句法分析、依存分析
内置分块器 分块:从文本中抽取短语 import nltk text = 'Lalbagh Botanical Garden is a well known botanical garden in B ...
- Codechef TRIPS Children Trips (分块、倍增)
题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...
- HTTP 协议中的 Content-Encoding 和 Transfer-Encoding(内容编码和传输编码)
转自:http://network.51cto.com/art/201509/491335.htm Transfer-Encoding,是一个 HTTP 头部字段,字面意思是「传输编码」.实际上,HT ...
随机推荐
- qrcode.js
(function(r){r.fn.qrcode=function(h){var s;function u(a){this.mode=s;this.data=a}function o(a,c){thi ...
- fastclick.js插件使用
引入插件步骤 ①在HTML页面中添加 <script type='application/javascript' src='/path/to/fastclick.js'></scr ...
- centos6基础优化
一.关闭SELinux功能 selinux功能太严苛,还是关闭了吧 法一:修改配置文件,永久生效 [root@web01 ~]# sed -i 's/SELINUX=enforcing/SELINUX ...
- LeetCode(55)Jump Game
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- Spring之HelloWorld
[Spring是什么?] 1.Spring是一个开源框架. 2.Spring为简化企业级应用开发而生,使用Spring可以使简单的JavaBean实现以前只有EJB(EJB是sun的JavaEE服务器 ...
- HDU 3537 Mock Turtles型翻硬币游戏
题目大意: 每次可以翻1个或者2个或者3个硬币,但要保证最右边的那个硬币是正面的,直到不能操作为输,这题目还有说因为主人公感情混乱可能描述不清会有重复的硬币说出,所以要去重 这是一个Mock Turt ...
- BBS+Blog项目流程及补充知识点
项目流程: 1. 产品需求 (1)基于用户认证组件和Ajax实现登陆验证(图片验证码) (2)基于forms组件和Ajax实现注册功能 (3)设计系统首页(文章列表渲染) (4)设计个人站点页面 (5 ...
- [K/3Cloud] 代码中设置某个字段必录
Control ctl = this.GetControl(fieldKey); FieldEditor editCtl = ctl as FieldEditor; if (editCtl != nu ...
- 特种部队(codevs 1427)
题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达仓库时候,发现 ...
- Redis基于Java的客户端SDK收集
如果要找这类的SDK,第一反应应该直奔官网,找一下看下有什么推荐.先找最权威的回答,找不到再尝试民间方案. 就Redis来说,官方已经提供了一个列表包括市面上绝大多数语言的SDK,可以参考以下网址看J ...