Skip to content

Commit bc8a574

Browse files
committed
add Fenwick::set/get
add new functions: set(int p, long x) and get(int p). I implemented this because I needed them on ABC186.
1 parent 99a84f3 commit bc8a574

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

FenwickTree/FenwickTree.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public FenwickTree(long[] data) {
1616
build(data);
1717
}
1818

19+
public void set(int p, long x){
20+
add(p, x - get(p));
21+
}
22+
1923
public void add(int p, long x){
2024
assert(0<=p && p<_n);
2125
p++;
@@ -29,6 +33,10 @@ public long sum(int l, int r){
2933
return sum(r)-sum(l);
3034
}
3135

36+
public long get(int p){
37+
return sum(p, p+1);
38+
}
39+
3240
private long sum(int r){
3341
long s = 0;
3442
while(r>0){

FenwickTree/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public void add(int p, long x)
3232
配列の第$p$要素に$x$を加える. すなわち, `a[p] += x` のこと.
3333
計算量: $O(\log N)$
3434

35+
### set
36+
```java
37+
public void set(int p, long x)
38+
```
39+
配列の第$p$要素を$x$に変更する. すなわち, `a[p] = x` のこと.
40+
計算量: $O(\log N)$
41+
42+
### get
43+
```java
44+
public long get(int p)
45+
```
46+
配列の第$p$要素を取得する. 計算量: $O(\log n)$
47+
3548
### sum
3649
```java
3750
public long sum(int l, int r)

0 commit comments

Comments
 (0)