线段树模板

线段树是一种支持区间修改和区间查询的数据结构, 详细介绍可以参考算法学习笔记(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;
};
相关推荐
sin_hielo11 分钟前
leetcode 2110
数据结构·算法·leetcode
Jay200211119 分钟前
【机器学习】33 强化学习 - 连续状态空间(DQN算法)
人工智能·算法·机器学习
panzer_maus29 分钟前
归并排序的简单介绍
java·数据结构·算法
獭.獭.29 分钟前
C++ -- STL【unordered_set和unordered_map的使用】
c++·stl·unordered_map·unordered_set
摆烂且佛系43 分钟前
B+树的“页分裂“机制
数据结构·b树
cici158741 小时前
二值化断裂裂缝的智能拼接算法
人工智能·算法·计算机视觉
麦格芬2301 小时前
LeetCode 763 划分字母区间
算法·leetcode·职场和发展
star _chen1 小时前
C++ std::move()详解:从小白到高手
开发语言·c++
福尔摩斯张2 小时前
C++核心特性精讲:从C语言痛点出发,掌握现代C++编程精髓(超详细)
java·linux·c语言·数据结构·c++·驱动开发·算法
charlie1145141912 小时前
如何快速在 VS2026 上使用 C++ 模块 — 完整上手指南
开发语言·c++·笔记·学习·现代c++