跳到正文

算法·理论

线段树模板 | 超级好维护 | 新手也能马上用!

twyeottk约 15 分钟
原文

这是 2025年12月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年11月23日
收录
2025年12月12日
更新
2026年10月25日
篇幅
约 15 分钟

版本

  1. v3当前
  2. v2
  3. v1

线段树模板

C++
template <typename Node,typename Lazy>
struct SGT {
    int n;
    vector<Node> st;
    vector<Lazy> lazy;

    SGT() : n(0) {}
    SGT(int _n) { init(_n); }
    template <typename T>
    SGT(const vector<T>& a) { init(int64_t(a.size())-1); if (n) build(1,1,n,a); }

    void init(int _n) {
        n=_n;
        st.assign(4*max(1ll,n),Node::init());
        lazy.assign(4*max(1ll,n),Lazy());
    }

    template <typename T>
    inline void build(int p,int l,int r,const vector<T>& a) {
        if (l==r) {
            st[p]=Node::trans(a[l]);
            return;
        }
        int m=(l+r)>>1;
        build(p<<1,l,m,a);
        build(p<<1|1,m+1,r,a);
        st[p]=Node::merge(st[p<<1],st[p<<1|1]);
    }

    inline void apply(int p,const Lazy& lz) {
        st[p]=Node::apply(st[p],lz);
        lazy[p]=Lazy::merge(lazy[p],lz);
    }

    inline void push_down(int p) {
        if (lazy[p].empty()) return ;
        apply(p<<1,lazy[p]);
        apply(p<<1|1,lazy[p]);
        lazy[p]=Lazy();
    }

    inline void update(int l,int r,const Lazy& lz) { update_er(1,1,n,l,r,lz); }

    inline void update_er(int p,int l,int r,int L,int R,const Lazy& lz) {
        if (R<l || r<L) return ;
        if (L<=l && r<=R) {
            apply(p,lz); return ;
        }
        push_down(p);
        int mid=l+r>>1;
        update_er(p<<1,l,mid,L,R,lz);
        update_er(p<<1|1,mid+1,r,L,R,lz);
        st[p]=Node::merge(st[p<<1],st[p<<1|1]);
    }

    inline Node query(int l,int r) { return query_er(1,1,n,l,r); }

    inline Node query_er(int p,int l,int r,int L,int R) {
        if (R<l || r<L) return Node::init();
        if (L<=l && r<=R) return st[p];
        push_down(p);
        int mid=(l+r)>>1;
        Node t1=query_er(p<<1,l,mid,L,R);
        Node t2=query_er(p<<1|1,mid+1,r,L,R);
        return Node::merge(t1,t2);
    }
};

struct Lazy { 
    int set;
    bool hav;

    Lazy(int _set=0,bool _hav=false) : set(_set),hav(_hav) {}
    inline bool empty() { return !hav; }

    static Lazy merge(Lazy a,Lazy b) {
        if (b.empty()) return a;
        return b; 
    }
};

struct Node {
    int minn;
    int len;
 
    Node(int _minn=0,int _len=0) : minn(_minn),len(_len) {} 

    inline static Node init() { return Node(LLONG_MAX,0); } // 无效值,在合并时该值不会使源数据变化
    inline static Node trans(int val) { return Node(val,1); } // 给出修改值,返回修改后值
    inline static Node apply(Node a,Lazy b) {
        if (b.empty()) return a;
        Node t=a;
        t.minn=b.set;
        return t;
    }
    inline static Node merge(Node a,Node b) {
        return Node(min(a.minn,b.minn),a.len+b.len);
    }
};

使用说明

概述

区间修改与区间查询,支持单线段树多字段存储。

SGT_Node

名称可自定义。

参数包含存储值。

应包含 44 个函数,要求全部加 inline static。

初始化

C++
inline static Node init() { return Node(...); }

... 中应包含 n = 结构体参数个数 个值,要求在合并时该值不会使源数据变化。例如对于求最大值的参数,初始化最好是 LLONG_MIN 或 INT_MIN;对于求和的参数,初始化最好是 0。

转义参数

C++
inline static Node trans(int val) { return Node(...); }

在 build 函数中会引用,如果有 len 参数需填 1,其余填 val 即可。

应用参数

C++
inline static Node apply(Node a,Lazy b) {
    if (b.empty()) return a;
    Node t=a;
    ...
    return t;
}

在 push_down 函数中被引用,将 lazy 值应用到线段树中。

合并函数

C++
inline static Node merge(Node a,Node b) {
    return Node(...,a.len+b.len);
}

类似于 push_up,例如取和就是 a.sum+b.sum,最大值直接写 max(a.maxn,b.maxn) 即可。

示例

C++
struct Node {
    int maxn;
    int len;
 
    Node(int _maxn=0,int _len=0) : maxn(_maxn),len(_len) {} 

    inline static Node init() { return Node(0,0); } // 无效值,在合并时该值不会使源数据变化
    inline static Node trans(int val) { return Node(val,1); } // 给出修改值,返回修改后值
    inline static Node apply(Node a,Lazy b) {
        if (b.empty()) return a;
        Node t=a;
        t.maxn=b.set;
        return t;
    }
    inline static Node merge(Node a,Node b) {
        return Node(max(a.maxn,b.maxn),a.len+b.len);
    }
};

该实例满足覆盖修改与求最大值操作。

Lazy_Node

懒标记,修改操作存储。

要哪些修改操作就有那些参数。

判空函数

C++
inline bool empty() { return ... == 初始值 && ... == 初始值; }

合并函数

C++
static Lazy merge(Lazy a,Lazy b) {
    return Lazy(...);
}

两个参数有先后之分,第一个是先,所以如果是覆盖操作直接取后即可。

示例

C++
struct Lazy { 
    int set;

    Lazy(int _set=0) : set(_set) {}
    inline bool empty() { return set==0; }

    static Lazy merge(Lazy a,Lazy b) {
        return Lazy(b.set);
    }
};

创建

创建一个名为 sgt 的线段树,数组满足 1-based (从 1 开头,使用 vector,n = vec.size()-1 )

C++
SGT<[SGT_Node],[Lazy_Node]> sgt([数组]);

创建后将自动构建,不需要再手动调用 build。

区间修改

对名为 sgt 线段树的 l,r 区间执行修改操作,修改值(Lazy_Node)为 lz。

C++
lz=Lazy(x); // 假设只有一个参数
sgt.update(l,r,lz);

区间查询

对名为 sgt 线段树的 l,r 区间执行查询操作操作,返回类型为 SGT_Node。

C++
SGT_Node result = sgt.query(l,r)

示例

该线段树支持区间覆盖与区间最大值的查询操作。

C++
template <typename Node,typename Lazy>
struct SGT {
    int n;
    vector<Node> st;
    vector<Lazy> lazy;

    SGT() : n(0) {}
    SGT(int _n) { init(_n); }
    template <typename T>
    SGT(const vector<T>& a) { init(int64_t(a.size())-1); if (n) build(1,1,n,a); }

    void init(int _n) {
        n=_n;
        st.assign(4*max(1ll,n),Node::init());
        lazy.assign(4*max(1ll,n),Lazy());
    }

    template <typename T>
    inline void build(int p,int l,int r,const vector<T>& a) {
        if (l==r) {
            st[p]=Node::trans(a[l]);
            return;
        }
        int m=(l+r)>>1;
        build(p<<1,l,m,a);
        build(p<<1|1,m+1,r,a);
        st[p]=Node::merge(st[p<<1],st[p<<1|1]);
    }

    inline void apply(int p,const Lazy& lz) {
        st[p]=Node::apply(st[p],lz);
        lazy[p]=Lazy::merge(lazy[p],lz);
    }

    inline void push_down(int p) {
        if (lazy[p].empty()) return ;
        apply(p<<1,lazy[p]);
        apply(p<<1|1,lazy[p]);
        lazy[p]=Lazy();
    }

    inline void update(int l,int r,const Lazy& lz) { update_er(1,1,n,l,r,lz); }

    inline void update_er(int p,int l,int r,int L,int R,const Lazy& lz) {
        if (R<l || r<L) return ;
        if (L<=l && r<=R) {
            apply(p,lz); return ;
        }
        push_down(p);
        int mid=l+r>>1;
        update_er(p<<1,l,mid,L,R,lz);
        update_er(p<<1|1,mid+1,r,L,R,lz);
        st[p]=Node::merge(st[p<<1],st[p<<1|1]);
    }

    inline Node query(int l,int r) { return query_er(1,1,n,l,r); }

    inline Node query_er(int p,int l,int r,int L,int R) {
        if (R<l || r<L) return Node::init();
        if (L<=l && r<=R) return st[p];
        push_down(p);
        int mid=(l+r)>>1;
        Node t1=query_er(p<<1,l,mid,L,R);
        Node t2=query_er(p<<1|1,mid+1,r,L,R);
        return Node::merge(t1,t2);
    }
};

struct Lazy { 
    int set;

    Lazy(int _set=0) : set(_set) {}
    inline bool empty() { return set==0; }

    static Lazy merge(Lazy a,Lazy b) {
        return Lazy(b.set);
    }
};

struct Node {
    int maxn;
    int len;
 
    Node(int _maxn=0,int _len=0) : maxn(_maxn),len(_len) {} 

    inline static Node init() { return Node(0,0); } // 无效值,在合并时该值不会使源数据变化
    inline static Node trans(int val) { return Node(val,1); } // 给出修改值,返回修改后值
    inline static Node apply(Node a,Lazy b) {
        if (b.empty()) return a;
        Node t=a;
        t.maxn=b.set;
        return t;
    }
    inline static Node merge(Node a,Node b) {
        return Node(max(a.maxn,b.maxn),a.len+b.len);
    }
};

后记

制作不易,不喜勿喷,感谢观看。