All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.psjava.ds.tree.segmenttree.RangeUpdatableSegmentTree Maven / Gradle / Ivy

The newest version!
package org.psjava.ds.tree.segmenttree;

import org.psjava.ds.array.Array;
import org.psjava.ds.math.BinaryOperator;
import org.psjava.formula.Power;

public class RangeUpdatableSegmentTree implements SegmentTree {

	public static final  RangeUpdatableSegmentTree create(Array initialData, BinaryOperator operator) {
		return new RangeUpdatableSegmentTree(initialData, operator);
	}

	private LazyPropagatingSegmentTree sub;

	private RangeUpdatableSegmentTree(Array initialData, final BinaryOperator operator) {
		sub = new LazyPropagatingSegmentTree(initialData, new EnhancedRangeUpdatableSegmentTreeOperator() {
			@Override
			public T mergeRangeValue(T oldRangeValue, int rangeSize, T updateData) {
				return Power.calc(updateData, rangeSize, operator);
			}

			@Override
			public T mergeSingleValue(T v1, T v2) {
				return operator.calc(v1, v2);
			}

			@Override
			public T mergeUpdateData(T oldData, T newData) {
				return newData;
			}
		});
	}

	@Override
	public T query(int start, int end) {
		return sub.queryRange(start, end);
	}

	@Override
	public void update(int p, final T v) {
		updateRange(p, p + 1, v);
	}

	public void updateRange(int start, int end, T value) {
		sub.updateRange(start, end, value);
	}

	@Override
	public String toString() {
		return sub.toString();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy