树状数组
简介
树状数组是一种支持单点修改,区间查询的数据结构。它能过维护具有结合律和可差分的信息。
作为引入我们看看下面这个问题: 假设我们需要知道a[1]—a[7]的和,我们会怎么做?一个简单的想法是前缀和,假设我们已经知道了a[1–4],a[4 – 6]和a[7]的值,显然将这个三值相加就是答案了。这也就是树状数组的核心了,将数据分段,维护一个特殊的前缀和。先看看树状数组的结构:
c[1] 管辖 1-1
c[2] 管辖 1-2
c[3] 管辖 3-3
c[4] 管辖 1-4
关于树状数组为何是这样的形态,作者在这无法给出更好的解释,有需要请看oi-wiki树状数组
习题
下面给出oi-wiki上几个习题的解答
树状数组 1 :单点修改,区间查询
最基础的模型:需要建树,区间查询和单点修改三个函数
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//#define int long long
#define ios ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
const int N = 1e6 + 5;
int n, q, a[N];
LL c[N];
int lowbit(int x) {
return x & -x;
}
void init(int n) {
for (int i = 1; i <= n; i++) {
c[i] += a[i];
int j = i + lowbit(i);
if (j <= n)
c[j] += c[i];
}
}
void add(int idx, LL x) {
int i = idx;
while (i <= n) {
c[i] += x;
i += lowbit(i);
}
}
LL get_sum(int r) {
LL ans = 0;
while (r) {
ans += c[r];
r -= lowbit(r);
}
return ans;
}
LL query(int l, int r) {
return get_sum(r) - get_sum(l - 1);
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i]; // 对于树状数组我们的下标从1开始
init(n);
while (q--) {
int op, l, r, i;
LL x;
cin >> op;
if (op == 1) {
cin >> i >> x;
add(i, x);
} else {
cin >> l >> r;
cout << query(l, r) << endl;
}
}
}
signed main() {
ios;
int _;
_ = 1; //cin >> _;
while (_--) {
solve();
}
return 0;
}
树状数组 2 :区间修改,单点查询
在这里需要引入差分数组来支持区间修改操作。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//#define int long long
#define ios ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
const int N = 1e6 + 5;
int n, q, a[N];
LL c[N];
int lowbit(int x) {
return x & -x;
}
void init(int n) {
for (int i = 1; i <= n; i++) {
c[i] += a[i] - a[i - 1];
int j = i + lowbit(i);
if (j <= n)
c[j] += c[i];
}
}
void add(int idx, LL x) {
int i = idx;
while (i <= n) {
c[i] += x;
i += lowbit(i);
}
}
LL get_sum(int r) {
LL ans = 0;
while (r) {
ans += c[r];
r -= lowbit(r);
}
return ans;
}
LL query(int l, int r) {
return get_sum(r) - get_sum(l - 1);
}
void update(int l, int r, LL x) {
add(l, x);
add(r + 1, -x);
}
LL getele(int idx) {
return query(1, idx);
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i]; // 对于树状数组我们的下标从1开始
init(n);
while (q--) {
int op, l, r, i;
LL x;
cin >> op;
if (op == 1) {
cin >> l >> r >> x;
update(l, r, x);
} else {
cin >> i;
cout << getele(i) << endl;
}
}
}
signed main() {
ios;
int _;
_ = 1; //cin >> _;
while (_--) {
solve();
}
return 0;
}
树状数组 3 :区间修改,区间查询
这里需要维护两个树状数组来支持区间查询和区间修改的操作
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define int long long
#define ios ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
const int N = 1e6 + 5;
int n, q, a[N];
LL c[N], d[N];
int lowbit(int x) {
return x & -x;
}
void init(int n) {
for (int i = 1; i <= n; i++) {
c[i] += a[i] - a[i - 1];
d[i] += (a[i] - a[i - 1]) * i;
int j = i + lowbit(i);
if (j <= n) {
c[j] += c[i];
d[j] += d[i];
}
}
}
void add(int idx, LL x) {
int i = idx;
while (i <= n) {
c[i] += x;
i += lowbit(i);
}
}
void add2(int idx, LL x) {
int i = idx;
while (i <= n) {
d[i] += x * idx;
i += lowbit(i);
}
}
LL get_sum(int r) {
LL ans = 0;
while (r) {
ans += c[r];
r -= lowbit(r);
}
return ans;
}
LL get_sum2(int r) {
LL ans = 0;
int i = r;
while (i) {
ans += c[i];
i -= lowbit(i);
}
ans = ans * (r + 1);
i = r;
while (i) {
ans -= d[i];
i -= lowbit(i);
}
return ans;
}
LL query(int l, int r) {
return get_sum(r) - get_sum(l - 1);
}
void update(int l, int r, LL x) {
add(l, x);
add(r + 1, -x);
add2(l, x);
add2(r + 1, -x);
}
LL getele(int idx) {
return query(1, idx);
}
LL get_interval(int l, int r) {
return get_sum2(r) - get_sum2(l - 1);
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i]; // 对于树状数组我们的下标从1开始
init(n);
while (q--) {
int op, l, r, i;
LL x;
cin >> op;
if (op == 1) {
cin >> l >> r >> x;
update(l, r, x);
} else {
cin >> l >> r;
cout << get_interval(l, r) << endl;
}
}
}
signed main() {
ios;
int _;
_ = 1; //cin >> _;
while (_--) {
solve();
}
return 0;
}