30pts,求助

P3372 【模板】线段树 1

wangft @ 2024-08-17 15:32:20

#include<iostream>
using namespace std;
struct node{
    long long al,ar,lp,rp,sum,la;
}tr[400010];
long long a[100010],n,tn,m;
long long built(int p){
    if(tr[p].al==tr[p].ar){
        tr[p].sum=a[tr[p].al];
        return tr[p].sum;
    }
    tn++;
    int m=(tr[p].al+tr[p].ar)/2;
    tr[tn].al=tr[p].al;
    tr[tn].ar=m;
    tr[p].lp=tn;
    tn++;
    tr[tn].al=m+1;
    tr[tn].ar=tr[p].ar;
    tr[p].rp=tn;
    tr[p].sum=built(tr[p].lp)+built(tr[p].rp);
    return tr[p].sum;
}
int find(int l,int r,int p){
    if(l<=tr[p].al&&r>=tr[p].ar){
        return tr[p].sum;
    }
    int m=(tr[p].al+tr[p].ar)/2;
    int sum=0;
    if(tr[p].la>0){
        tr[tr[p].rp].sum+=(tr[tr[p].rp].ar-tr[tr[p].rp].al+1)*tr[p].la;
        tr[tr[p].rp].la=tr[p].la;
        tr[tr[p].lp].sum+=(tr[tr[p].lp].ar-tr[tr[p].lp].al+1)*tr[p].la;
        tr[tr[p].lp].la=tr[p].la;
        tr[p].la=0;
        }
    if(r>m){
        sum+=find(l,r,tr[p].rp);
    }
    if(l<=m){
        sum+=find(l,r,tr[p].lp);
    }
    return sum;
}
void add(int l,int r,int k,int p){
    int m=(tr[p].al+tr[p].ar)/2;
    if(l==tr[p].al&&r==tr[p].ar){
        tr[p].sum+=(r-l+1)*k;
        tr[p].la+=k;
    }
    else{
        tr[p].sum+=(r-l+1)*k;
        if(r<=m) add(l,r,k,tr[p].lp);
        else if(l>m) add(l,r,k,tr[p].rp);
        else if(l<=m&&r>m){
            add(l,m,k,tr[p].lp);
            add(m+1,r,k,tr[p].rp);
        }
    }
}
int main(){
    int i,j;
    cin>>n>>m;;
    for(i=1;i<=n;i++) cin>>a[i];
    tn++;
    tr[1].al=1; tr[1].ar=n;
    built(1);
    int x,y,z,gg;
    for(i=1;i<=m;i++){
        cin>>gg;
        if(gg==1){
            cin>>x>>y>>z;
            add(x,y,z,1);
        }
        else{
            cin>>x>>y;
            cout<<find(x,y,1)<<endl;;
        }
    }
    return 0;
}

by kaaaai @ 2024-08-17 15:41:20

@wangft qp


by kaaaai @ 2024-08-17 17:57:31

@wangft hp


by kaaaai @ 2024-08-18 11:25:27

@wangft 考古


by wangft @ 2024-08-18 21:52:31

搞定了,没事了


|