线段树模板(新)

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pushup(rt) t[rt] = t[rt<<1] + t[rt<<1|1];
const int maxn = 100000+10;
typedef long long ll;
ll t[maxn<<2],lazy[maxn<<2],a[maxn];

void build(int l,int r,int rt)
{
    lazy[rt] = 0;
    if(l==r)
    {
        scanf("%lld",&t[rt]);
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}


void pushdown(int l,int r,int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1] += lazy[rt];
        lazy[rt<<1|1] += lazy[rt];
        t[rt<<1] += l*lazy[rt];
        t[rt<<1|1] += r*lazy[rt];
        lazy[rt] = 0;
    }
}
void update(int L,int R,int C,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        t[rt] += (r-l+1)*C;
        lazy[rt] += C;
        return ;
    }
    int m = (l+r)>>1;
    pushdown(m-l+1,r-m,rt);
    if(L<=m)
        update(L,R,C,lson);
    if(R>m)
        update(L,R,C,rson);
    pushup(rt);
}

ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return t[rt];
    int m = (l+r)>>1;
    pushdown(m-l+1,r-m,rt);
    ll ans = 0;
    if(L<=m)
        ans += query(L,R,lson);
    if(R>m)
        ans += query(L,R,rson);
    return ans;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值