Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ hs_err_pid*

# IntelliJ
*.iml
.idea/
.idea/
19 changes: 19 additions & 0 deletions FenwickTree/FenwickTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public FenwickTree(int 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 add(int p, long x){
assert(0<=p && p<_n);
p++;
Expand All @@ -28,4 +37,14 @@ private long sum(int 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];
}
}
}
}
7 changes: 7 additions & 0 deletions FenwickTree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public FenwickTree(int n)
長さ $n$ の配列 $a_0, a_1, \dots, a_{n-1}$を作ります. 初期値はすべて0です.
計算量: $O(n)$

```java
public FenwickTree(long[] data)
```

長さ $n$ の配列 $a_0, a_1, \dots, a_{n-1}$を `data` により初期化します.
計算量: $O(n)$

## メソッド
### add
```java
Expand Down
Loading