全RE样例过求调

P3372 【模板】线段树 1

Auzhaousir @ 2024-08-29 11:34:45

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000001*4
unsigned long long ans[N],n,m,a[N],tag[N];
inline ll ls(ll u){return u<<1;}inline ll rs(int u){return u<<1|1;}
inline ll pushup(ll u){ans[u]=ans[ls(u)]+ans[rs(u)];}
void build(ll u,ll l,ll r){
    tag[u]=0;
    if(l==r){ans[u]=a[l];return;}
    ll mid=(l+r)>>1;
    build(u<<1,l,mid);build(u<<1|1,mid+1,r);
    pushup(u);
}
inline void f(ll u,ll l,ll r,ll k){ans[u]=ans[u]+k*(r-l+1);tag[u]=tag[u]+k;}
inline void pushdown(ll u,ll l,ll r){
    ll mid=(l+r)>>1;
    f(u<<1,l,mid,tag[u]);f(u<<1|1,mid+1,r,tag[u]);
    tag[u]=0;
}
inline void update(ll u,ll nl,ll nr,ll l,ll r,ll k){
    if(nl<=l&&r<=nr){ans[u]+=k*(r-l+1);tag[u]+=k;return;}
    pushdown(u,l,r);ll mid=(l+r)>>1;
    if(nl<=mid)update(ls(u),nl,nr,l,mid,k);
    if(nr>mid)update(rs(u),nl,nr,mid+1,r,k);
    pushup(u);
}
ll query(ll u,ll nl,ll nr,ll l,ll r){
    ll res=0;
    if(nl<=l&&r<=nr)return ans[u];
    ll mid=(l+r)>>1;pushdown(u,l,r);
    if(nl<=mid)res+=query(ls(u),nl,nr,l,mid);
    if(nr>mid)res+=query(rs(u),nl,nr,mid+1,r);
    return res;
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll p,b,c,d,e,f;
    cin>>n>>m;
    for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
    build(1,1,n);
    while(m--){
        cin>>p;
        switch(p){
            case 1:{scanf("%lld%lld%lld",&b,&c,&d);update(1,b,c,1,n,d);break;}
            case 2:{scanf("%lld%lld",&e,&f);printf("%lld\n",query(1,e,f,1,n));break;}
        }   
    }
    return 0;
}

by _buzhidao_ @ 2024-08-29 11:37:38

@Auzhaousir 后面的 mid 不是整个区间/2,而是 (t[u].l+t[u].r)/2 之类的。


by _buzhidao_ @ 2024-08-29 11:38:34

@Auzhaousir 当初的我


by _buzhidao_ @ 2024-08-29 11:40:23

@Auzhaousir 现在的我
几乎就改了一行代码。


by Auzhaousir @ 2024-08-29 11:41:47

@_buzhidao_什么意思,没懂,抱歉


by Auzhaousir @ 2024-08-29 11:42:31

@buzhidao 什么意思,没懂,抱歉


by Auzhaousir @ 2024-08-29 11:43:41

哦,谢谢 @buzhidao


by _buzhidao_ @ 2024-08-29 12:00:13

@Auzhaousir 不用谢,


|