跳到正文

算法·理论

树状数组

Wonderful_Anser约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年10月21日
收录
2026年10月22日
更新
2026年11月9日
篇幅
约 10 分钟

版本

  1. v1当前

树状数组原理

lowbit

lowbit(x) = x & (-x),用于取出 x 二进制表示中最低位的 1 所对应的值。

该值决定了三件事:c[i] 管辖的区间长度、修改操作中的跳转步长、查询操作中的跳转步长。

核心规则

树状数组的每个元素 c[i] 管辖原数组的一段连续区间:

  • 管辖区域:[i - lowbit(i) + 1, i]
  • 管辖长度:lowbit(i)

即 c[i] 存储 a 数组在 [i - lowbit(i) + 1, i] 上的和。该规则是树状数组各项操作的基础。

区间查询原理

求前缀和 a[1..x] 时,将 [1, x] 拆分为若干 c 节点管辖的区间之和。

自 x 开始,c[x] 管辖 [x - lowbit(x) + 1, x],累加后令 x -= lowbit(x),处理剩余部分。每次剥离一段已累加的区间,拆分出的段数不超过 log n。

单点修改原理

修改 a[x] 后,所有管辖范围覆盖 x 的 c 节点均需更新。

由于 c[i] 管辖 [i - lowbit(i) + 1, i],下一个覆盖 i 的节点为 i + lowbit(i)。因此自 x 开始不断加上 lowbit(x),逐级向上直至超过 n,恰好遍历所有覆盖 x 的节点。

两个操作的对称性

update 向更大下标跳转,用于找到所有覆盖 x 的节点;query 向更小下标跳转,用于将 [1, x] 拆分为若干节点区间。两者步长同为 lowbit,故复杂度均为 O(log n)。

区间和

前缀和具有可减性,query(r) - query(l - 1) 即为 a[l..r] 的和。这是树状数组能够处理区间查询的根本原因。

差分的原理

对差分数组 diff[i] = a[i] - a[i-1] 建立树状数组:

  • 区间 [x, y] 加 k,等价于 diff[x] += k、diff[y+1] -= k,即两次单点修改;
  • 查询 a[x],等价于 diff[1..x] 的前缀和,即一次前缀查询。

由此将区间修改、单点查询转化为树状数组擅长的单点修改、前缀查询。

逆序对的原理

固定下标这一维,按顺序从前往后处理。处理到某元素时,已插入的元素均为其前方元素,查询已插入元素中比它大的个数,即为以该元素结尾的逆序对数,累加即得总数。

值域过大时先进行离散化,仅保留元素间的大小关系,再按下标顺序插入并查询。

例题

以下各题统一采用变量名:原数组 a,树状数组 c,序列长度 n,操作数 m。

P3374 【模板】树状数组 1

单点修改、区间查询。按核心规则维护树状数组即可。求区间 [l, r] 的和时,利用前缀和的可减性,答案为 query(r) - query(l - 1)。建树时逐个 update(i, a[i]),总复杂度 O(n log n)。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N], c[N], n, m;
int lowbit(int x) {
    return x & (-x);
}
int query(int x) {
    int ans = 0;
    while (x) {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void update(int x, int val) {
    while (x <= n) {
        c[x] += val;
        x += lowbit(x);
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        update(i, a[i]);
    }
    while (m--) {
        int op, x, y;
        cin >> op >> x >> y;
        if (op == 1) update(x, y);
        else cout << query(y) - query(x - 1) << '\n';
    }
    return 0;
}

P3368 【模板】树状数组 2

区间修改、单点查询。与上一题的唯一区别在于操作类型。对原数组的差分数组 diff[i] = a[i] - a[i-1] 建立树状数组:区间 [x, y] 加 k 等价于 diff[x] += k、diff[y+1] -= k 两次单点修改;查询 a[x] 等价于求差分数组的前缀和。由此把区间修改、单点查询转化为单点修改、前缀查询。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N], diff[N], c[N], n, m;
int lowbit(int x) {
    return x & (-x);
}
int query(int x) {
    int ans = 0;
    while (x) {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void update(int x, int val) {
    while (x <= n) {
        c[x] += val;
        x += lowbit(x);
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        diff[i] = a[i] - a[i - 1];
        update(i, diff[i]);
    }
    while (m--) {
        int op, x, y, k;
        cin >> op >> x;
        if (op == 1) {
            cin >> y >> k;
            update(x, k);
            update(y + 1, -k);
        } else {
            cout << query(x) << '\n';
        }
    }
    return 0;
}

P1908 逆序对

固定下标这一维,按顺序从前往后处理。处理到某元素时,已插入的元素均为其前方元素,查询已插入元素中比它大的个数,即为以该元素结尾的逆序对数,累加即得总数。由于数值范围达到 1e9,需先离散化,仅保留元素间的大小关系,再按下标顺序插入并查询。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N], b[N], c[N], n, len, ans;
int lowbit(int x) {
    return x & (-x);
}
int query(int x) {
    int ans = 0;
    while (x) {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void update(int x, int val) {
    while (x <= len) {
        c[x] += val;
        x += lowbit(x);
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = a[i];
    }
    sort(a + 1, a + n + 1);
    len = unique(a + 1, a + n + 1) - (a + 1);
    for (int i = 1; i <= n; i++) b[i] = lower_bound(a + 1, a + len + 1, b[i]) - a;
    for (int i = 1; i <= n; i++) {
        ans += i - 1 - query(b[i]);
        update(b[i], 1);
    }
    cout << ans;
    return 0;
}

P5149 会议座位

与逆序对一题思路一致,区别在于输入为字符串。先用 map<string,int> 记录每个名字在初始序列中的位置,将新序列映射为位置序列,随后对该位置序列求逆序对即可。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N], c[N], n, ans;
string s;
map<string, int> mp;
int lowbit(int x) {
    return x & (-x);
}
int query(int x) {
    int ans = 0;
    while (x) {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void update(int x, int val) {
    while (x <= n) {
        c[x] += val;
        x += lowbit(x);
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        mp[s] = i;
    }
    for (int i = 1; i <= n; i++) {
        cin >> s;
        a[i] = mp[s];
    }
    for (int i = 1; i <= n; i++) {
        ans += i - 1 - query(a[i]);
        update(a[i], 1);
    }
    cout << ans;
    return 0;
}
原文