救救孩子!!!在线急等

P3372 【模板】线段树 1

Vuglar_zuo @ 2023-08-06 16:07:36

样例没过QWQ

#include<bits/stdc++.h>
using namespace std;
struct data
{
    long long l;
    long long r;
    long long x;
};
data total[400004];
void build(long long l,long long r,long long id)
{
    total[id].l=l;
    total[id].r=r;
    if(l==r)
    {
        cin>>total[id].x;
        return;
    }
    long long mid=(l+r)/2;
    build(l,mid,id*2);
    build(mid+1,r,id*2+1);
    total[id].x=(total[id*2].x+total[id*2+1].x);
}

void updatetree(int x,int y,int id,int k)
{
    if(total[id].l==total[id].r)
    {
        total[id].x=total[id].x+k;
        return;
    }
    int mid=(total[id].l+total[id].r)/2;
    if(x<=mid)
    {
        updatetree(x,y,id*2,k);
    }
    else
    {
        updatetree(x,y,id*2+1,k);   
    }
    int ans1=total[id*2].x;
    int ans2=total[id*2+1].x;
    total[id].x=max(ans1,ans2);
}

long long tree1(long long l,long long r,long long id)
{
    if(total[id].l>=l&&total[id].r<=r)
    {
        return total[id].x;
    }
    long long maxx=0;
    long long mid=(total[id].l+total[id].r)/2;
    if(l<=mid)maxx+=(tree1(l,r,id*2));
    if(r>mid)maxx+=(tree1(l,r,id*2+1));
    return maxx;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    long long n,m,a,b,c,k;
    cin>>n>>m;
    build(1,n,1);
    for(long long i=1;i<=m;i++)
    {
        cin>>c;
        if(c==1)
        {
            cin>>a>>b>>k;
            updatetree(a,b,1,k);
        }
        else if(c==2)
        {
            cin>>a>>b;
            cout<<tree1(a,b,1)<<endl;
        }
    }
    return 0;
}

by c_z_d @ 2023-08-06 16:41:30

你的 updatetree 函数是单点修改,而这道题是区间修改


by MrcFrst @ 2023-08-06 16:43:18

@oierzjc666 您是没有写线段树懒标记吗?


by Vuglar_zuo @ 2023-08-06 16:48:02

收到……


|