黄老师的博客http://hzwer.com/8053.html

模板:

const int N=1e5+5;
int a[N],belong[N]/*属于哪个块*/,blo/*块的大小*/,block[1000];
void update(int l,int r){
    if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++){
            //你的操作 
        }
        return ;
    }
    for(int i=l;i<=belong[l]*blo;i++){
        //你的操作 
    } 
    for(int i=belong[l]+1;i<=belong[r]-1;i++){
        //你的操作 
    } 
    for(int i=(belong[r]-1)*blo+1;i<=r;i++){
        //你的操作 
    } 
} 
int query(int l,int r){
    int ans=0;
    if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++){
            //你的操作 
        }
        return ans;
    }
    for(int i=l;i<=belong[l]*blo;i++){
        //你的操作 
    } 
    for(int i=belong[l]+1;i<=belong[r]-1;i++){
        //你的操作 
    } 
    for(int i=(belong[r]-1)*blo+1;i<=r;i++){
        //你的操作 
    } 
    return ans;
} 

数列分块入门 1

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=5e4+5;
int belong[N],a[N],block[1000],b;
void add(int l,int r,int c){
    if(belong[l]==belong[r]){
        for(int i=l;i<=r;i++)
        a[i]+=c;
        return ;
    }
    for(int i=l;i<=belong[l]*b;i++)a[i]+=c;
    for(int i=belong[l]+1;i<=belong[r]-1;i++)block[i]+=c;
    for(int i=(belong[r]-1)*b+1;i<=r;i++)a[i]+=c;

}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,o,l,r,c;
    cin>>n;
    b=sqrt(n);
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)belong[i]=(i-1)/b+1;
    for(int i=1;i<=n;i++){
        cin>>o>>l>>r>>c;
        if(o==0)add(l,r,c);
        else cout<<a[r]+block[belong[r]]<<endl;
    }
    return 0;
}
View Code

相关文章:

  • 2022-01-19
  • 2021-10-30
  • 2022-12-23
  • 2021-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2022-12-23
  • 2021-06-20
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案