-
Notifications
You must be signed in to change notification settings - Fork 472
/
FenwickTree.java
63 lines (53 loc) · 1.08 KB
/
FenwickTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package data_structures.trees;
public class FenwickTree { // one-based DS
int n;
int[] ft;
FenwickTree(int size) { n = size; ft = new int[n+1]; }
int rsq(int b) //O(log n)
{
int sum = 0;
while(b > 0) { sum += ft[b]; b -= b & -b;} //min?
return sum;
}
int rsq(int a, int b) { return rsq(b) - rsq(a-1); }
void point_update(int k, int val) //O(log n), update = increment
{
while(k <= n) { ft[k] += val; k += k & -k; } //min?
}
int point_query(int idx) // c * O(log n), c < 1
{
int sum = ft[idx];
if(idx > 0)
{
int z = idx ^ (idx & -idx);
--idx;
while(idx != z)
{
sum -= ft[idx];
idx ^= idx & -idx;
}
}
return sum;
}
void scale(int c) { for(int i = 1; i <= n; ++i) ft[i] *= c; }
int findIndex(int cumFreq)
{
int msk = n;
while((msk & (msk - 1)) != 0)
msk ^= msk & -msk; //msk will contain the MSB of n
int idx = 0;
while(msk != 0)
{
int tIdx = idx + msk;
if(tIdx <= n && cumFreq >= ft[tIdx])
{
idx = tIdx;
cumFreq -= ft[tIdx];
}
msk >>= 1;
}
if(cumFreq != 0)
return -1;
return idx;
}
}