线段树_模板

来源:互联网 发布:我是大美人 淘宝 编辑:程序博客网 时间:2024/05/16 15:50
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 111111
int tree[maxn<<2];
int col[maxn<<2];
int f;
void pushup(int rt)
{
         tree[rt]=tree[rt<<1]+tree[rt<<1|1];
        //max[rt]=max[rt<<1|1];
}
void pushdown(int rt,int m)
{
    if(col[rt])
    {
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        tree[rt<<1]+=(m-(m>>1))*col[rt];
        tree[rt<<1|1]+=(m>>1)*col[rt];
        col[rt]=0;
    }
}
void update(int L,int R,int sc,int l,int r,int rt)
{
    int m;
    if(L<=l&&r<=R)
    {
        col[rt]+=sc;
        tree[rt]+=sc*(r-l+1);
        return ;
    }
    pushdown(rt,r-l+1);
    m=(l+r)>>1;
    if(L<=m)
        update(L,R,sc,lson);
    if(R>m)
        update(L,R,sc,rson);
    pushup(rt);
}

int query(int L,int R,int l,int r,int rt)
{
    int m;
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    pushdown(rt,r-l+1);//)
    m=(l+r)>>1;
    int ret=0;
    if(L<=m)
        ret+=query(L,R,lson);
    if(R>m)
        ret+=query(L,R,rson);
    return ret;
}
0 0
原创粉丝点击