English 中文(简体)
支持添加和部分内容的数据结构
原标题:Data structure supporting Add and Partial-Sum

让[1.n]成为一系列实际数字。 a. 设计一种算法,以实施下列业务的任何顺序:

Add(i,y) -- Add the value y to the ith number.
Partial-sum(i) -- Return the sum of the first i numbers, i.e.

没有插入或删除;唯一的改动是数字的数值。 每个行动都应采取O(logn)步骤。 您可以再使用一阵大小,作为工作空间。

如何设计上述算法的数据结构?

最佳回答

ruct平平平的双向树;按原顺序沿树底 elements。

树木中每一树冠的“树木的树叶”;树有独角树-1节点,因此,用O(n)设定时间(我们有)。

部分消费也一样: 将树木推向 que(叶) no,但只要你放弃权利,就在左边加上你刚才访问的内容,因为这些内容总合。

改变价值就是这样: Find the query (left) node. 计算你增加的差额。 前往树根;在你去向根基时,通过添加大差(如果你重新储存“树叶”或“左树加我本人”或某些变体,你可能需要访问邻近的树苗);主要想法是,你适当更新需要更新的所有强化分支数据,并且数据将放在根本道路上或与之相邻。

这两项行动需要O(log(n)时间( tree树的高度),而O(1)在每一节点工作。

You can probably use any search tree (e.g. a self-balancing binary search tree might allow for insertions, others for quicker access) but I haven t thought that one through.

问题回答

页: 1

见问题





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签