70pts线段树求调QAQ

P3372 【模板】线段树 1

xw_lemon @ 2024-08-07 21:27:43

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
long long ans;
long long t[400010],lz[400010],a[100001];
void build(int id,int l,int r){
    if(l==r){
        t[id]=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    t[id]=t[id<<1|1]+t[id<<1];
}
void uppush(int id){
    t[id]=t[id<<1]+t[id<<1|1];
}
void downpush(int id,int l,int r){
    if(lz[id]){
        int mid=(l+r)>>1;
        lz[id<<1]+=lz[id];
        lz[id<<1|1]+=lz[id];
        t[id<<1]+=lz[id]*(mid-l+1);
        t[id<<1|1]+=lz[id]*(r-mid);
        lz[id]=0;
    }
}
void gx(int id,int l,int r,int x,int y,long long v){
    if(l>=x&&r<=y){
        lz[id]+=v;
        t[id]+=v*(r-l+1);
        return ;
    }
    downpush(id,l,r);
    int mid=(l+r)>>1;
    if(x<=mid) gx(id<<1,l,mid,x,y,v);
    if(y>mid) gx(id<<1|1,mid+1,r,x,y,v);
    uppush(id);
}
void tfind(int id,int l,int r,int x,int y){
    if(l>=x&&r<=y){
        ans+=t[id];
        return ;
    }
    downpush(id,l,r);
    int mid=(l+r)>>1;
    if(x<=mid) tfind(id<<1,l,mid,x,y);
    if(y>mid) tfind(id<<1|1,mid+1,r,x,y);
}
signed main(){
//  freopen("P3372_8.in","r",stdin);
//  freopen("ii.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    build(1,1,n);
    int a,b,c;
    long long k;
    for(int i=1;i<=m;i++){
        ans=0;
        scanf("%d%d%d",&c,&a,&b);
        if(c==1){
            scanf("%u",&k);
            gx(1,1,n,a,b,k);
        }
        else{
            tfind(1,1,n,a,b);
            printf("%u\n",ans);
        }
    }
    return 0;
}

评测

谢谢大佬指点


by _buzhidao_ @ 2024-08-07 21:30:04

@xw_lemon 开lazy_tag了没?


by K_J_M @ 2024-08-07 21:32:02

@xw_lemon 你用了define int long long,可是你的scanf里面却是%d,应该改为%lld


by xw_lemon @ 2024-08-07 21:32:05

@buzhidao 谢谢 我傻


by xw_lemon @ 2024-08-07 21:32:20

@buzhidao 已关


by xw_lemon @ 2024-08-07 21:36:13

@K_J_M 过了,谢谢,已关


by _buzhidao_ @ 2024-08-07 22:50:49

@xw_lemon 感谢,已回关。


|