Implementing SUM(i,j) and CHANGE(i,j) in O(log(n)) using a datastructure with O(n) space complexity
-
05-11-2019 - |
質問
I have two operations:
$Sum(i,j)$ : Calculate $A[i]+A[i+1]+....+A[j]$
$Change(i,x)$: Set $A[i]=x$
I need to implement these operations in an appropriate data structure using $O(n)$ space ($n$ is the length of the input array $A$).
The $Sum(i,j)$ procedure needs to have a runtime complexity of $O(\log(n))$ and the $Change(i,x)$ procedure needs to have a runtime of $O(\log(n))$.
I thought about using a heap seeing as change-key in a heap is $O(\log(n))$, but I cant figure out how exactly to get the $Sum(i,j)$ procedure down to $O(\log(n))$. To me it seems you would need $O(n)$ time to do this. If anyone could nudge me in the right direction I would appreciate it.
正しい解決策はありません
所属していません cs.stackexchange