跳到正文

算法·理论

吉司机线段树(Segment Tree Beats)算法详解

dyc222约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年3月1日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 15 分钟

版本

  1. v1当前

吉司机线段树(Segment Tree Beats)算法详解

引言

吉司机线段树(Segment Tree Beats)是一种强大的数据结构,由吉如一(吉司机)在2016年提出。它是对传统线段树的扩展,能够处理一些传统线段树无法高效解决的问题,尤其是涉及区间最值操作的场景。本文将详细介绍吉司机线段树的原理、实现及其应用。


1. 传统线段树的局限性

传统线段树是一种用于处理区间查询和区间更新的数据结构,支持以下操作:

  • 区间查询:查询某个区间的某种属性(如区间和、区间最大值等)。
  • 区间更新:对某个区间进行某种操作(如区间加、区间赋值等)。

然而,传统线段树在处理某些复杂操作时效率较低,例如:

  • 区间取最值操作:将区间内的所有元素与某个值取最小值或最大值。
  • 区间历史最值查询:查询某个区间在历史操作中的最大值或最小值。

吉司机线段树通过引入“势能分析”和“懒标记优化”等方法,解决了这些问题。


2. 吉司机线段树的核心思想

吉司机线段树的核心思想是通过势能分析和懒标记优化,将复杂操作的复杂度控制在可接受的范围内。具体来说,它通过以下方式实现:

  1. 势能分析:

    • 每个节点维护一个“势能”值,表示该节点的子树中需要处理的复杂操作的数量。
    • 通过势能分析,确保每次操作的复杂度不会过高。
  2. 懒标记优化:

    • 引入多种懒标记,分别表示不同的操作(如区间加、区间取最值等)。
    • 通过懒标记的合并和下传,减少不必要的递归调用。
  3. 剪枝策略:

    • 在递归过程中,如果发现某个节点的子树已经满足某些条件,则直接剪枝,避免进一步递归。

3. 吉司机线段树的基本操作

吉司机线段树支持以下操作:

3.1 区间取最值操作

  • 操作描述: 将区间 [l,r][l, r] 内的所有元素与某个值 xx 取最小值或最大值。
  • 实现方法:
    • 在每个节点维护区间的最小值和次小值(或最大值和次大值)。
    • 当递归到某个节点时,如果 xx 小于该节点的最小值(或大于最大值),则直接剪枝。
    • 否则,递归处理子节点。

3.2 区间历史最值查询

  • 操作描述: 查询某个区间在历史操作中的最大值或最小值。
  • 实现方法:
    • 在每个节点维护当前值的历史最值。
    • 在每次更新操作时,同步更新历史最值。

3.3 区间加操作

  • 操作描述: 对区间 [l,r][l, r] 内的所有元素加上某个值 xx。
  • 实现方法:
    • 使用传统的懒标记方法实现。

4. 吉司机线段树的实现

以下是吉司机线段树的 C++ 实现代码:

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int INF = 1e9;

struct Node {
    int min1, min2; // 最小值和次小值
    int max1, max2; // 最大值和次大值
    int cntMin, cntMax; // 最小值和最大值的个数
    int lazyAdd; // 区间加的懒标记
    int lazyMin, lazyMax; // 区间取最值的懒标记
};

class SegmentTreeBeats {
private:
    int n;
    vector<Node> tree;

    // 合并两个节点的信息
    void merge(int p) {
        int l = p * 2, r = p * 2 + 1;
        tree[p].min1 = min(tree[l].min1, tree[r].min1);
        tree[p].min2 = min(tree[l].min1 == tree[p].min1 ? tree[l].min2 : tree[l].min1,
                          tree[r].min1 == tree[p].min1 ? tree[r].min2 : tree[r].min1);
        tree[p].cntMin = (tree[l].min1 == tree[p].min1 ? tree[l].cntMin : 0) +
                         (tree[r].min1 == tree[p].min1 ? tree[r].cntMin : 0);

        tree[p].max1 = max(tree[l].max1, tree[r].max1);
        tree[p].max2 = max(tree[l].max1 == tree[p].max1 ? tree[l].max2 : tree[l].max1,
                          tree[r].max1 == tree[p].max1 ? tree[r].max2 : tree[r].max1);
        tree[p].cntMax = (tree[l].max1 == tree[p].max1 ? tree[l].cntMax : 0) +
                         (tree[r].max1 == tree[p].max1 ? tree[r].cntMax : 0);
    }

    // 下传懒标记
    void pushDown(int p) {
        int l = p * 2, r = p * 2 + 1;
        if (tree[p].lazyAdd) {
            tree[l].min1 += tree[p].lazyAdd;
            tree[l].min2 += tree[p].lazyAdd;
            tree[l].max1 += tree[p].lazyAdd;
            tree[l].max2 += tree[p].lazyAdd;
            tree[l].lazyAdd += tree[p].lazyAdd;

            tree[r].min1 += tree[p].lazyAdd;
            tree[r].min2 += tree[p].lazyAdd;
            tree[r].max1 += tree[p].lazyAdd;
            tree[r].max2 += tree[p].lazyAdd;
            tree[r].lazyAdd += tree[p].lazyAdd;

            tree[p].lazyAdd = 0;
        }
        if (tree[p].lazyMin != INF) {
            if (tree[l].max1 > tree[p].lazyMin) {
                tree[l].max1 = tree[p].lazyMin;
                if (tree[l].min1 == tree[l].max1) tree[l].min1 = tree[p].lazyMin;
            }
            if (tree[r].max1 > tree[p].lazyMin) {
                tree[r].max1 = tree[p].lazyMin;
                if (tree[r].min1 == tree[r].max1) tree[r].min1 = tree[p].lazyMin;
            }
            tree[p].lazyMin = INF;
        }
        if (tree[p].lazyMax != -INF) {
            if (tree[l].min1 < tree[p].lazyMax) {
                tree[l].min1 = tree[p].lazyMax;
                if (tree[l].max1 == tree[l].min1) tree[l].max1 = tree[p].lazyMax;
            }
            if (tree[r].min1 < tree[p].lazyMax) {
                tree[r].min1 = tree[p].lazyMax;
                if (tree[r].max1 == tree[r].min1) tree[r].max1 = tree[p].lazyMax;
            }
            tree[p].lazyMax = -INF;
        }
    }

public:
    SegmentTreeBeats(int size) {
        n = size;
        tree.resize(4 * n);
        build(1, 1, n);
    }

    // 构建线段树
    void build(int p, int l, int r) {
        if (l == r) {
            tree[p].min1 = tree[p].max1 = 0;
            tree[p].min2 = INF;
            tree[p].max2 = -INF;
            tree[p].cntMin = tree[p].cntMax = 1;
            tree[p].lazyAdd = 0;
            tree[p].lazyMin = INF;
            tree[p].lazyMax = -INF;
            return;
        }
        int mid = (l + r) / 2;
        build(p * 2, l, mid);
        build(p * 2 + 1, mid + 1, r);
        merge(p);
    }

    // 区间加操作
    void rangeAdd(int p, int l, int r, int ql, int qr, int val) {
        if (ql > r || qr < l) return;
        if (ql <= l && r <= qr) {
            tree[p].min1 += val;
            tree[p].min2 += val;
            tree[p].max1 += val;
            tree[p].max2 += val;
            tree[p].lazyAdd += val;
            return;
        }
        pushDown(p);
        int mid = (l + r) / 2;
        rangeAdd(p * 2, l, mid, ql, qr, val);
        rangeAdd(p * 2 + 1, mid + 1, r, ql, qr, val);
        merge(p);
    }

    // 区间取最小值操作
    void rangeMin(int p, int l, int r, int ql, int qr, int val) {
        if (ql > r || qr < l || tree[p].max1 <= val) return;
        if (ql <= l && r <= qr && tree[p].max2 < val) {
            tree[p].max1 = val;
            if (tree[p].min1 == tree[p].max1) tree[p].min1 = val;
            tree[p].lazyMin = val;
            return;
        }
        pushDown(p);
        int mid = (l + r) / 2;
        rangeMin(p * 2, l, mid, ql, qr, val);
        rangeMin(p * 2 + 1, mid + 1, r, ql, qr, val);
        merge(p);
    }

    // 区间取最大值操作
    void rangeMax(int p, int l, int r, int ql, int qr, int val) {
        if (ql > r || qr < l || tree[p].min1 >= val) return;
        if (ql <= l && r <= qr && tree[p].min2 > val) {
            tree[p].min1 = val;
            if (tree[p].max1 == tree[p].min1) tree[p].max1 = val;
            tree[p].lazyMax = val;
            return;
        }
        pushDown(p);
        int mid = (l + r) / 2;
        rangeMax(p * 2, l, mid, ql, qr, val);
        rangeMax(p * 2 + 1, mid + 1, r, ql, qr, val);
        merge(p);
    }

    // 区间查询最小值
    int queryMin(int p, int l, int r, int ql, int qr) {
        if (ql > r || qr < l) return INF;
        if (ql <= l && r <= qr) return tree[p].min1;
        pushDown(p);
        int mid = (l + r) / 2;
        return min(queryMin(p * 2, l, mid, ql, qr),
                   queryMin(p * 2 + 1, mid + 1, r, ql, qr));
    }

    // 区间查询最大值
    int queryMax(int p, int l, int r, int ql, int qr) {
        if (ql > r || qr < l) return -INF;
        if (ql <= l && r <= qr) return tree[p].max1;
        pushDown(p);
        int mid = (l + r) / 2;
        return max(queryMax(p * 2, l, mid, ql, qr),
                   queryMax(p * 2 + 1, mid + 1, r, ql, qr));
    }
};

5. 吉司机线段树的应用

吉司机线段树广泛应用于以下场景:

  1. 区间最值操作:
    • 将区间内的所有元素与某个值取最小值或最大值。
  2. 区间历史最值查询:
    • 查询某个区间在历史操作中的最大值或最小值。
  3. 复杂区间更新:
    • 支持多种区间更新操作(如区间加、区间赋值、区间取最值等)。

6. 总结

吉司机线段树是一种功能强大的数据结构,能够高效处理传统线段树无法解决的复杂问题。通过势能分析和懒标记优化,它在保证时间复杂度的同时,提供了丰富的操作支持。掌握吉司机线段树,将为解决复杂的区间操作问题提供强有力的工具。


参考文献:

  1. 吉如一. (2016). Segment Tree Beats.
  2. Codeforces Blog: Segment Tree Beats
原文