使用SegmentMap查询维护区间信息
====================

FAST Kit提供Segment Map用于查询维护区间信息，实现数据序列区间段的快速更新和快速查询。线段表（Segment Map）是一种用于高效处理区间段信息的数据结构，适用于需要频繁对数据序列的某个区间段进行统计或修改的场景。其典型操作包括单点修改、区间修改、区间查询等。

线段表有多种实现方式，其中最常见的是使用二分树的方案，也被称为线段树（Segment Tree）。与直接遍历区间相比，线段表能将许多区间操作的时间复杂度从 ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/de/v3/pwJcprczQwGSufz3n77WWw/zh-cn_image_0000002664208285.png?HW-CC-KV=V1&HW-CC-Date=20260709T132446Z&HW-CC-Expire=86400&HW-CC-Sign=9A53AD1D968EEB27B4363A48540DAB14410FA852527B3B0796F6754C0407F1F2) 优化至![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/06/v3/iZV15WoFRcmr1HDdsFf55g/zh-cn_image_0000002664328345.png?HW-CC-KV=V1&HW-CC-Date=20260709T132446Z&HW-CC-Expire=86400&HW-CC-Sign=59F83543FEE5E4D99DC9B268D54336834BA373A95358A759C87ABAF7501F7AA5)，在处理大规模数据时优势显著，为构建高性能、响应迅速的应用程序提供数据结构基础。

#### 接口说明

具体API说明详见[接口文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast)。

| 名称 | 描述 |
| :-- | :-- |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_CreateConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_createconfig) ([FAST\_SegmentMapConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapconfig) \*\*config) | 创建线段表的不透明配置。 |
| FAST\_EXPORT void [HMS\_FAST\_SegmentMap\_DestroyConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_destroyconfig) ([FAST\_SegmentMapConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapconfig) \*config) | 销毁线段表的不透明配置。 |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_SetQueryType](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_setquerytype) ([FAST\_SegmentMapConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapconfig) \*config, [FAST\_SegmentMapQueryType](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapquerytype-1) type) | 设置线段表不透明配置中的查询类型。 |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_SetUpdateType](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_setupdatetype) ([FAST\_SegmentMapConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapconfig) \*config, [FAST\_SegmentMapUpdateType](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapupdatetype-1) type) | 设置线段表不透明配置中的更新类型。 |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_Create](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_create) ([FAST\_SegmentMapHandle](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmaphandle) \*handle, size\_t size, const int32\_t \*array, [FAST\_SegmentMapConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmapconfig) \*config) | 创建线段表。 |
| FAST\_EXPORT void [HMS\_FAST\_SegmentMap\_Destroy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_destroy) ([FAST\_SegmentMapHandle](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmaphandle) handle) | 销毁线段表实例，释放内存，再次调用为未定义行为。 |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_Update](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_update) ([FAST\_SegmentMapHandle](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmaphandle) handle, size\_t left, size\_t right, int32\_t value) | 更新线段表的区间，根据配置按照赋值、加法、减法等操作更新。 |
| FAST\_EXPORT [FAST\_ErrorCode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_errorcode-1) [HMS\_FAST\_SegmentMap\_Query](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#hms_fast_segmentmap_query) ([FAST\_SegmentMapHandle](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/fast-kit-fast#fast_segmentmaphandle) handle, size\_t left, size\_t right, int32\_t \*result) | 查询线段表的区间，根据配置返回最大值、最小值、求和等数据。 |

#### 开发步骤

1.  在CMake脚本中链接相关动态库。
    
    find\_library(
        lib\_fast\_ads
        NAMES fast\_ads
    )
    target\_link\_libraries(entry PRIVATE ${lib\_fast\_ads})
    
2.  调用HMS\_FAST\_SegmentMap\_CreateConfig生成线段表配置实例（FAST\_SegmentMapConfig）。
    
3.  调用HMS\_FAST\_SegmentMap\_SetQueryType设置查询类型。
    
4.  调用HMS\_FAST\_SegmentMap\_SetUpdateType设置更新类型。
    
5.  调用HMS\_FAST\_SegmentMap\_Create生成线段表实例 （FAST\_SegmentMapHandle）。生成实例之后，无法再修改查询和更新类型。
    
6.  调用HMS\_FAST\_SegmentMap\_Query进行高效区间查询操作。
    
7.  调用HMS\_FAST\_SegmentMap\_Update进行高效区间更新操作。
    
8.  调用HMS\_FAST\_SegmentMap\_Destroy销毁线段表实例。
    
9.  调用HMS\_FAST\_SegmentMap\_DestroyConfig销毁线段表配置实例。
    

#include <cassert>
#include <iostream>
#include "FASTKit/fast\_ads\_segment\_map.h"

FAST\_ErrorCode demoSegmentMapSumSet()
{
    FAST\_SegmentMapConfig \*config = nullptr;
    FAST\_SegmentMapHandle handle = nullptr;
    int32\_t \*array = nullptr;
    FAST\_ErrorCode ret;

    ret = HMS\_FAST\_SegmentMap\_CreateConfig(&config);
    if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
        return ret;
    }

    do {
        // 初始化配置
        ret = HMS\_FAST\_SegmentMap\_SetQueryType(config, FAST\_SEGMENTMAP\_QUERY\_TYPE\_SUM);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }

        ret = HMS\_FAST\_SegmentMap\_SetUpdateType(config, FAST\_SEGMENTMAP\_UPDATE\_TYPE\_SET);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }

        // 初始化数组
        size\_t size = 10;
        array = new int32\_t\[size\];
        for (size\_t i = 0; i < size; ++i) {
            array\[i\] = i + 1;
        }
        // array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

        // 创建线段表实例
        ret = HMS\_FAST\_SegmentMap\_Create(&handle, size, array, config);
        // 线段表初始化为 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }

        int32\_t result;

        // 第一次查询：查询区间\[0, 5)的求和值
        ret = HMS\_FAST\_SegmentMap\_Query(handle, 0, 5, &result);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        assert(result == 15);  // 1 + 2 + 3 + 4 + 5 = 15

        // 第一次更新：将区间\[3, 7)的值设置为-1
        ret = HMS\_FAST\_SegmentMap\_Update(handle, 3, 7, -1);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        // 线段表更新为 {1, 2, 3, -1, -1, -1, -1, 8, 9, 10}

        // 第二次查询：查询区间\[0, 5)的求和值
        ret = HMS\_FAST\_SegmentMap\_Query(handle, 0, 5, &result);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        assert(result == 4);  // 1 + 2 + 3 - 1 - 1 = 4

        // 第二次更新：将区间\[5, 9)的值设置为2
        ret = HMS\_FAST\_SegmentMap\_Update(handle, 5, 9, 2);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        // 线段表更新为 {1, 2, 3, -1, -1, 2, 2, 2, 2, 10}

        // 第三次查询：查询区间\[0, 10)的求和值
        ret = HMS\_FAST\_SegmentMap\_Query(handle, 0, 10, &result);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        assert(result == 22);  // 1 + 2 + 3 -1 -1 + 2 + 2 + 2 + 2 + 10 = 22

        // 第三次更新：将区间\[0, 3)的值设置为0
        ret = HMS\_FAST\_SegmentMap\_Update(handle, 0, 3, 0);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        // 线段表更新为 {0, 0, 0, -1, -1, 2, 2, 2, 2, 10}

        // 第四次查询：查询区间\[3, 7)的求和值
        ret = HMS\_FAST\_SegmentMap\_Query(handle, 3, 7, &result);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        assert(result == 2);  // -1 -1 + 2 + 2 = 2

        // 第四次更新：将区间\[7, 10)的值设置为5
        ret = HMS\_FAST\_SegmentMap\_Update(handle, 7, 10, 5);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        // 线段表更新为 {0, 0, 0, -1, -1, 2, 2, 5, 5, 5}

        // 第五次查询：查询区间\[0, 10)的求和值
        ret = HMS\_FAST\_SegmentMap\_Query(handle, 0, 10, &result);
        if (ret != FAST\_ERROR\_CODE\_SUCCESS) {
            break;
        }
        assert(result == 17);  // 0 + 0 + 0 -1 -1 + 2 + 2 + 5 + 5 + 5 = 17
    } while (0);

    // 销毁线段表实例
    HMS\_FAST\_SegmentMap\_Destroy(handle);

    // 销毁配置
    HMS\_FAST\_SegmentMap\_DestroyConfig(config);

    // 释放数组
    if (array) {
        delete\[\] array;
    }

    return ret;
}