A data structure that supports <O (n) sums of element requests from 0 to n
As an example, suppose you had the following numbers in a list in that order:
list = [4, 10, 3, 5, 1]
so list [0] == 4 and list [4] == 1.
Now imagine that you need a sum request that tells you the sum of all previous values up to this given position.
list.sum(0) == 4
list.sum(1) == 14
list.sum(2) == 17
list.sum(3) == 22
list.sum(4) == 23
In addition, I would like to perform the following operations while maintaining unchanged summary queries:
list.swap(0, 1) // swap the two positions
list == [10, 4, 3, 5, 1]
list.slideBefore(0, 3) // slides 1st position value to before the 2nd position
list == [4, 3, 10, 5, 1]
list.slideAfter(2, 3) // slide 1st position value to after 2nd position
list == [4, 3, 5, 10, 1]
list.replace(3, 9) // replace value at 1st param with literal value 2nd param
list == [4, 3, 5, 9, 1]
list.append(17) // adds value to end
list == [4, 3, 5, 9, 1, 17]
This can be trivially handled by an array. But the sum request will always be O (n). I was hoping to find a data structure that would store the sum request in O (1) or O (log n), and also store the above operations in O (1) or O (log n).
, fast array, , , .
, , - , , .
, , ?
, , . , , "" , "".
, " ", , . . , , , , " ".
, , , , .
'swap' 'append` O (1) "" , . "replace", , , (, , ).
slidebefore slideafter O (N), , . :
list == [10, 4, 3, 5, 1]
list.slideBefore(0, 3) // slides 1st position value to before the 2nd position
list == [4, 3, 10, 5, 1]
, 1 2 , 0. slideBefore(0, 1000), 1000 . , , , .
- " ". 20 , 4 5 . . node . , . , , , - .
, . , "" - . 10, . 0, , , , 3 .
, , , , , . , O (N) (, ), , , .
( O (n/5) - O (N)), . .