线段树C++模板

例题:洛谷3372:[模板]线段树1

应用情景

线段树在广义上被归类为二叉搜索树,适用于满足于区间加法的问题。

实现

节点结构

struct node{
    int l,r;
    long long val,lazy;
}tree[1000000];

通过数组进行建树

void build(int p,int l,int r){
    tree[p].l=l;
    tree[p].r=r;
    tree[p].lazy=0;
    if(l==r) {
        tree[p].val = arr[l];
        return;
    }
    int mid=(l+r)>>1;
    build(2*p,l,mid);
    build(2*p+1,mid+1,r);
    tree[p].val=tree[2*p].val+tree[2*p+1].val;

}

对节点的lazy标记进行下传操作

//下传lazy标记
void spread(int p){
    if(tree[p].lazy){
        tree[2*p].val+=(tree[2*p].r-tree[2*p].l+1)*tree[p].lazy;
        tree[2*p].lazy+=tree[p].lazy;
        tree[2*p+1].val+=(tree[2*p+1].r-tree[2*p+1].l+1)*tree[p].lazy;
        tree[2*p+1].lazy+=tree[p].lazy;
        tree[p].lazy=0;
    }
}

对区间进行修改

//区间修改
void change(int p,int l, int r,int x){
    //如果覆盖,打上lazy标记
    if(tree[p].l>=l && tree[p].r<=r){
        tree[p].val+=x*(tree[p].r-tree[p].l+1);
        tree[p].lazy+=x;
        return;
    }
    spread(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid<r)
        change(2*p+1,l,r,x);
    if(mid>=l)
        change(2*p,l,r,x);
    tree[p].val=tree[p*2].val+tree[p*2+1].val;
}

搜索区间和

long long search(int p,int l,int r){
    if(tree[p].l>=l && tree[p].r<=r)
        return tree[p].val;
    long long ans=0;
    spread(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid<r)
        ans+= search(2*p+1,l,r);
    if(mid>=l)
        ans+= search(p*2,l,r);
    return ans;

}

完整代码

#include<bits/stdc++.h>
using namespace std;
struct node{
    int l,r;
    long long val,lazy;
}tree[1000000];
int arr[1000000];

//建树操作
void build(int p,int l,int r){
    tree[p].l=l;
    tree[p].r=r;
    tree[p].lazy=0;
    if(l==r) {
        tree[p].val = arr[l];
        return;
    }
    int mid=(l+r)>>1;
    build(2*p,l,mid);
    build(2*p+1,mid+1,r);
    tree[p].val=tree[2*p].val+tree[2*p+1].val;

}


//下传lazy标记
void spread(int p){
    if(tree[p].lazy){
        tree[2*p].val+=(tree[2*p].r-tree[2*p].l+1)*tree[p].lazy;
        tree[2*p].lazy+=tree[p].lazy;
        tree[2*p+1].val+=(tree[2*p+1].r-tree[2*p+1].l+1)*tree[p].lazy;
        tree[2*p+1].lazy+=tree[p].lazy;
        tree[p].lazy=0;
    }
}

//区间修改
void change(int p,int l, int r,int x){
    //如果覆盖,打上lazy标记
    if(tree[p].l>=l && tree[p].r<=r){
        tree[p].val+=x*(tree[p].r-tree[p].l+1);
        tree[p].lazy+=x;
        return;
    }
    spread(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid<r)
        change(2*p+1,l,r,x);
    if(mid>=l)
        change(2*p,l,r,x);
    tree[p].val=tree[p*2].val+tree[p*2+1].val;
}

long long search(int p,int l,int r){
    if(tree[p].l>=l && tree[p].r<=r)
        return tree[p].val;
    long long ans=0;
    spread(p);
    int mid=(tree[p].l+tree[p].r)>>1;
    if(mid<r)
        ans+= search(2*p+1,l,r);
    if(mid>=l)
        ans+= search(p*2,l,r);
    return ans;

}


int main(){
    int n,m;
    cin >> n >>m;

    for(int i=1;i<=n;i++)
        cin >> arr[i];
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int q,x,y,z;
        cin>>q;
        if(q==1){
            cin>>x>>y>> z;
            change(1,x,y,z);
        }
        else {
            cin>>x>>y;
            cout<<search(1,x,y)<<endl;
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰宝IWZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值