全 RE,刚学没多久,已哭晕在撤锁

P3372 【模板】线段树 1

Ravener @ 2024-06-11 20:23:42

#include <bits/stdc++.h>
#define ll long long
#define rt return
#define N 100005
#define lc(a) a<<1
#define rc(a) a<<1|1
using namespace std;
ll t[4*N],a[N],lazy[4*N];
void pushup(ll u){t[u]=t[lc(u)]+t[rc(u)];}
void build(ll u,ll l,ll r)
{
    if(l==r) 
    {
        t[u]=a[l];
        rt;
    }
    ll mid=l+((r-l)>>1);
    build(lc(u),l,mid);
    build(rc(u),mid+1,r);
    pushup(u);  
}
void update(ll u,ll c,ll L,ll R,ll l,ll r)
{
    if(L<=l&&r<=R)
    {
        t[u]+=(r-l+1)*c,lazy[u]+=c;
        rt;
    }
    ll mid=l+((r-l)>>1);
    if(lazy[u])
    {
        t[lc(u)]+=lazy[u]*(mid-l+1),t[rc(u)]+=lazy[u]*(r-mid);
        lazy[lc(u)]+=lazy[u],lazy[rc(u)]+=lazy[u];
        lazy[u]=0;
    }
    if(l<=mid) update(lc(u),c,L,R,l,mid);
    if(r>mid) update(rc(u),c,L,R,mid+1,r);
    pushup(u);
}
ll getsum(ll u,ll L,ll R,ll l,ll r)
{
    if(L<=l&&r<=R) rt t[u];
    ll mid=l+((r-l)>>1),sum=0;
    if(lazy[u])
    {
        t[lc(u)]+=lazy[u]*(mid-l+1),t[rc(u)]+=lazy[u]*(r-mid);
        lazy[lc(u)]+=lazy[u],lazy[rc(u)]+=lazy[u];
        lazy[u]=0;
    }
    if(l<=mid) sum=getsum(lc(u),L,R,l,mid);
    if(r>mid) sum+=getsum(rc(u),L,R,mid+1,r);
    rt sum;
}
int main()
{
    ios::sync_with_stdio(0);
    ll n,m,op,x,y,z;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,n);
    while(m--)
    {
        cin>>op>>x>>y;
        if(op==2) cout<<getsum(1,x,y,1,n)<<"\n";
        else cin>>z,update(1,z,x,y,1,n);
    }
    rt 0;
}

by Kazeno_Akina @ 2024-06-11 21:02:11

@Ravener 你要不试试把 mid 的定义式改成 mid=l+r>>1


by 2023gdgz01 @ 2024-06-11 21:02:50

@Ravener 很细微的一个错误:在所有 if(l<=mid)if(r>mid),把 lr 改成大写即可。


by Ravener @ 2024-06-11 21:12:19

@2023gdgz01 谢谢,已调好


|