【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 ...
随机推荐
- IDEA -- idea无法导入HttpServlet包解决方法
IntelliJ IDEA 没有导入 servlet-api.jar 这个架包,需要你手动导入支持. 步骤1: 步骤2: 步骤3: 在弹出框中找到Tomcat安装路径 下的lib文件夹..中的Serv ...
- vim跳转(一)
参考资料:http://easwy.com/blog/archives/advanced-vim-skills-basic-move-method/ 在normal模式下使用如下命令 1.h, j, ...
- C++命名空间、标准库(std,全局命名空间)
背景 别人遇到的问题: C++ 全局变量不明确与 using namespace std 冲突 我遇到的问题与他相似,函数调用冲突 using namespace std; class compare ...
- [Python3网络爬虫开发实战] 6.3-Ajax结果提取
这里仍然以微博为例,接下来用Python来模拟这些Ajax请求,把我发过的微博爬取下来. 1. 分析请求 打开Ajax的XHR过滤器,然后一直滑动页面以加载新的微博内容.可以看到,会不断有Ajax请求 ...
- PHPExcel读取表格内容
PHPExcel读取表格 先引入类IOFactory.php require_once '../PHPExcel/IOFactory.php'; $filePath = "test.xlsx ...
- Buffer.isBuffer()详解
Buffer.isBuffer(obj) obj {Object} 返回:{Boolean} 如果 obj 是一个 Buffer 则返回 true.
- Bootstrap-Table 总结
Bootstrap-Table 总结 jQuery Java Bootstrap-Table JS文件 传参:直接将需要的参数置于 queryParams 方法中,例如 line:formData注意 ...
- 1002. A+B for Polynomials (25) (浮点数判0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
- * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)
题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...
- hdu - 1172 猜数字 (思维题)
http://acm.hdu.edu.cn/showproblem.php?pid=1172 这个题换一种想法,可以找出四位数中所有满足条件的数看是否只有一个. #include <iostre ...