AC ON #1,3 其余WA,求调

P3372 【模板】线段树 1

ET_theStarSky @ 2023-01-14 17:36:29


#include <bits/stdc++.h>

using namespace std;
long long m;
long long xds[1000000]= {0};
long long lazy_tag[1000000]= {0};
int n;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
    {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9')
    {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}

inline int lowbit(int i)
{
    return i&(-i);
}
int rn;

int init()
{
    n=read();
    m=read();
    rn=n;
    while(rn-lowbit(rn))
    {
        rn+=lowbit(rn);
    }
    for(int i=0; i<n; i++)
    {
        xds[rn+i]=read();
    }
    for(int i=rn-1;i>0;i--){
    xds[i]=xds[2*i+1]+xds[2*i];
    }
    /*for(int i=1;i<=2*rn;i++){
    cout<<xds[i]<<endl;
    }*/
}

int add(int l,int r,int al,int ar,long long  v,int place)
{
    if(l==ar or r==al){return 0;}
    int mid=(l+r)/2;xds[place]+=v*(ar-al);
    if(l==al and r==ar)
    {
        lazy_tag[place]+=v;

        return 0;
    }
    if(al>=mid)
    {
        add(mid,r,al,ar,v,place*2+1);
        return 0;
    }
    if(ar<=mid)
    {
        add(l,mid,al,ar,v,place*2);
        return 0;
    }
    add(l,mid,al,mid,v,place*2);
    add(mid,r,mid,ar,v,place*2+1);
    return 0;
}

long long get(int l,int r,int al,int ar,long long  v,int place)
{
    xds[place]+=v*(r-l);
    v+=lazy_tag[place];

    lazy_tag[place]=0;
    if(l==ar or r==al){lazy_tag[place]+=v;return 0;}
    if(l==al and r==ar)
    {
        lazy_tag[place]+=v;
        //cout<<l<<' '<<r<<' '<<al<<' '<<ar<<' '<<v<<' '<<place<<' '<<xds[place]<<endl;
        return xds[place];
    }
    int mid=(l+r)/2;

    if(al>=mid)
    {

        return get(mid,r,al,ar,v,place*2+1);
    }
    if(ar<=mid)
    {

        return get(l,mid,al,ar,v,place*2);
    }

    return get(l,mid,al,mid,v,place*2)+get(mid,r,mid,ar,v,place*2+1);;

}

int main()
{
    init();
    long long  op,l,r,v;
    for(int i=0; i<m; i++)
    {
        op=read();
        if(op==1)
        {
            l=read();
            r=read();
            v=read();
            l--;
            add(0,rn,l,r,v,1);
        }
        else
        {
            l=read();
            r=read();
            cout<<get(0,rn,l-1,r,0,1)<<endl;;
        }
        /*for(int i=1;i<=2*rn;i++){
    cout<<xds[i]<<" ";
    }cout<<endl;for(int i=1;i<=2*rn;i++){
    cout<<lazy_tag[i]<<" ";
    }cout<<endl;*/

    }

}

|