70pts求调

P3372 【模板】线段树 1

jason_jason @ 2024-10-22 21:39:23

https://www.luogu.com.cn/record/184103679

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
ll d[3000001],a[100002],b[3000001];
void build(int s,int t,int p)
{
    if(s==t)
    {
        d[p]=a[s];
        return;
    }
    int m=(s+t)/2;
    build(s,m,2*p);
    build(m+1,t,2*p+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 and r>=t) return d[p];
    int m=(t+s)/2,sum=0;
    if(b[p])
    {
        b[2*p]+=b[p];b[2*p+1]+=b[p];
        d[2*p]+=b[p]*(m-s+1);d[2*p+1]+=b[p]*(t-m);
        b[p]=0;
    }
    if(l<=m) sum+=getsum(l,r,s,m,2*p);
    if(r>=m+1) sum+=getsum(l,r,m+1,t,2*p+1);
    return sum;
}
void upset(int l,int r,int c,int s,int t,int p)
{
    if(l<=s and r>=t)
    {
        d[p]+=c*(t-s+1);
        b[p]+=c;
        return ;
    }
    int m=(t+s)/2;
    if(b[p] and s!=t)
    {
        b[2*p]+=b[p],b[2*p+1]+=b[p];
        d[2*p]+=b[p]*(m-s+1),d[2*p+1]+=b[p]*(t-m);
        b[p]=0; 
    }
    if(l<=m) upset(l,r,c,s,m,2*p);
    if(r>m) upset(l,r,c,m+1,t,2*p+1);
    d[p]=d[2*p]+d[2*p+1];
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,n,1);
    while(m--)
    {
        int nu; cin>>nu;
        if(nu==1)
        {
            ll x,y,k;
            cin>>x>>y>>k;
            upset(x,y,k,1,n,1);
        }
        else
        {
            ll x,y;
            cin>>x>>y;
            cout<<getsum(x,y,1,n,1)<<endl;
        }
    }
    return 0;
}

感谢


by sxshm @ 2024-10-22 21:43:24

你的getsum里面的sum没开long long @jason_jason


by sxshm @ 2024-10-22 21:45:01

干脆把涉及运算的变量都改成long long


by Azure_Space @ 2024-10-22 21:46:25

@jason_jason 还有你getsum的返回值应该是long long


by sxshm @ 2024-10-22 21:48:33

int溢出了我喜欢用 #define int long long


|