public T ConcatTo(int i) { var r = _monoid.Unit; for (i++; i > 0; i -= i & -i) { r = _monoid.Append(r, _tree[i]); } return(r); }
public FenwickTree(IEnumerable <T> collection, Monoid <T> monoid) { var count = collection.Count(); _monoid = monoid; _tree = new T[count + 1]; collection.ForEach((x, i) => { _tree[i + 1] = x; }); for (var i = 1; i < count; i++) { var j = i + (i & -i); if (j < count) { _tree[j] = monoid.Append(_tree[j], _tree[i]); } } }
public SegmentTree(IList <T> collection, Monoid <T> monoid) { if (collection.Count > (int.MaxValue >> 1) + 1) { throw new ArgumentException(); } _size = 1; while (_size < collection.Count) { _size <<= 1; } _tree = new T[_size << 1]; for (var i = 0; i < _size; i++) { _tree[i + _size] = i < collection.Count ? collection[i] : monoid.Unit; } for (var i = _size - 1; i > 0; i--) { _tree[i] = monoid.Append(_tree[i << 1], _tree[(i << 1) + 1]); } this.Monoid = monoid; this.Length = collection.Count; }