介绍下简单的分块:

当我们遇到区间类问题的时候,如何保证我们快速而高效地完成操作?

答案是线段树分块。

所谓分块,就是把一个序列分成许多块分别维护。是不是想起了树状数组 这样能大大提高效率:

例如,我们需要查询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】简单的分块的更多相关文章

  1. HYSBZ 2957 分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 题意:中文题面 思路: 来自此博客 首先明确问题,对于每栋楼房的斜率K=H/X,问题 ...

  2. [BZOJ 2957]楼房重建(THU2013集训)(分块思想)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 首先明确问题,对于每栋楼房的斜率K=H/X,问题就是问有多少个楼房的K比前面所有 ...

  3. P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?LCT?...FAQ orz

    好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...

  4. P3203 [HNOI2010]弹飞绵羊 —— 懒标记?分块?

    好久没写博客了哈,今天来水一篇._(:з」∠)_ 题目 :弹飞绵羊(一道省选题) 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏 ...

  5. 分块基础练习 UESTC 1324

    http://acm.uestc.edu.cn/#/problem/show/1324 思路:基础分块,这个是一个特别简单的分块,就当做是一个练习了.然后这题也是很简单的单点线段树更新. //看看会不 ...

  6. [luogu3203 HNOI2010] 弹飞绵羊 (分块)

    传送门 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...

  7. NLP(六) 分块、句法分析、依存分析

    内置分块器 分块:从文本中抽取短语 import nltk text = 'Lalbagh Botanical Garden is a well known botanical garden in B ...

  8. Codechef TRIPS Children Trips (分块、倍增)

    题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...

  9. HTTP 协议中的 Content-Encoding 和 Transfer-Encoding(内容编码和传输编码)

    转自:http://network.51cto.com/art/201509/491335.htm Transfer-Encoding,是一个 HTTP 头部字段,字面意思是「传输编码」.实际上,HT ...

随机推荐

  1. H5实现调用本地摄像头实现实时视频以及拍照功能

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  3. 根据Dockerfile创建hello docker镜像

    一.编写hello可执行c文件: 1.安装:gcc glibc glibc-static yum install -y gcc glibc glibc-static 2.编写hello.c:vim h ...

  4. django-1创建项目创建app设置setting、urls、templates、views等

    1. python -m django --version 查看版本 1.11.4 在需要创建项目的目录下执行: 2. django-admin startproject myblog => 创 ...

  5. 牛客网NOIP赛前集训营 第6场 T1 最长路

    [题解] 先建反向图,然后跑拓扑排序求出最长路. 将所有的点按照最长路从小到大分层,把上一层连向这一层的边按照边权为第一关键字.起点的排名为第二关键字排序. 按照这个顺序更新这一层的答案,按照这一层每 ...

  6. The Bells are Ringing(枚举)

    Description Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this ...

  7. 【01】《响应式Web设计:HTML5和CSS3实战》

    [01]   (魔芋:已看完.) [01]<响应式Web设计:HTML5和CSS3实战>(全).pdf 共246页.   2013年1月出版.   读后感:适合入门的书籍,对于响应式布局, ...

  8. HDU 5458 Stability

    Stability Time Limit: 2000ms Memory Limit: 102400KB This problem will be judged on HDU. Original ID: ...

  9. xtu read problem training 4 A - Moving Tables

    Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...

  10. tyvj1045 最大的算式

    描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号.例 ...