70分求调

P3372 【模板】线段树 1

xuman @ 2024-12-08 17:47:21

#include<bits/stdc++.h>
using namespace std;

long long shu[400010],a[100010],ld[100010];
long long n,m;

void cj(long long l,long long r,long long x){
    if(l==r){
        shu[x]=a[l];
        return;
    }
    long long mid=(l+r)>>1;
    cj(l,mid,x*2);
    cj(mid+1,r,x*2+1);
    shu[x]=shu[x*2]+shu[x*2+1];
}

void qjxg(long long xx,long long xy,long long nx,long long ny,long long z,long long x){
    //cout<<"dyx";
    if(xx<=nx&&ny<=xy){
        shu[x]+=(ny-nx+1)*z;
        ld[x]+=z;
        return;
    }
    long long mid=(nx+((ny-nx)>>1));
    if(ld[x]&&(nx!=ny)){
        shu[x*2]+=(mid-nx+1)*ld[x];
        shu[x*2+1]+=(ny-mid)*ld[x];
        ld[x*2]+=ld[x];
        ld[x*2+1]+=ld[x];
        ld[x]=0;
    }
    if(xx<=mid)qjxg(xx,xy,nx,mid,z,x*2);
    if(xy>mid)qjxg(xx,xy,mid+1,ny,z,x*2+1);
    shu[x]=shu[x*2]+shu[x*2+1];
}

long long qjqh(long long xx,long long xy,long long nx,long long ny,long long x){
    //cout<<"dyx";
    if(xx<=nx&&ny<=xy){
        return shu[x];
    }
    long long mid=(nx+((ny-nx)>>1));
    if(ld[x]){
        shu[x*2]+=(mid-nx+1)*ld[x];
        shu[x*2+1]+=(ny-mid)*ld[x];
        ld[x*2]+=ld[x];
        ld[x*2+1]+=ld[x];
        ld[x]=0;
    }
    long long dyx=0;
    if(xx<=mid)dyx+=qjqh(xx,xy,nx,mid,x*2);
    if(xy>mid)dyx+=qjqh(xx,xy,mid+1,ny,x*2+1);
    return dyx;
}

int main(){
    cin>>n>>m;
    for(long long i=1;i<=n;i++){
        cin>>a[i];
    }
    cj(1,n,1);
    for(long long i=1;i<=m;i++){
        //cout<<"dyx";
        int dyx;
        cin>>dyx;
        if(dyx==1){
            long long x,y,z;
            cin>>x>>y>>z;
            qjxg(x,y,1,n,z,1);
        }else{
            long long x,y;
            cin>>x>>y;
            cout<<qjqh(x,y,1,n,1)<<'\n';
            //cout<<"dyx";
        }
    }
    return 0;
}

by BennyT @ 2024-12-13 12:45:37

你的ld数组开小了,开成400010


|