-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathFenwickTree.java
More file actions
58 lines (51 loc) · 1.2 KB
/
FenwickTree.java
File metadata and controls
58 lines (51 loc) · 1.2 KB
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
class FenwickTree{
private int _n;
private long[] data;
public FenwickTree(int n){
this._n = n;
data = new long[n];
}
/**
* @verified https://atcoder.jp/contests/practice2/tasks/practice2_b
* @submission https://atcoder.jp/contests/practice2/submissions/16580495
*/
public FenwickTree(long[] data) {
this(data.length);
build(data);
}
public void set(int p, long x){
add(p, x - get(p));
}
public void add(int p, long x){
assert(0<=p && p<_n);
p++;
while(p<=_n){
data[p-1] += x;
p += p&-p;
}
}
public long sum(int l, int r){
assert(0<=l && l<=r && r<=_n);
return sum(r)-sum(l);
}
public long get(int p){
return sum(p, p+1);
}
private long sum(int r){
long s = 0;
while(r>0){
s += data[r-1];
r -= r&-r;
}
return s;
}
private void build(long[] dat) {
System.arraycopy(dat, 0, data, 0, _n);
for (int i=1; i<=_n; i++) {
int p = i+(i&-i);
if(p<=_n){
data[p-1] += data[i-1];
}
}
}
}