70pts求助

P3372 【模板】线段树 1

leilei1 @ 2024-10-10 21:43:46

#include<bits/stdc++.h>
using namespace std;

long long n,m,a[100005],d[400000],f[40000];

int read(){
    long long res=0;
    long long fl=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') fl=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        res=res*10+ch-'0';
        ch=getchar();
    }
    return fl*res;
}

void build(int l,int r,int p){
    if(l==r){
        d[p]=a[l];
        return;
    }
    long long num=l+((r-l)>>1);
    build(l,num,p*2);
    build(num+1,r,p*2+1);
    d[p]=d[p*2]+d[p*2+1];
}

int getsum(int l,int r,int s,int t,int p){
    if(l<=s&&r>=t){
        return d[p];
    }
    long long num=s+((t-s)>>1);
    if(f[p]){
        d[2*p]+=(num-s+1)*f[p];
        d[2*p+1]+=(t-num)*f[p];
        f[2*p]+=f[p];
        f[2*p+1]+=f[p];
        f[p]=0;
    }
    int cnt=0;
    if(l<=num) cnt+=getsum(l,r,s,num,p*2);
    if(r>num) cnt+=getsum(l,r,num+1,t,p*2+1);
    return cnt;
}

void uplate(int l,int r,int k,int s,int t,int p){
    if(l<=s&&r>=t){
        d[p]+=(t-s+1)*k,f[p]+=k;
        return;
    }
    long long num=s+((t-s)>>1);
    if(f[p]&&s!=t){
        d[2*p]+=(num-s+1)*f[p];
        d[2*p+1]+=(t-num)*f[p];
        f[2*p]+=f[p];
        f[2*p+1]+=f[p];
        f[p]=0;
    }
    if(l<=num) uplate(l,r,k,s,num,p*2);
    if(r>num) uplate(l,r,k,num+1,t,p*2+1);
    d[p]=d[p*2]+d[p*2+1];
}

int main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    build(1,n,1);
    for(int i=1;i<=m;i++){
        long long g,x,y,c;
        g=read(),x=read(),y=read();
        if(g==1){
            c=read();
            uplate(x,y,c,1,n,1);
        }
        else{
            cout<<getsum(x,y,1,n,1)<<endl;
        }
    }
    return 0;
}

by Ooj_bai @ 2024-10-10 22:01:52

@leilei1 你f数组是不是少开了个0


by leilei1 @ 2024-10-12 20:52:29

@Ooj_bai改了,

但还是不对呀 70分


by Lmh1128 @ 2024-10-14 19:58:16

long long---------------------------------------


by leilei1 @ 2024-10-14 21:56:15

@Lmh1128 开了


by Lmh1128 @ 2024-10-14 23:35:37

数组无脑开打过了记录

d[400000],f[40000]

这是你代码片段,建议补习数组


by Lmh1128 @ 2024-10-14 23:36:09

@leilei1 你和leilei2什么关系?


by Lmh1128 @ 2024-10-14 23:37:41

a[100001],d[400001],f[400001];极限AC


by Lmh1128 @ 2024-10-14 23:38:21

a[100000],d[400000],f[400000];少一个就70!!!!!!!


by leilei1 @ 2024-10-16 22:17:50

已AC thanx


|