线段树模板

线段树是一种支持区间修改和区间查询的数据结构, 详细介绍可以参考算法学习笔记(14): 线段树, 下面记录几种使用过的模板:

  • 区间加+查询区间求和
  • 区间更新+查询区间极小值
  • 区间加+查询区间极小值

注意模板线段树函数中的下标都是从1开始, 用于初始化的数组下标从0开始

区间加+查询区间求和

cpp 复制代码
class SegmentTree {
public:
    typedef long long ll;

    inline void push_down(ll index) {
        st[index << 1].lazy = 1;
        st[index << 1 | 1].lazy = 1;
        st[index << 1].mark += st[index].mark;
        st[index << 1 | 1].mark += st[index].mark;
        st[index << 1].s += st[index].mark * (st[index << 1].tr - st[index << 1].tl + 1);
        st[index << 1 | 1].s += st[index].mark * (st[index << 1 | 1].tr - st[index << 1 | 1].tl + 1);

        st[index].lazy = 0;
        st[index].mark = 0;
    }

    inline void push_up(ll index) {
        st[index].s = st[index << 1].s + st[index << 1 | 1].s;
    }

    SegmentTree(vector<int> &init_list) {
        st = vector<SegmentTreeNode>(init_list.size() * 4 + 10);
        build(init_list, 1, init_list.size());
    }

    void build(vector<int> &init_list, ll l, ll r, ll index = 1) {
        st[index].tl = l;
        st[index].tr = r;
        st[index].lazy = 0;
        st[index].mark = 0;
        if (l == r) {
            st[index].s = init_list[l - 1];
        } else {
            ll mid = (l + r) >> 1;
            build(init_list, l, mid, index << 1);
            build(init_list, mid + 1, r, index << 1 | 1);
            push_up(index);
        }
    }

    void add(ll l, ll r, ll d, ll index = 1) {
        if (l > st[index].tr or r < st[index].tl)
            return;
        else if (l <= st[index].tl and st[index].tr <= r) {
            st[index].s += (st[index].tr - st[index].tl + 1) * d;
            st[index].mark += d;
            st[index].lazy = 1;
        } else {
            if (st[index].lazy)
                push_down(index);
            add(l, r, d, index << 1);
            add(l, r, d, index << 1 | 1);
            push_up(index);
        }
    }

    ll query(ll l, ll r, ll index = 1) {
        if (l <= st[index].tl and st[index].tr <= r) {
            return st[index].s;
        } else {
            if (st[index].lazy)
                push_down(index);
            if (r <= st[index << 1].tr)
                return query(l, r, index << 1);
            else if (l > st[index << 1].tr)
                return query(l, r, index << 1 | 1);
            return query(l, r, index << 1) + query(l, r, index << 1 | 1);
        }
    }

private:
    struct SegmentTreeNode {
        ll tl;
        ll tr;
        ll s;
        ll mark;
        int lazy;
    };
    vector<SegmentTreeNode> st;
};

区间更新+查询区间极小值

这里的区间更新等效于执行这样的操作: l i [ k ] = m i n ( l i [ k ] , v a l ) , l e f t ≤ k ≤ r i g h t li[k]=min(li[k],val) , left \le k \le right li[k]=min(li[k],val),left≤k≤right

cpp 复制代码
class SegmentTree {
public:
    typedef long long ll;

    inline void push_down(ll index) {
        st[index << 1].lazy = 1;
        st[index << 1 | 1].lazy = 1;
        st[index << 1].mark = min(st[index << 1].mark, st[index].mark);
        st[index << 1 | 1].mark = min(st[index << 1 | 1].mark, st[index].mark);
        st[index << 1].s = min(st[index << 1].s, st[index].mark);
        st[index << 1 | 1].s = min(st[index << 1 | 1].s, st[index].mark);
        st[index].lazy = 0;
    }

    inline void push_up(ll index) {
        st[index].s = min(st[index << 1].s, st[index << 1 | 1].s);
    }

    SegmentTree(vector<int> &init_list) {
        st = vector<SegmentTreeNode>(init_list.size() * 4 + 10);
        build(init_list, 1, init_list.size());
    }

    void build(vector<int> &init_list, ll l, ll r, ll index = 1) {
        st[index].tl = l;
        st[index].tr = r;
        st[index].lazy = 0;
        st[index].mark = INT64_MAX;
        if (l == r) {
            st[index].s = init_list[l - 1];
        } else {
            ll mid = (l + r) >> 1;
            build(init_list, l, mid, index << 1);
            build(init_list, mid + 1, r, index << 1 | 1);
            push_up(index);
        }
    }

    void modify(ll l, ll r, ll val, ll index = 1) {
        if (l > st[index].tr or r < st[index].tl)
            return;
        else if (l <= st[index].tl and st[index].tr <= r) {
            st[index].s = min(st[index].s, val);
            st[index].mark = min(val, st[index].mark);
            st[index].lazy = 1;
        } else {
            if (st[index].lazy)
                push_down(index);
            modify(l, r, val, index << 1);
            modify(l, r, val, index << 1 | 1);
            push_up(index);
        }
    }

    ll query(ll l, ll r, ll index = 1) {
        if (l <= st[index].tl and st[index].tr <= r) {
            return st[index].s;
        } else {
            if (st[index].lazy)
                push_down(index);
            if (r <= st[index << 1].tr)
                return query(l, r, index << 1);
            else if (l > st[index << 1].tr)
                return query(l, r, index << 1 | 1);
            return min(query(l, r, index << 1), query(l, r, index << 1 | 1));
        }
    }

private:
    struct SegmentTreeNode {
        ll tl;
        ll tr;
        ll s;
        ll mark;
        int lazy;
    };
    vector<SegmentTreeNode> st;
};

区间加+查询区间极小值

cpp 复制代码
class SegmentTree {
public:
    typedef long long ll;

    inline void push_down(ll index) {
        st[index << 1].lazy = 1;
        st[index << 1 | 1].lazy = 1;
        st[index << 1].mark += st[index].mark;
        st[index << 1 | 1].mark += st[index].mark;
        st[index << 1].s += st[index].mark;
        st[index << 1 | 1].s += st[index].mark;
        st[index].lazy = 0;
        st[index].mark = 0;
    }

    inline void push_up(ll index) {
        st[index].s = min(st[index << 1].s, st[index << 1 | 1].s);
    }

    SegmentTree(vector<int> &init_list) {
        st = vector<SegmentTreeNode>(init_list.size() * 4 + 10);
        build(init_list, 1, init_list.size());
    }

    void build(vector<int> &init_list, ll l, ll r, ll index = 1) {
        st[index].tl = l;
        st[index].tr = r;
        st[index].lazy = 0;
        st[index].mark = INT64_MAX;
        if (l == r) {
            st[index].s = init_list[l - 1];
        } else {
            ll mid = (l + r) >> 1;
            build(init_list, l, mid, index << 1);
            build(init_list, mid + 1, r, index << 1 | 1);
            push_up(index);
        }
    }

    void add(ll l, ll r, ll d, ll index = 1) {
        if (l > st[index].tr or r < st[index].tl)
            return;
        else if (l <= st[index].tl and st[index].tr <= r) {
            st[index].s += d;
            st[index].mark += d;
            st[index].lazy = 1;
        } else {
            if (st[index].lazy)
                push_down(index);
            add(l, r, d, index << 1);
            add(l, r, d, index << 1 | 1);
            push_up(index);
        }
    }

    ll query(ll l, ll r, ll index = 1) {
        if (l <= st[index].tl and st[index].tr <= r) {
            return st[index].s;
        } else {
            if (st[index].lazy)
                push_down(index);
            if (r <= st[index << 1].tr)
                return query(l, r, index << 1);
            else if (l > st[index << 1].tr)
                return query(l, r, index << 1 | 1);
            return min(query(l, r, index << 1), query(l, r, index << 1 | 1));
        }
    }

private:
    struct SegmentTreeNode {
        ll tl;
        ll tr;
        ll s;
        ll mark;
        int lazy;
    };
    vector<SegmentTreeNode> st;
};
相关推荐
会员源码网15 小时前
使用`mysql_*`废弃函数(PHP7+完全移除,导致代码无法运行)
后端·算法
木心月转码ing16 小时前
Hot100-Day10-T438T438找到字符串中所有字母异位词
算法
HelloReader17 小时前
Wi-Fi CSI 感知技术用无线信号“看见“室内的人
算法
颜酱19 小时前
二叉树分解问题思路解题模式
javascript·后端·算法
qianpeng89720 小时前
水声匹配场定位原理及实验
算法
董董灿是个攻城狮1 天前
AI视觉连载8:传统 CV 之边缘检测
算法
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员2 天前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish2 天前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱2 天前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法