Fenwick Tree Java

自作多情 提交于 2019-12-02 07:25:44

You do not initialize it properly. Instead of:

for (int i = 0; i < N; i++) {
    a[i] = Long.parseLong(str[i]);
}

It should be:

for (int i = 0; i < N; i++) {
    increment(i, (int)Long.parseLong(str[i]));
}

because a[i] should store a cumulative sum, not a single element.

If you want to store the initial array elements too, you can just create one more array:

long[] initA = new long[N];
for (int i = 0; i < N; i++) {
    initA[i] = Long.parseLong(str[i]);
    increment(i, (int)initA[i]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!