70分求调

P3372 【模板】线段树 1

wjj201p @ 2023-10-26 15:27:47

WA #8#9#10

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
ll n,m,x,y,d,a[1000001];
struct need{
    ll l,r,m,add; 
}tree[400010];
void pushdown(int x){
    if(tree[x].add!=0)
    {   
        tree[x<<1].add+=tree[x].add; 
        tree[x<<1|1].add+=tree[x].add;  
        tree[x<<1].m+=tree[x].add*(tree[x<<1].r-tree[x<<1].l+1);  
        tree[x<<1|1].m+=tree[x].add*(tree[x<<1|1].r-tree[x<<1|1].l+1); 
        tree[x].add=0;
    } 
    return ;
} 
void bulid(ll l,ll r,ll now){
    tree[now].l=l,tree[now].r=r,tree[now].add=0;
    if(l>=r)
    {
        scanf("%lld",&tree[now].m);
        return ;
    }
    ll mid=(l+r)>>1;
    bulid(l,mid,now<<1); 
    bulid(mid+1,r,now<<1|1);
    tree[now].m=tree[now<<1].m+tree[now<<1|1].m;
    return ;
} 
void sum(ll l,ll r,ll now,ll ans){
    if(l<=tree[now].l && tree[now].r<=r)
    {
        tree[now].m+=ans*(tree[now].r-tree[now].l+1);
        tree[now].add+=ans;
        return ;
    }
    pushdown(now);
    ll mid=(tree[now].l+tree[now].r)>>1;
    if(l<=mid) sum(l,r,now<<1,ans);
    if(mid<r) sum(l,r,now<<1|1,ans);
    tree[now].m=tree[now<<1].m+tree[now<<1|1].m;
    return ;
}
int cheag(ll l,ll r,ll now){
    ll ans=0;
    if(tree[now].r<l || r<tree[now].l) return 0;
    if(l<=tree[now].l && tree[now].r<=r) 
        return tree[now].m;
    pushdown(now); 
    ll mid=(tree[now].l+tree[now].r)>>1;
    if(l<=mid) ans+=cheag(l,r,now<<1);
    if(mid<r) ans+=cheag(l,r,now<<1|1);
    return ans;
}
int main(){
    scanf("%lld%lld",&n,&m);
    bulid(1,n,1);
    for(ll i=1;i<=m;i++)
    {
        int l;
        scanf("%d",&l);
        if(l==1)
        {
            scanf("%lld%lld%lld",&x,&y,&d);
            sum(x,y,1,d);
        }
        else 
        {
            scanf("%lld%lld",&x,&y);
            printf("%lld\n",cheag(x,y,1));
        }
    }
    return 0;
}

by 红黑树 @ 2023-10-26 19:58:56

@wjj2011 开 long long


by wjj201p @ 2023-10-29 17:14:03

@红黑树 已关,谢谢


|