We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d844167 commit cbdbc38Copy full SHA for cbdbc38
code/data-structures/fenwick_tree.cpp
@@ -1,13 +1,13 @@
1
-struct fwtree{ // 1-indexed
+struct fwtree{ // 0-indexed
2
int n; vi bit;
3
fwtree(int n): n(n), bit(n+1){}
4
int rsq(int r){
5
int sum = 0;
6
- for(; r; r -= r & -r) sum += bit[r];
+ for(r+=1; r; r -= r & -r) sum += bit[r];
7
return sum;
8
}
9
- int rsq(int l, int r){return rsq(r) - rsq(l-1);}
+ int rsq(int l, int r){return rsq(r) - (l==0 ? 0 : rsq(l-1));}
10
void upd(int r, int v){
11
- for(; r <= n; r += r & -r) bit[r] += v;
+ for(r+=1; r <= n; r += r & -r) bit[r] += v;
12
13
};
0 commit comments