|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +public class LazySegmentTree { |
| 4 | + /** |
| 5 | + * Lazy Segment Tree |
| 6 | + * |
| 7 | + * @see |
| 8 | + * <a href="https://www.geeksforgeeks.org/lazy-propagation-in-segment-tree/"> |
| 9 | + */ |
| 10 | + static class Node { |
| 11 | + private final int start, end; // start and end of the segment represented by this node |
| 12 | + private int value; // value is the sum of all elements in the range [start, end) |
| 13 | + private int lazy; // lazied value that should be added to children nodes |
| 14 | + Node left, right; // left and right children |
| 15 | + public Node(int start, int end, int value) { |
| 16 | + this.start = start; |
| 17 | + this.end = end; |
| 18 | + this.value = value; |
| 19 | + this.lazy = 0; |
| 20 | + this.left = null; |
| 21 | + this.right = null; |
| 22 | + } |
| 23 | + |
| 24 | + /** Update the value of this node with the given value diff. |
| 25 | + * |
| 26 | + * @param diff The value to add to every index of this node range. |
| 27 | + */ |
| 28 | + public void applyUpdate(int diff) { |
| 29 | + this.lazy += diff; |
| 30 | + this.value += (this.end - this.start) * diff; |
| 31 | + } |
| 32 | + |
| 33 | + /** Shift the lazy value of this node to its children. |
| 34 | + */ |
| 35 | + public void shift() { |
| 36 | + if (lazy == 0) return; |
| 37 | + if (this.left == null && this.right == null) return; |
| 38 | + this.value += this.lazy; |
| 39 | + if (this.left != null) this.left.applyUpdate(this.lazy); |
| 40 | + if (this.right != null) this.right.applyUpdate(this.lazy); |
| 41 | + this.lazy = 0; |
| 42 | + } |
| 43 | + |
| 44 | + /** Create a new node that is the sum of this node and the given node. |
| 45 | + * |
| 46 | + * @param left The left Node of merging |
| 47 | + * @param right The right Node of merging |
| 48 | + * @return The new Node. |
| 49 | + */ |
| 50 | + static Node merge(Node left, Node right) { |
| 51 | + if (left == null) return right; |
| 52 | + if (right == null) return left; |
| 53 | + Node result = new Node(left.start, right.end, left.value + right.value); |
| 54 | + result.left = left; |
| 55 | + result.right = right; |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + public int getValue() { |
| 60 | + return value; |
| 61 | + } |
| 62 | + |
| 63 | + public Node getLeft() { |
| 64 | + return left; |
| 65 | + } |
| 66 | + |
| 67 | + public Node getRight() { |
| 68 | + return right; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private final Node root; |
| 73 | + |
| 74 | + /** Create a new LazySegmentTree with the given array. |
| 75 | + * |
| 76 | + * @param array The array to create the LazySegmentTree from. |
| 77 | + */ |
| 78 | + public LazySegmentTree(int[] array) { |
| 79 | + this.root = buildTree(array, 0, array.length); |
| 80 | + } |
| 81 | + |
| 82 | + /** Build a new LazySegmentTree from the given array in O(n) time. |
| 83 | + * |
| 84 | + * @param array The array to build the LazySegmentTree from. |
| 85 | + * @param start The start index of the current node. |
| 86 | + * @param end The end index of the current node. |
| 87 | + * @return The root of the new LazySegmentTree. |
| 88 | + */ |
| 89 | + private Node buildTree(int[] array, int start, int end) { |
| 90 | + if (end - start < 2) return new Node(start, end, array[start]); |
| 91 | + int mid = (start + end) >> 1; |
| 92 | + Node left = buildTree(array, start, mid); |
| 93 | + Node right = buildTree(array, mid, end); |
| 94 | + return Node.merge(left, right); |
| 95 | + } |
| 96 | + |
| 97 | + /** Update the value of given range with the given value diff in O(log n) time. |
| 98 | + * |
| 99 | + * @param left The left index of the range to update. |
| 100 | + * @param right The right index of the range to update. |
| 101 | + * @param diff The value to add to every index of the range. |
| 102 | + * @param curr The current node. |
| 103 | + */ |
| 104 | + private void updateRange(int left, int right, int diff, Node curr) { |
| 105 | + if (left <= curr.start && curr.end <= right) { |
| 106 | + curr.applyUpdate(diff); |
| 107 | + return; |
| 108 | + } |
| 109 | + if (left >= curr.end || right <= curr.start) return; |
| 110 | + curr.shift(); |
| 111 | + updateRange(left, right, diff, curr.left); |
| 112 | + updateRange(left, right, diff, curr.right); |
| 113 | + Node merge = Node.merge(curr.left, curr.right); |
| 114 | + curr.value = merge.value; |
| 115 | + } |
| 116 | + |
| 117 | + /** Get Node of given range in O(log n) time. |
| 118 | + * |
| 119 | + * @param left The left index of the range to update. |
| 120 | + * @param right The right index of the range to update. |
| 121 | + * @return The Node representing the sum of the given range. |
| 122 | + */ |
| 123 | + private Node getRange(int left, int right, Node curr) { |
| 124 | + if (left <= curr.start && curr.end <= right) return curr; |
| 125 | + if (left >= curr.end || right <= curr.start) return null; |
| 126 | + curr.shift(); |
| 127 | + return Node.merge(getRange(left, right, curr.left), getRange(left, right, curr.right)); |
| 128 | + } |
| 129 | + |
| 130 | + public int getRange(int left, int right) { |
| 131 | + Node result = getRange(left, right, root); |
| 132 | + return result == null ? 0 : result.getValue(); |
| 133 | + } |
| 134 | + |
| 135 | + public void updateRange(int left, int right, int diff) { |
| 136 | + updateRange(left, right, diff, root); |
| 137 | + } |
| 138 | + |
| 139 | + public Node getRoot() { |
| 140 | + return root; |
| 141 | + } |
| 142 | +} |
0 commit comments