70pts 求调

P3372 【模板】线段树 1

lishengkai @ 2023-11-18 17:42:16

#include<bits/stdc++.h>
using namespace std;
long long n,m,op,x,y,k,a[100001];
struct tree
{
    long long l,r;
    long long sum,tag;
}t[400001];
long long read()
{
    int x=0,sgn=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            sgn=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+(ch&15);
        ch=getchar();
    }
    return x*sgn;
}
void pushdown(int id)
{
    int mid=(t[id].l+t[id].r)/2;
    if(t[id].tag!=0)
    {
        t[id*2].sum+=t[id].tag*(mid-t[id].l+1);
        t[id*2+1].sum+=t[id].tag*(t[id].r-mid);
        t[id*2].tag+=t[id].tag;
        t[id*2+1].tag+=t[id].tag;
        t[id].tag=0;
    }else
        return;
}
void build(long long l,long long r,long long id)
{
    t[id].l=l;
    t[id].r=r;
    if(l==r)
    {
        t[id].sum=a[l];
        return; 
    }
    long long mid=(l+r)/2;
    build(l,mid,id*2);
    build(mid+1,r,id*2+1);
    t[id].sum=t[id*2].sum+t[id*2+1].sum;
}
void update(long long l,long long r,long long id)
{
    if(x<=l&&y>=r)
    {
        t[id].tag+=k;
        t[id].sum+=k*(r-l+1);
        return;
    }
    pushdown(id);
    long long mid=(l+r)/2;
    if(x<=mid)
        update(l,mid,id*2);
    if(y>mid)
        update(mid+1,r,id*2+1);
    t[id].sum=t[id*2].sum+t[id*2+1].sum;
}
long long query(long long l,long long r,long long id)
{
    if(x<=l&&y>=r)
        return(t[id].sum);
    pushdown(id);
    long long mid=(l+r)/2;
    int ans=0;
    if(x<=mid)
        ans+=query(l,mid,id*2);
    if(y>mid)
        ans+=query(mid+1,r,id*2+1);
    return(ans);
}
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)
    {
        op=read();
        if(op==1)
        {
            x=read();
            y=read();
            k=read();
            update(1,n,1);
        }else
        {
            x=read();
            y=read();
            printf("%lld\n",query(1,n,1));
        }
    }
    return 0;
}

by Misophiliac @ 2023-11-18 17:45:43

@lishengkai read()里的x也需要开long long


by lishengkai @ 2023-11-18 17:46:55

@yuanxiuan 还是70pts


by Misophiliac @ 2023-11-18 17:48:57

@lishengkai 哦还有query里ans


by lishengkai @ 2023-11-18 17:50:39

@yuanxiuan orzorz!!!


|