0pts求调

P3372 【模板】线段树 1

YZMX @ 2024-02-12 10:36:32

#include<iostream>
#include<algorithm>
#include<cstring>
#define in long long
using namespace std;
const int N=1e5+5;

int ls(int x){
    return x<<1;
} 
int rs(int x){
    return x<<1|1; 
}

int tr[4*N],a[N],tag[4*N],n,m;

void push_up(int p){
    tr[p]=tr[ls(p)]+tr[rs(p)];
}

void build(int p,int pl,int pr){
    tag[p]=0;
    if(pl==pr){
        tr[p]=a[pl];
        return;
    }
    int mid=(pl+pr)/2;
    build(ls(p),pl,mid);
    build(rs(p),mid+1,pr);
    push_up(p);
}

void addtag(int p,int pl,int pr,int d){
    tag[p]+=d;
    tr[p]+=d*(pr-pl+1);
}

void push_down(int p,int pl,int pr){
    if(tag[p]){
        int mid=(pl+pr)/2;
        addtag(ls(p),pl,mid,tag[p]);
        addtag(rs(p),mid+1,pr,tag[p]);
        tag[p]=0;
    }
} 

void update(int L,int R,int p,int pl,int pr,int d){
    if(L<=pr&&pr<=R){
        addtag(p,pl,pr,d);
        return;
    }
    push_down(p,pl,pr);
    int mid=(pl+pr)/2;
    if(L<=mid) update(L,R,ls(p),pl,mid,d);
    if(R>mid) update(L,R,rs(p),mid+1,pr,d);
    push_up(p);
}

int query(int L,int R,int p,int pl,int pr){
    if(pl>=L&&R>=pr){
        return tr[p];
    }
    push_down(p,pl,pr);
    int res=0;
    int mid=(pl+pr)/2;
    if(L<=mid) res+=query(L,R,ls(p),pl,mid);
    if(R>mid) res+=query(L,R,rs(p),mid+1,pr);
    return res;
}

signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    } 
    build(1,1,n);
    while(m--){
        int op,x,y,k;
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            update(x,y,1,1,n,k);
        }else{
            cin>>x>>y;
            cout<<query(x,y,1,1,n)<<endl;
        }
    }

    return 0;
}

by lovely_hyzhuo @ 2024-02-12 11:03:10

你的update有点问题,具体问题是啥不清楚


by lovely_hyzhuo @ 2024-02-12 11:03:43

if(L<=pr&&pr<=R){

by lovely_hyzhuo @ 2024-02-12 11:04:46

@YZMX 还有,

#define in long long

by lovely_hyzhuo @ 2024-02-12 11:05:03

#include<iostream>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int N=1e5+5;

int ls(int x){
    return x<<1;
} 
int rs(int x){
    return x<<1|1; 
}

int tr[4*N],a[N],tag[4*N],n,m;

void push_up(int p){
    tr[p]=tr[ls(p)]+tr[rs(p)];
}

void build(int p,int pl,int pr){
    tag[p]=0;
    if(pl==pr){
        tr[p]=a[pl];
        return;
    }
    int mid=(pl+pr)/2;
    build(ls(p),pl,mid);
    build(rs(p),mid+1,pr);
    push_up(p);
}

void addtag(int p,int pl,int pr,int d){
    tag[p]+=d;
    tr[p]+=d*(pr-pl+1);
}

void push_down(int p,int pl,int pr){
    if(tag[p]){
        int mid=(pl+pr)/2;
        addtag(ls(p),pl,mid,tag[p]);
        addtag(rs(p),mid+1,pr,tag[p]);
        tag[p]=0;
    }
} 

void update(int L,int R,int p,int pl,int pr,int d){
    if(L<=pl&&pr<=R){
        addtag(p,pl,pr,d);
        return;
    }
    push_down(p,pl,pr);
    int mid=(pl+pr)/2;
    if(L<=mid) update(L,R,ls(p),pl,mid,d);
    if(R>mid) update(L,R,rs(p),mid+1,pr,d);
    push_up(p);
}

int query(int L,int R,int p,int pl,int pr){
    if(pl>=L&&R>=pr){
        return tr[p];
    }
    push_down(p,pl,pr);
    int res=0;
    int mid=(pl+pr)/2;
    if(L<=mid) res+=query(L,R,ls(p),pl,mid);
    if(R>mid) res+=query(L,R,rs(p),mid+1,pr);
    return res;
}

signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    } 
    build(1,1,n);
    while(m--){
        int op,x,y,k;
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            update(x,y,1,1,n,k);
        }else{
            cin>>x>>y;
            cout<<query(x,y,1,1,n)<<endl;
        }
    }

    return 0;
}

by YZMX @ 2024-02-12 11:31:21

@lovely_hyzhuo 谢谢谢谢佬,我代码能力不太好


|