码迷,mamicode.com
首页 > 其他好文 > 详细

模板 - 数据结构 - 线段树/SegmentTree

时间:2019-11-17 17:49:53      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:pushd   线段   query   lse   +=   turn   date   hup   lazy   

struct SegmentTree {
#define ls (o<<1)
#define rs (o<<1|1)
    static const int MAXN = 100000;
    ll a[MAXN + 5];
    ll st[(MAXN << 2) + 5];

    void PushUp(int o) {
        st[o] = st[ls] + st[rs];
    }

    void Build(int o, int l, int r) {
        if(l == r)
            st[o] = a[l];
        else {
            int m = l + r >> 1;
            Build(ls, l, m);
            Build(rs, m + 1, r);
            PushUp(o);
        }
        lazy[o] = 0;
    }

    void Update(int o, int l, int r, int p, ll v) {
        if(l == r) {
            st[o] += v;
            return;
        } else {
            int m = l + r >> 1;
            if(p <= m)
                Update(ls, l, m, p, v);
            if(p >= m + 1)
                Update(rs, m + 1, r, p, v);
            PushUp(o);
        }
    }

    ll Query(int o, int l, int r, int ql, int qr) {
        if(ql <= l && r <= qr) {
            return st[o];
        } else {
            int m = l + r >> 1;
            ll res = 0;
            if(ql <= m)
                res = res + Query(ls, l, m, ql, qr);
            if(qr >= m + 1)
                res = res + Query(rs, m + 1, r, ql, qr);
            return res;
        }
    }
#undef ls
#undef rs
};
struct SegmentTree {
#define ls (o<<1)
#define rs (o<<1|1)
    static const int MAXN = 100000;
    ll a[MAXN + 5];
    ll st[(MAXN << 2) + 5], lazy[(MAXN << 2) + 5];

    void PushUp(int o) {
        st[o] = st[ls] + st[rs];
    }

    void PushDown(int o, int l, int r) {
        if(lazy[o]) {
            lazy[ls] += lazy[o];
            lazy[rs] += lazy[o];
            int m = l + r >> 1;
            st[ls] += lazy[o] * (m - l + 1);
            st[rs] += lazy[o] * (r - m);
            lazy[o] = 0;
        }
    }

    void Build(int o, int l, int r) {
        if(l == r)
            st[o] = a[l];
        else {
            int m = l + r >> 1;
            Build(ls, l, m);
            Build(rs, m + 1, r);
            PushUp(o);
        }
        lazy[o] = 0;
    }

    void Update(int o, int l, int r, int ql, int qr, ll v) {
        if(ql <= l && r <= qr) {
            lazy[o] += v;
            st[o] += v * (r - l + 1);
            return;
        } else {
            PushDown(o, l, r);
            int m = l + r >> 1;
            if(ql <= m)
                Update(ls, l, m, ql, qr, v);
            if(qr >= m + 1)
                Update(rs, m + 1, r, ql, qr, v);
            PushUp(o);
        }
    }

    ll Query(int o, int l, int r, int ql, int qr) {
        if(ql <= l && r <= qr) {
            return st[o];
        } else {
            PushDown(o, l, r);
            int m = l + r >> 1;
            ll res = 0;
            if(ql <= m)
                res = res + Query(ls, l, m, ql, qr);
            if(qr >= m + 1)
                res = res + Query(rs, m + 1, r, ql, qr);
            return res;
        }
    }
#undef ls
#undef rs
};

模板 - 数据结构 - 线段树/SegmentTree

标签:pushd   线段   query   lse   +=   turn   date   hup   lazy   

原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11877098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!