代码拉取完成,页面将自动刷新
namespace DataStructures.SegmentTrees;
/// <summary>
/// This is an extension of a segment tree, which allows the update of a single element.
/// </summary>
public class SegmentTreeUpdate : SegmentTree
{
/// <summary>
/// Initializes a new instance of the <see cref="SegmentTreeUpdate" /> class.
/// Runtime complexity: O(n) where n equals the array-length.
/// </summary>
/// <param name="arr">Array on which the queries should be made.</param>
public SegmentTreeUpdate(int[] arr)
: base(arr)
{
}
/// <summary>
/// Updates a single element of the input array.
/// Changes the leaf first and updates its parents afterwards.
/// Runtime complexity: O(logN) where N equals the initial array-length.
/// </summary>
/// <param name="node">Index of the node that should be updated.</param>
/// <param name="value">New Value of the element.</param>
public void Update(int node, int value)
{
Tree[node + Tree.Length / 2] = value;
Propagate(Parent(node + Tree.Length / 2));
}
/// <summary>
/// Recalculates the value of node by its children.
/// Calls its parent to do the same.
/// </summary>
/// <param name="node">Index of current node.</param>
private void Propagate(int node)
{
if (node == 0)
{
// passed root
return;
}
Tree[node] = Tree[Left(node)] + Tree[Right(node)];
Propagate(Parent(node));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。