stevenhalim / cpbook-code

CP4 Free Source Code Project (C++17, Java11, Python3 and OCaml)
2.03k stars 488 forks source link

Fenwick Tree's select method gives incorrect answer #85

Open rchavesf opened 3 years ago

rchavesf commented 3 years ago

Sometimes the method won't return the correct value.

How to reproduce:

FenwickTree ft(10);
for (int i = 1; i <= 10; i++){
    ft.update(i,1);
}
for (int i = 1; i <= 10; i++){
    cout << ft.select(i) << "\n";
}

Expected output:

1
2
3
4
5
6
7
8
9
10

Obtained result:

1
2
3
4
5
6
7
8
16
16

Code execution: https://ideone.com/k5IMXN

stevenhalim commented 3 years ago

eh wait, this looks legit (will KIV), I check again later