树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)
2015-01-19 13:03
323 查看
刘汝佳:《训练指南》Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; //一维树状数组基础模板 int lowbit(int x) { return x&(-x); } int c[1001]; int sum(int x) //计算从1到x的数组元素的和 { int s=0; while(x>0) { s=s+c[x]; x=x-lowbit(x); } return s; } void add(int x, int d ) //将A(x)的值增加d,也可以减掉某个值 { while(x<=1000) //此处的1000只是我开的数组的封顶,这是根据题目的数据进行更改的 { c[x]=c[x]+d; x=x+lowbit(x); } } int main() { int a[1001]; int i, j; for(i=0; i<=1000; i++ ) { a[i]=i; } c[0]=0; for(i=1; i<=1000; i++) //有a[]数组进行计算c[]数组的值 { c[i]=0; for(j=i-lowbit(i)+1; j<=i; j++ ) { c[i]=c[i]+a[j]; } // } int x; scanf("%d", &x); printf("%d\n", sum(x) ); //查询1->x的a[]的和 int y; scanf("%d", &y); add(x, y); //a[]下标为x的元素增加d printf("%d\n", sum(x)); return 0; }
相关文章推荐
- 树状数组的区间修改和区间查询模板
- 树状数组模板(区间更新单点查询)
- (模板)树状数组 (区间修改,单点查询)
- CS Academy Round #30 (Div. 2 only) C.Constant Sum(树状数组,区间修改,单点查询模板)
- A Simple Problem with Integers(树状数组,更新区间查询区间模板)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- 树状数组模板2——区间修改,单点查询
- 一维树状数组区间更新区间查询
- hdu1556-树状数组 一维 区间更新 单点查询
- Flowers(树状数组+区间更新+单点查询+区间更新单点查询模板)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- bzoj 3132 上帝造题的七分钟(二维树状数组区间修改区间查询模板)
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
- 树状数组模板(max,min,区间和,区间修改,单点查询)
- 【原创】【数据结构】一维树状数组的基本操作(单点修改,区间查询) (HDU1166 敌兵布阵)
- codevs 1082 一维树状数组区间修改区间查询
- poj3468-树状数组 一维 区间更新 区间查询
- hdu 2642二维树状数组 单点更新区间查询 模板题
- hdu 2642二维树状数组 单点更新区间查询 模板题
- 树状数组模板1——单点修改区间查询