当前位置:网站首页>树状数组模版+例题
树状数组模版+例题
2022-08-05 08:11:00 【9ack!?】
简介
树状数组是一种维护区间和的数据结构,支持单点查询。经过魔改后也可以进行区间修改-单点查询, 区间修改-区间查询。你问为什么没有单点修改, 单点查询?因为数组就可以解决了哈哈。
我这里就不介绍原理了,网上一搜一大把。
基本树状数组
const int maxn = 100;
int a[maxn+5]; // 存放原始数据
int c[maxn+5]; // 存放树状数组
int n; // 存放数据的最大下标
int lowbit(int x) {
return x&(-x);
}
void add(int i, int k) {
while(i <= n) {
c[i] += k;
i += lowbit(i);
}
}
int getsum(int i) {
// 计算从1开始到i(两边都是闭区间)的区间和
int res = 0;
while(i > 0) {
res += c[i];
i -= lowbit(i);
}
}
void init() {
// 创建树的时候调用
for(int i = 1; i <= n; ++i) {
c[i] += a[i];
int j = i + lowbit(i);
if( j <= n) c[j] += c[i];
}
}
// 查询[x, y]
int sum = getsum(y)-getsum(x-1);
区间修改, 单点查询
首先不能直接套用上面的模版,对区间内的每个元素进行单点修改时间复杂度肯定会爆。
思考一下对区间进行修改时的特点,对整个区间进行修改的时候,区间内相邻元素的差值是不变的,所以我们可以用树状数组维护原数组的差分数组,修改的时候只需要对区间的两个端点修改即可。查询某个点的值时,求出差分数组的前缀和即可。
// 这里的函数直接套用上面的模版即可
// [x, y]区间加上k
for(int i = 1; i <= n; ++i) {
// 读入数据
scanf("%d", &a[i]);
update(i, a[i]-a[i-1]);
}
// [x, y]区间加上k
update(x, k);
update(y+1, -k);
int sum = getsum(i); // 这里的getsum变成了对差分数组的前i个元素进行求和, 于是得到的是a[i]
区间修改, 区间查询
这个就更玄学了,这个问题完全可以用线段树来解决,而且明显树状数组能维护的信息不如线段树多。但是线段树的常数要比线段树小,所以在某些条件下还是能解决一些问题的 (就比如下面的例题)
实际代码模版也差不多,但是运用了更加神奇的方法
// 需要维护两个树状数组
const int maxn;
int n, m;
int a[maxn];
int sum1[maxn]; // (D[1] + D[2] + ... D[n]), D是差分数组
int sum2[maxn]; // (1*D[1] + 2*D[2] + .. + n*D[n])
void update(int i, int k) {
int x = i;
while(i <= n) {
sum1[i] += k;
sum2[i] += k*(x-1);
i += lowbit(i);
}
}
int getsum(int i) {
int res = 0, x = i;
while(i > 0) {
res += x*sum1[i] - sum2[i];
i -= lowbit(i);
}
return res;
}
// 读入数据
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
update(i, a[i]-a[i-1]);
}
// [x, y]区间加上k
update(x, k);
update(y+1, -k);
// 求[x, y]区间和
int sum = getsum(y) - getsum(x-1);
//连这一篇都要限流...
边栏推荐
猜你喜欢
随机推荐
力扣每日一题
[Structural Internal Power Cultivation] Structural Realization Stages (2)
Chapter 12 Bayesian Networks
学习笔记14--机器学习在局部路径规划中的应用
Green Apple Forum reopens
Basic introduction of stack and queue and C language implementation of functions such as creation, destruction, entry and exit, counting the number of elements, viewing elements, etc., as well as stac
JS语法使用
青苹果论坛重新开放
Spark cluster deployment (third bullet)
【 a daily topic 】 1403. The increasing order of the sequence, boy
【深度学习实践(一)】安装TensorFlow
D2--FPGA SPI接口通信2022-08-03
Chapter3、色调映射
Thinking after writing a code with a very high CPU usage
P1160 队列安排
Redis implements distributed lock-principle-detailed explanation of the problem
TensorFlow installation steps
DataFrame在指定位置插入行和列
真正爱你的女人是这样的
Controlling number and letter input in ASP