【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 ...
随机推荐
- 三、spring中高级装配(1)
大概看了一下第三章的内容,我从项目中仔细寻找,始终没有发现哪里有这种配置,但是看完觉得spring还有这么牛B的功能啊,spring的厉害之处,这种设计程序的思想,很让我感慨... 一.环境与prof ...
- 初识Typescript及vscode环境配置
什么是typescript?为什么要用它? typescript简称ts,是js语法的一个超级,由微软团队维护的 js特点(不足) 弱类型:js中的数据变量没有确定的类型,可以存储对象,可以存储数字, ...
- KBE_创建项目和基本常识
此笔记参考官方文档 第一个项目 资产库:是每一个项目文件夹的名称,使用KBE提供的生成工具生成一个最小资产库,其中包含了很多常用的工具,默认名server_assets: res:放置一些资源(入地图 ...
- Linux基础学习二
新建分区:fdisk /dev/sda(a代表第一块硬盘) 建完后w保存 更新分区表:partprobe mkfs.ext4 /dev/sdb{1..3} : 格式 ...
- FreeRTOS--疑难杂症
花了3个晚上,把这个章节看完,受益匪浅. 最有用的应该是与中断相关的错误,优先排查中断优先级设置. 堆栈溢出检查,可能用到,一般先把堆栈设置的足够大,只要没有溢出就是好事,溢出了,掌握了栈溢出钩子函数 ...
- Automation 的 ReportFlow
ReportFlow: // click the Grid icon and switch to grid page public void changeToGrid() // click the A ...
- HDU1024 多段最大和 DP
题目大意: 在n个数,求不重复的m段中的数据总和的最大值 令dp[i][j]表示将前j个数分成 i 段时得到的最大值(必取到第 j 个数) 状态转移可列为 dp[i][j]=Max(dp[i][j-1 ...
- Delphi DBGrid实现多选
DBGrid1.Options:= DBGrid1.Options+[dgMultiSelect]; //先设置DBGrid1的多选属性为True if DBGrid1.SelectedRows.C ...
- CentOS7下安装单机版RabbitMQ及权限赋予
RabbitMQ官网rpm软件包地址:https://www.rabbitmq.com/releases/ 一.安装环境: CentOS7.erlang-19.0.4-1.el7.centos.x86 ...
- [bzoj2453]维护队列_带修改莫队
维护队列 bzoj-2453 题目大意:给定一个n个数序列,支持查询区间数的种类数,单点修改.不强制在线. 注释:$1\le n,m\le 10^5$. 想法: 带修改莫队裸题. 如果没有修改操作的话 ...