求调,悬1关

P3372 【模板】线段树 1

OIer_Hhy @ 2024-07-26 11:54:20

为啥不对

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int val[N],tag[N],a[N];
inline void pushup(int rt){
    val[rt]=val[rt<<1]+val[rt<<1|1];
}
inline void pushdown(int rt,int len){
    if(tag[rt]){
        val[rt<<1]+=tag[rt]*(len>>1);
        val[rt<<1|1]+=tag[rt]*((len+1)>>1);
        tag[rt<<1]+=tag[rt];
        tag[rt<<1|1]+=tag[rt];
        tag[rt]=0; 
    }
}
inline void build(int rt,int l,int r){
    if(l==r){
        val[rt]=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    pushup(rt);
}
inline void update(int rt,int L,int R,int l,int r,int k){
    if(L<=l&&r<=R){
        val[rt]+=k*(r-l+1);
        tag[rt]+=k;
    }
    pushdown(rt,r-l+1);
    int mid=(l+r)>>1;
    if(L<=mid) update(rt<<1,L,R,l,mid,k);
    if(R>mid) update(rt<<1|1,L,R,mid+1,r,k);
    pushup(rt);
}
inline int query(int rt,int L,int R,int l,int r){
    if(L<=l&&r<=R) return val[rt];
    pushdown(rt,r-l+1);
    int mid=(l+r)>>1,ans=0;
    if(L<=mid) ans+=query(rt<<1,L,R,l,mid);
    if(R>mid) ans+=query(rt<<1|1,L,R,mid+1,r);
    return ans;
}
int n,q,opt,x,y,k;
int main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,n);
    while(q--){
        cin>>opt;
        if(opt==1){
            cin>>x>>y>>k;
            update(1,x,y,1,n,k);
        }else{
            cin>>x>>y;
            cout<<query(1,x,y,1,n)<<endl;
        }
    }
    return 0;
}

by sea_water @ 2024-07-26 12:06:33

这道题范围是longlong


by sea_water @ 2024-07-26 12:07:34

你开错范围了


by sea_water @ 2024-07-26 12:09:11

@OIer_Hhy


by OIer_Hhy @ 2024-07-26 12:11:27

@sunboyang_01 thx,关注了


|