|
| 1 | +# Segment Tree with Lazy Propagation |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +Segment trees are advanced data structures used for efficient range queries and updates. Lazy propagation optimizes updates by deferring them, making segment trees suitable for scenarios with frequent range modifications. |
| 6 | + |
| 7 | +This challenge involves implementing a segment tree that supports range sum queries and range updates efficiently using lazy propagation. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class SegmentTree { |
| 13 | + constructor(data) { |
| 14 | + this.n = data.length; |
| 15 | + this.tree = new Array(4 * this.n).fill(0); |
| 16 | + this.lazy = new Array(4 * this.n).fill(0); |
| 17 | + this._build(data, 0, 0, this.n - 1); |
| 18 | + } |
| 19 | + |
| 20 | + _build(data, node, start, end) { |
| 21 | + if (start === end) { |
| 22 | + this.tree[node] = data[start]; |
| 23 | + } else { |
| 24 | + const mid = Math.floor((start + end) / 2); |
| 25 | + this._build(data, 2 * node + 1, start, mid); |
| 26 | + this._build(data, 2 * node + 2, mid + 1, end); |
| 27 | + this.tree[node] = this.tree[2 * node + 1] + this.tree[2 * node + 2]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + _updateRange(node, start, end, l, r, val) { |
| 32 | + if (this.lazy[node] !== 0) { |
| 33 | + this.tree[node] += (end - start + 1) * this.lazy[node]; |
| 34 | + if (start !== end) { |
| 35 | + this.lazy[2 * node + 1] += this.lazy[node]; |
| 36 | + this.lazy[2 * node + 2] += this.lazy[node]; |
| 37 | + } |
| 38 | + this.lazy[node] = 0; |
| 39 | + } |
| 40 | + |
| 41 | + if (start > r || end < l) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (l <= start && end <= r) { |
| 46 | + this.tree[node] += (end - start + 1) * val; |
| 47 | + if (start !== end) { |
| 48 | + this.lazy[2 * node + 1] += val; |
| 49 | + this.lazy[2 * node + 2] += val; |
| 50 | + } |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const mid = Math.floor((start + end) / 2); |
| 55 | + this._updateRange(2 * node + 1, start, mid, l, r, val); |
| 56 | + this._updateRange(2 * node + 2, mid + 1, end, l, r, val); |
| 57 | + this.tree[node] = this.tree[2 * node + 1] + this.tree[2 * node + 2]; |
| 58 | + } |
| 59 | + |
| 60 | + updateRange(l, r, val) { |
| 61 | + this._updateRange(0, 0, this.n - 1, l, r, val); |
| 62 | + } |
| 63 | + |
| 64 | + _queryRange(node, start, end, l, r) { |
| 65 | + if (start > r || end < l) { |
| 66 | + return 0; |
| 67 | + } |
| 68 | + |
| 69 | + if (this.lazy[node] !== 0) { |
| 70 | + this.tree[node] += (end - start + 1) * this.lazy[node]; |
| 71 | + if (start !== end) { |
| 72 | + this.lazy[2 * node + 1] += this.lazy[node]; |
| 73 | + this.lazy[2 * node + 2] += this.lazy[node]; |
| 74 | + } |
| 75 | + this.lazy[node] = 0; |
| 76 | + } |
| 77 | + |
| 78 | + if (l <= start && end <= r) { |
| 79 | + return this.tree[node]; |
| 80 | + } |
| 81 | + |
| 82 | + const mid = Math.floor((start + end) / 2); |
| 83 | + const leftSum = this._queryRange(2 * node + 1, start, mid, l, r); |
| 84 | + const rightSum = this._queryRange(2 * node + 2, mid + 1, end, l, r); |
| 85 | + return leftSum + rightSum; |
| 86 | + } |
| 87 | + |
| 88 | + queryRange(l, r) { |
| 89 | + return this._queryRange(0, 0, this.n - 1, l, r); |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### History |
| 95 | + |
| 96 | +Segment trees have been widely used in competitive programming and computer science for efficient range queries and updates. Lazy propagation is a technique that defers updates to child nodes, improving performance when multiple range updates are involved. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Español |
| 101 | + |
| 102 | +Árbol de Segmentos con Propagación Perezosa |
| 103 | + |
| 104 | +Los árboles de segmentos son estructuras de datos avanzadas usadas para consultas y actualizaciones eficientes en rangos. La propagación perezosa optimiza las actualizaciones al diferirlas, haciendo que los árboles de segmentos sean adecuados para escenarios con modificaciones frecuentes en rangos. |
| 105 | + |
| 106 | +Este reto consiste en implementar un árbol de segmentos que soporte consultas de suma en rangos y actualizaciones en rangos de manera eficiente usando propagación perezosa. |
| 107 | + |
| 108 | +### Fragmento de Código Relevante |
| 109 | + |
| 110 | +```javascript |
| 111 | +class SegmentTree { |
| 112 | + constructor(data) { |
| 113 | + this.n = data.length; |
| 114 | + this.tree = new Array(4 * this.n).fill(0); |
| 115 | + this.lazy = new Array(4 * this.n).fill(0); |
| 116 | + this._build(data, 0, 0, this.n - 1); |
| 117 | + } |
| 118 | + |
| 119 | + _build(data, node, start, end) { |
| 120 | + if (start === end) { |
| 121 | + this.tree[node] = data[start]; |
| 122 | + } else { |
| 123 | + const mid = Math.floor((start + end) / 2); |
| 124 | + this._build(data, 2 * node + 1, start, mid); |
| 125 | + this._build(data, 2 * node + 2, mid + 1, end); |
| 126 | + this.tree[node] = this.tree[2 * node + 1] + this.tree[2 * node + 2]; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + _updateRange(node, start, end, l, r, val) { |
| 131 | + if (this.lazy[node] !== 0) { |
| 132 | + this.tree[node] += (end - start + 1) * this.lazy[node]; |
| 133 | + if (start !== end) { |
| 134 | + this.lazy[2 * node + 1] += this.lazy[node]; |
| 135 | + this.lazy[2 * node + 2] += this.lazy[node]; |
| 136 | + } |
| 137 | + this.lazy[node] = 0; |
| 138 | + } |
| 139 | + |
| 140 | + if (start > r || end < l) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (l <= start && end <= r) { |
| 145 | + this.tree[node] += (end - start + 1) * val; |
| 146 | + if (start !== end) { |
| 147 | + this.lazy[2 * node + 1] += val; |
| 148 | + this.lazy[2 * node + 2] += val; |
| 149 | + } |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + const mid = Math.floor((start + end) / 2); |
| 154 | + this._updateRange(2 * node + 1, start, mid, l, r, val); |
| 155 | + this._updateRange(2 * node + 2, mid + 1, end, l, r, val); |
| 156 | + this.tree[node] = this.tree[2 * node + 1] + this.tree[2 * node + 2]; |
| 157 | + } |
| 158 | + |
| 159 | + updateRange(l, r, val) { |
| 160 | + this._updateRange(0, 0, this.n - 1, l, r, val); |
| 161 | + } |
| 162 | + |
| 163 | + _queryRange(node, start, end, l, r) { |
| 164 | + if (start > r || end < l) { |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + if (this.lazy[node] !== 0) { |
| 169 | + this.tree[node] += (end - start + 1) * this.lazy[node]; |
| 170 | + if (start !== end) { |
| 171 | + this.lazy[2 * node + 1] += this.lazy[node]; |
| 172 | + this.lazy[2 * node + 2] += this.lazy[node]; |
| 173 | + } |
| 174 | + this.lazy[node] = 0; |
| 175 | + } |
| 176 | + |
| 177 | + if (l <= start && end <= r) { |
| 178 | + return this.tree[node]; |
| 179 | + } |
| 180 | + |
| 181 | + const mid = Math.floor((start + end) / 2); |
| 182 | + const leftSum = this._queryRange(2 * node + 1, start, mid, l, r); |
| 183 | + const rightSum = this._queryRange(2 * node + 2, mid + 1, end, l, r); |
| 184 | + return leftSum + rightSum; |
| 185 | + } |
| 186 | + |
| 187 | + queryRange(l, r) { |
| 188 | + return this._queryRange(0, 0, this.n - 1, l, r); |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +### Historia |
| 194 | + |
| 195 | +Los árboles de segmentos han sido ampliamente usados en programación competitiva y ciencias de la computación para consultas y actualizaciones eficientes en rangos. La propagación perezosa es una técnica que difiere las actualizaciones a nodos hijos, mejorando el rendimiento cuando se involucran múltiples actualizaciones en rangos. |
0 commit comments