int c[MAX];
int lowbit(int x)
{
return x & (-x);
}
int sum(int x)
{
int s = 0;
while(x > 0)
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int update(int x, int value)
{
while(x <= MAX)
{
c[x] += value;
x += lowbit(x);
}
}树状数组模板
最新推荐文章于 2025-02-21 16:20:19 发布
本文介绍了一种数据结构——树状数组,并展示了如何通过树状数组实现快速查询和更新操作。具体包括计算某个位置的前缀和(sum)及更新特定位置的数值(update)。通过对lowbit函数的使用,使得这些操作的时间复杂度降低到O(log n)。
2369

被折叠的 条评论
为什么被折叠?



