当前位置:网站首页>基本.分块
基本.分块
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;
}
边栏推荐
- GPO:在 Start/Logon 中使用 PowerShell 脚本
- Kunlunbase support for MySQL private DML syntax
- 使用tidymodels搞定二分类logistic模型
- Steps of project explanation in interview
- 使用R包PreMSIm根据基因表达量来预测微卫星不稳定
- GBase8s Informix Dodker 高可用集群自恢复集群启动命令oninitdb的设计与实现
- How to realize the function of adding watermark
- 一键搭建博客:如何使用WordPress插件搭建专属博客
- 判断两个对象的值是否都相等
- Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
猜你喜欢

Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase

浅谈安科瑞灭弧式智慧用电在养老机构的应用

Kunlunbase instruction manual (III) data import & synchronization

站点数据收集-Scrapy使用笔记

开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源

Why use markdown to write?

报表控件FastReport与StimulSoft功能对比

开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览

factoextra:多元统计方法的可视化PCA

Data visualization design guide (information chart)
随机推荐
使用tidymodels搞定二分类logistic模型
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
factoextra:多元统计的可视化
Pytorch 入门
Kunlun storage vs PostgreSQL OLTP test
周鸿祎:360是世界上最大的安全大数据公司
PaddleLite 编译以及代码跑通复盘
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Understand what a binary tree is (types, traversal methods, definitions of binary trees)
Spark高效数据分析01、idea开发环境搭建
GBase8s核心数据备份
重磅 | 开放原子校源行活动正式启动
聊聊性能测试环境搭建
Leetcode bit operation
Site data collection -scrapy usage notes
matplotlib中文问题
leetcode-位运算
If distributed file storage is realized according to integrated Minio
Kunlunbase instruction manual (I) quick installation manual
Conference OA project - my approval