哪里有问题?
查看原帖
哪里有问题?
873417
Istruggle楼主2023/11/13 22:22

哪里有问题?

#include<bits/stdc++.h>
#define int long long
using namespace std;
int tree[2000005],a[200005];
int mark[2000005];
int n,m;
void build(int l,int r,int p){
    if(l==r)
    tree[p] = a[l];
    else{
        int mid=(l+r)>>1;
        build(1,mid,p*2);
        build(mid+1,r,p*2+1);
        tree[p]=tree[p*2]+tree[p*2+1];
    }
}
inline void push_down(int p,int len)
{
    mark[p*2]+=mark[p];
    mark[p*2+1]+=mark[p];
    tree[p*2]+=mark[p]*(len-len/2);
    tree[p*2+1]+=mark[p]*(len/2);
    mark[p]=0;
}
void update(int l,int r,int d,int p,int cl,int cr)
{
    if(cl>r||cr<l)
    return;
    else if(cl>=l&&cr<=r){
        tree[p]+=(cr-cl+1)*d;
        if(cr>cl) mark[p]+=d;
    }
    else{
        int mid = (cr+cl)>>1;
        push_down(p,cr-cl+1);
        update(l,r,d,p*2,cl,mid);
        update(l,r,d,p*2+1,mid+1,cr);
        tree[p]=tree[p*2]+tree[p*2+1];
    }
}
int query(int l,int r,int p,int cl,int cr)
{
    if(cl>r||cr<l) return 0;
    else if(cl>=l&&cr<=r){
        return tree[p];
    }
    else{
        int mid=(cl+cr)>>1;
        push_down(p,cr-cl+1);
        return query(l,r,p*2,cl,mid)+query(l,r,p*2+1,mid+1,cr);
    }
}
signed main()
{
    scanf("%lld%lld",&n,&m);
    for(int i = 1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    build(1,n,1);
    while(m--){
        int opt;
        cin>>opt;
        if(opt==1){
            int x,y,k;
            scanf("%lld%lld%lld",&x,&y,&k);
            update(x,y,k,1,1,n);
        }
        if(opt==2){
            int x,y;
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",query(x,y,1,1,n));
        }
    }
    return 0;
}
2023/11/13 22:22
加载中...