动态连续和查询问题:
支持操作:
- Add(x,d):让 Ax 增加 d
- Query(L,R):计算sum(AL,AR)
定义lowbit如下:
1 int lowbit(int u){ return u & (-u); }
前缀和sum与修改结点add如下:
1 int sum(int u){ 2 int ret=0; 3 while(u > 0){ 4 ret += C[u]; u -= lowbit(u); 5 } 6 return ret; 7 } 8 9 int add(int u,int d){ 10 while(u<=n){ 11 C[u] += d; u += lowbit(u); 12 } 13 }
作者所给的模板:
//inline int lowbit(int x) { return x&(x^(x-1)); } inline int lowbit(int x) { return x&-x; } struct FenwickTree { int n; vector<int> C; void resize(int n) { this->n = n; C.resize(n); } void clear() { fill(C.begin(), C.end(), 0); } // 计算A[1]+A[2]+...+A[x] (x<=n) int sum(int x) { int ret = 0; while(x > 0) { ret += C[x]; x -= lowbit(x); } return ret; } // A[x] += d (1<=x<=n) void add(int x, int d) { while(x <= n) { C[x] += d; x += lowbit(x); } } };
联系题目:LA4329
链接: