Skip to content

Commit 5c4053e

Browse files
suisen-cpsuisen.13104f7@gmail.com
authored andcommitted
FenwickTree の配列による O(N) 初期化
1 parent 9aa5f4f commit 5c4053e

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
#gitignore
26+
.gitignore
27+
28+
# test
29+
Main.java

‎FenwickTree/FenwickTree.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public FenwickTree(int n){
77
data = new long[n];
88
}
99

10+
/**
11+
* @verified https://atcoder.jp/contests/practice2/tasks/practice2_b
12+
* @submission https://atcoder.jp/contests/practice2/submissions/16580495
13+
*/
14+
public FenwickTree(long[] data) {
15+
this(data.length);
16+
build(data);
17+
}
18+
1019
public void add(int p, long x){
1120
assert(0<=p && p<_n);
1221
p++;
@@ -28,4 +37,14 @@ private long sum(int r){
2837
}
2938
return s;
3039
}
31-
}
40+
41+
private void build(long[] dat) {
42+
System.arraycopy(dat, 0, data, 0, _n);
43+
for (int i=1; i<=_n; i++) {
44+
int p = i+(i&-i);
45+
if(p<=_n){
46+
data[p-1] += data[i-1];
47+
}
48+
}
49+
}
50+
}

‎FenwickTree/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public FenwickTree(int n)
1818
長さ $n$ の配列 $a_0, a_1, \dots, a_{n-1}$を作ります. 初期値はすべて0です.
1919
計算量: $O(n)$
2020

21+
```java
22+
public FenwickTree(long[] data)
23+
```
24+
25+
長さ $n$ の配列 $a_0, a_1, \dots, a_{n-1}$を `data` により初期化します.
26+
計算量: $O(n)$
27+
2128
## メソッド
2229
### add
2330
```java

0 commit comments

Comments
 (0)