当前位置:网站首页>基本.分块
基本.分块
2022-07-29 10:54:00 【Selvaggia】
普通的数列求和,前缀和
对数列进行修改之后再求和,分块、线段树、树状数组
分块时间复杂度 n ∗ n n*\sqrt{n} n∗n ,慢于 线段树、树状数组 n ∗ l o g n n*logn n∗logn
数量级一般为 1 e 5 1e5 1e5 (到了 1 e 6 1e6 1e6就用线段树)
设 n = = 7 n==7 n==7,则分块大小 l e n = 7 len=\sqrt{7} len=7 ,取整约等于2
查询区间 包含一个完整的块,取块的和 否则,分开处理,在某个块里遍历
总体分为两种情况
要处理(更新/查询)的数据处于同一个块
更新时逐个遍历,修改元素值和sum值
查询时,逐个遍历,元素值+作为整块曾做的修改的记录lazy【块号】(分块的修改,整块曾经的修改 )不处于同一个快块,则可细分为三种状态
左边某块不完整的部分+中间完整的若干块(p+1~q-1)+ 右边某块不完整的部分
更新时,左右不完整的块同情况一
完整的块,直接遍历块(不遍历块中元素)更新sum、打上lazy标记
查询时,左右不完整的块同情况一(分块的修改,整块曾经的修改)
完整的块直接看sum
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
#define int long long int
const int N=1e5+5;
int a[N];
int len,n,m;
int sum[N],lazy[N];
int get(int x){
return x/len+1;
}
void update(int l,int r,int d){
int p=get(l),q=get(r);
if(p==q){
//要处理的数据同一个块
for(int i=l;i<=r;i++){
a[i]+=d,sum[p]+=d;
}
return;
}
for(int i=l;get(i)==p;i++){
//存下来整块做了什么更改
a[i]+=d,sum[p]+=d;
}
for(int i=r;get(i)==q;i--){
//左边不完整的块
a[i]+=d,sum[q]+=d;
}
for(int i=p+1;i<=q-1;i++){
//右边不完整的块
sum[i]+=len*d,lazy[i]+=d;
}
}
int ask(int l,int r){
int res=0;
int p=get(l),q=get(r);
if(p==q){
for(int i=l;i<=r;i++){
res+=(a[i]+lazy[i]);//作为部分块的修改,整块曾经的修改
}
return res;
}
for(int i=l;get(i)==p;i++){
res+=(a[i]+lazy[p]);
}
for(int i=r;get(i)==q;i--){
res+=(a[i]+lazy[q]);
}
for(int i=p+1;i<=q-1;i++){
//中间经历的完整的块
res+=sum[i];
}
return res;
}
void solve(){
cin>>n>>m;
len=sqrt(n);
for(int i=1;i<=n;i++){
cin>>a[i];
sum[get(i)]+=a[i];
}
int l,r,op,d;
while(m--){
cin>>op>>l>>r;
if(op==1){
cin>>d;
update(l,r,d);
}
else {
cout<<ask(l,r)<<endl;
}
}
}
signed main(){
int t;
t=1;
while(t--){
solve();
}
return 0;
}
边栏推荐
- Leetcode binary tree series -- 144. Preorder traversal of binary trees
- Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
- ggdag 绘制DAG和因果图
- 会议OA项目----我的审批
- 「PHP基础知识」使用数组保存数据
- LeetCode_416_分割等和子集
- Luogu p4185 [usaco18jan]mootube g problem solution
- R language brca MRNA data set analysis
- StarRocks 技术内幕:实时更新与极速查询如何兼得
- Understand what a binary tree is (types, traversal methods, definitions of binary trees)
猜你喜欢

主子仓库都修改,如何进行同步?

leetcode-位运算

Niuke net brush questions

StarRocks 技术内幕:实时更新与极速查询如何兼得

AI模型风险评估 第2部分:核心内容

How can agile development reduce cognitive bias in collaboration| Agile way

Meeting OA project (V) -- meeting notice and feedback details

Basic construction of QT project

Drunken driving alarm system based on stm32

阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!
随机推荐
聊聊性能测试环境搭建
A tour of grp:04 - GRP unary call unary call
Detailed arrangement of JVM knowledge points (long text warning)
ADDS:使用 PowerShell 创建 OU 结构
Data visualization design guide (information chart)
R 语言 BRCA.mRNA数据集 分析
『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
带你浅聊一下PHP搭建的电商商城系统
LeetCode二叉树系列——144.二叉树的前序遍历
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
Error: Protobuf syntax version should be first thing in file
Hutool日期时间
多线程顺序运行的 4 种方法,面试随便问!
AI模型风险评估 第2部分:核心内容
如何使用 grep 跨多行查找模式匹配
Using xgboost with tidymodels
浅谈string中的compareTo方法
Kunlunbase instruction manual (I) quick installation manual
QT工程基本构建
JS two array objects for merging and de duplication