求助,70

P3372 【模板】线段树 1

liankunxiang @ 2024-03-31 14:27:00

rt


#include<bits/stdc++.h>
#define MAXN 400010
using namespace std;
int r[MAXN];
int n,m;
int tree[MAXN];
int b[MAXN];
void build(int s,int t,int p) {
    if(s == t) { 
        tree[p]=r[s];
        return;
    }
    int tmp=s+((t-s)/2);
    build(s,tmp,p*2);
    build(tmp+1,t,p*2+1);
    tree[p]=tree[p*2]+tree[p*2+1];
}
void update(int l,int r,int c,int s,int t,int p) {
    if(l<=s&&t<=r) {
        tree[p]+=(t-s+1)*c;
        b[p]+=c;
        return;
    }
    int tmp=s+((t-s)/2);
    if(b[p]&&s!=t) {
        tree[p*2]+=(tmp-s+1)*b[p];
        tree[p*2+1]+=(t-tmp)*b[p];
        b[p*2]+=b[p];
        b[p*2+1]+=b[p];
        b[p]=0;
    }
    if(l<=tmp) update(l,r,c,s,tmp,p*2);
    if(r>tmp) update(l,r,c,tmp+1,t,p*2+1);
    tree[p]=tree[p*2]+tree[p*2+1];
}
int getsum(int l,int r,int s,int t,int p) {
    if(l<=s&&t<=r) return tree[p];
    int tmp=s+((t-s)/2),sum=0;
    if (b[p]) {
        tree[p*2]+=b[p]*(tmp-s+1);
        tree[p*2+1]+=b[p]*(t-tmp);
        b[p*2]+=b[p];
        b[p*2+1]+=b[p];
        b[p] = 0;
    }
    if(l<=tmp) sum+=getsum(l,r,s,tmp,p*2);
    if(r>tmp) sum+=getsum(l,r,tmp+1,t,p*2+1);
    return sum;
}
int main() {
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
        cin>>r[i];
    }
    build(1,n,1);
    for(int i=1;i<=m;i++) {
        int op;
        cin>>op;
        if(op==1) {
            int l,r,c;
            cin>>l>>r>>c;
            update(l,r,c,1,n,1);
        }else {
            int l,r;
            cin>>l>>r;
            cout<<getsum(l,r,1,n,1)<<endl;
        }
    }
    return 0;
}

by danlao @ 2024-03-31 14:35:14

@liankunxiang int r[MAXN];?


by _wsq_ @ 2024-03-31 15:00:17

十年OI一场空,不开__见祖宗


by Xdik @ 2024-03-31 15:38:50

开long long


|