10分 求教qwq
查看原帖
10分 求教qwq
719357
pppzz楼主2022/7/27 18:44
#include<bits/stdc++.h>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define ll long long int
using namespace std;
const int maxn=1e5+9;

ll n,m,nums[maxn];
ll sum[4*maxn],tag[4*maxn];

void update(int x){
    sum[x]=sum[ls(x)]+sum[rs(x)];
}

void build(int l,int r,int x){
    if(l==r){
        sum[x]=nums[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,ls(x));
    build(mid+1,r,rs(x));
    update(x);
}

void push_down(int l,int r,int x){
    if(tag[x]){
        int mid=(l+r)>>1;
        tag[ls(x)]+=tag[x];
        tag[rs(x)]+=tag[x];
        sum[ls(x)]+=(mid-l+1)*tag[x];
        sum[rs(x)]+=(r-mid)*tag[x];
        tag[x]=0;
    }
}

void change(int A,int B,int l,int r,int v,int x){
    if(A<=l&&r<=B){
        tag[x]+=v;
        sum[x]+=(r-l+1)*tag[x] ;
        return;
    }
    push_down(l,r,x);
    int mid=(l+r)>>1;
    if(A<=mid)change(A,B,l,mid,v,ls(x));
    if(B>mid)change(A,B,mid+1,r,v,rs(x));
    update(x);
}

ll query(int A,int B,int l,int r,int x){
    if(A<=l&&r<=B){
        return sum[x];
    }
    push_down(l,r,x);
    ll ret=0;
    int mid=(l+r)>>1;
    if(A<=mid)ret+=query(A,B,l,mid,ls(x));
    if(B>mid)ret+=query(A,B,mid+1,r,rs(x));
    return ret;
}

int main(){
    scanf("%lld %lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&nums[i]);
    build(1,n,1);
    for(int i=1;i<=m;i++){
        int s;
        scanf("%d",&s);
        if(s==1){
            int x,y,k;
            scanf("%d %d %d",&x,&y,&k);
            change(x,y,1,n,k,1);
        }
        else {
            int x,y;
            scanf("%d %d",&x,&y);
            cout<<query(x,y,1,n,1)<<"\n";
        }
    }
    return 0;
}

2022/7/27 18:44
加载中...