分块10pts求调

P3372 【模板】线段树 1

Milthm @ 2023-07-24 18:43:34

#include<iostream>
#include<cmath>
#define int long long
using namespace std;
int n,m,len,a[100005],f[1005],s[1005],e[1005],size[1005];
int lazy[1005];
int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
signed main(){
    cin>>n>>m;
    len=sqrt(n);
    for(int i=1;i<=n;++i)a[i]=read();
    for(int i=1;i<=len;++i){
        s[i]=(i-1)*n/len+1,e[i]=i*n/len;
        for(int j=s[i];j<=e[i];++j)f[i]+=a[j];
        size[i]=e[i]-s[i]+1;
        //cout<<f[i]<<" "<<s[i]<<" "<<e[i]<<"\n";
    }
    while(m--){
        int p,l,r,k;
        p=read();
        if(p==1){
            l=read();
            r=read();
            k=read();
            for(int i=1;i<=len;++i){
                if(l>s[i]&&l<=e[i]){
                    for(int j=l;j<=min(e[i],r);++j)a[j]+=k;
                }
                else if(l<=s[i]&&e[i]<=r){
                    lazy[i]+=k;
                }
                else if(r>=s[i]&&r<=e[i]){
                    for(int j=max(l,s[i]);j<=r;++j)a[j]+=k;
                }
                else if(s[i]>r)break;
            }
        }
        else{
            l=read();
            r=read();
            int ans=0;
            for(int i=1;i<=len;++i){
                if(l>s[i]&&l<=e[i]){
                    for(int j=l;j<=min(e[i],r);++j)ans+=a[j]+lazy[i];
                }
                else if(l<=s[i]&&e[i]<=r){
                    ans+=f[i]+lazy[i]*size[i];
                }
                else if(r>=s[i]&&r<=e[i]){
                    for(int j=max(l,s[i]);j<=r;++j)ans+=a[j]+lazy[i];
                }
                else if(s[i]>r)break;
            }
            printf("%lld\n",ans);
        }
        /*
        cout<<"-----------------\n";
        for(int i=1;i<=len;++i){
            cout<<lazy[i]<<" ";
        }
        cout<<endl;
        for(int i=1;i<=n;++i){
            cout<<a[i]<<" ";
        }
        cout<<endl<<"------------------\n";
        */
    }

    return 0;
}

蒟蒻不会分块,瞎写的(我甚至不知道这是不是分块),大佬们看看哪里错了(


by Milthm @ 2023-07-24 18:45:43

悬赏5毛()


by ACRUSHj @ 2023-07-24 18:55:06

@songjiahao_ 哥,块长不一定是块数


by Milthm @ 2023-07-24 19:00:52

@ACRUSHj 可是我连数据2都过不了,我看着数据2里n是8啊


by Milthm @ 2023-07-24 19:07:34

哦此贴结,原因是单独加的时候a[i]没有更新


by ACRUSHj @ 2023-07-24 19:08:46

哦哦,你这个写法看起来可以


by ACRUSHj @ 2023-07-24 19:09:29

噗,f[i]+lazy[i]*size[i]没括号


|