File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 4848| [ LRUCache] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/LRUCache.java ) |
4949| [ BinarySearchTree] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/BinarySearchTree.java ) |
5050| [ BinarySearchTreeWithParent] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/BinarySearchTreeWithParent.java ) |
51+ | [ BinaryIndexedTree] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/BinaryIndexedTree.java ) |
5152| [ MinHeap] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/MinHeap.java ) |
5253| [ DisjointSet (Union-Find)] ( https://github.com/fluency03/leetcode-java/blob/master/src/data-structures/DisjointSet.java ) |
5354| [ Graph] ( https://github.com/fluency03/leetcode-java/blob/master/src/graph ) |
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ */
4+
5+ public class BinaryIndexedTree {
6+ private int [] tree ;
7+ private int N ;
8+
9+ public BinaryIndexedTree (int [] nums ) {
10+ if (nums == null ) return ;
11+ this .N = nums .length ;
12+ this .tree = new int [N +1 ];
13+ constructBIT (nums );
14+ }
15+
16+ private void constructBIT (int [] nums ) {
17+ int N = nums .length ;
18+ for (int i =0 ; i <N ; i ++) {
19+ update (i , nums [i ]);
20+ }
21+ }
22+
23+ public void update (int i , int delta ) {
24+ int k = i ;
25+ while (k <= this .N ) {
26+ this .tree [k ] += delta ;
27+ k += lowBit (k );
28+ }
29+ }
30+
31+ public int query (int i ) {
32+ int k = i ;
33+ int res = 0 ;
34+ while (k > 0 ) {
35+ res += this .tree [k ];
36+ k -= lowBit (k );
37+ }
38+ return res ;
39+ }
40+
41+ private int lowBit (int x ) {
42+ return x & (-x );
43+ }
44+
45+ }
You can’t perform that action at this time.
0 commit comments