模板求调,错了样例

P3372 【模板】线段树 1

Nwayy @ 2023-02-20 12:32:52

#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define int long long
int n,m,i,j,ans,a[N],q;
int opt,x,y,k,tr[N<<2],tag[N<<2];
void build(int l,int r,int poi){
    if(l==r){
        tr[poi]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,poi<<1);
    build(mid+1,r,poi<<1|1);
    tr[poi]=tr[poi<<1]+tr[poi<<1|1];
} 
void pushdown(int l,int r,int mid,int poi){
    if(l==r || tag[poi]==0) return;
    tr[poi<<1]+=(mid-l+1)*tag[poi];
    tr[poi<<1|1]+=(r-mid)*tag[poi];
    tag[poi<<1]+=tag[poi];
    tag[poi<<1|1]+=tag[poi];
    tag[poi]=0;
}
void update(int l,int r,int poi,int x,int y,int c){
    if(l>=x && r<=y){
        tr[poi]+=(r-l+1)*c;
        tag[poi]+=c;
        return;
    }
    int mid=(l+r)>>1;
    pushdown(l,r,mid,poi);
    if(mid>=x && mid<=y) update(l,mid,poi<<1,x,y,c);
    if(mid+1>=x && mid+1<=y) update(mid+1,r,poi<<1|1,x,y,c);
    tr[poi]=tr[poi<<1]+tr[poi<<1|1];
}
int query(int l,int r,int poi,int x,int y){
    if(l>=x && r<=y) return tr[poi];
    int mid=(l+r)>>1,sum=0;
    pushdown(l,r,m,poi);
    if(mid>=x && mid<=y) sum+=query(l,mid,poi<<1,x,y);
    if(mid+1>=x && mid+1<=y) sum+=query(mid+1,r,poi<<1|1,x,y);
    return sum;
}
signed main(){
    scanf("%lld%lld",&n,&q);
    for(i=1;i<=n;i++) scanf("%lld",&a[i]);
    build(1,n,1);
    while(q--){
        scanf("%lld%lld%lld",&opt,&x,&y);
        if(opt==1){
            scanf("%lld",&k);
            update(1,n,1,x,y,k);
        }
        else printf("%lld\n",query(1,n,1,x,y));
    }
    return 0;
}

|