Skip to content

Commit a5119d9

Browse files
some fixes on segment tree page
1 parent 5000feb commit a5119d9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

docs/data-structures/segment-tree.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Previously, update function was called to update only a single value in array. P
103103
104104
### Lazy Propogation Algorithm
105105
We need a structure that can perform following operations on an array $[1,N]$.
106+
106107
- Add inc to all elements in the given range $[l, r]$.
107108
- Return the sum of all elements in the given range $[l, r]$.
108109
@@ -118,6 +119,7 @@ Trick is to be lazy i.e, do work only when needed. Do the updates only when you
118119
Let’s be <i>lazy</i> as told, when we need to update an interval, we will update a node and mark its children that it needs to be updated and update them when needed. For this we need an array $lazy[]$ of the same size as that of segment tree. Initially all the elements of the $lazy[]$ array will be $0$ representing that there is no pending update. If there is non-zero element $lazy[k]$ then this element needs to update node k in the segment tree before making any query operation, then $lazy[2\cdot k]$ and $lazy[2 \cdot k + 1]$ must be also updated correspondingly.
119120
120121
To update an interval we will keep 3 things in mind.
122+
121123
- If current segment tree node has any pending update, then first add that pending update to current node and push the update to it’s children.
122124
- If the interval represented by current node lies completely in the interval to update, then update the current node and update the $lazy[]$ array for children nodes.
123125
- If the interval represented by current node overlaps with the interval to update, then update the nodes as the earlier update function.
@@ -202,6 +204,7 @@ Notice that the only difference with the regular query function is pushing the l
202204
203205
## Binary Search on Segment Tree
204206
Assume we have an array A that contains elements between 1 and $M$. We have to perform 2 kinds of operations.
207+
205208
- Change the value of the element in given index i by x.
206209
- Return the value of the kth element on the array when sorted.
207210
@@ -240,8 +243,9 @@ This is of course, slow. Let’s use segment tree’s to improve it. First we wi
240243
<figure markdown = "span">
241244
![segment tree updates](img/updated_segtree.png){ width="100%" }
242245
<figcaption>Segment Tree After First Update</figcaption>
246+
</figure>
243247

244-
```c++
248+
```cpp
245249
void update(int i, int x) {
246250
update(1, 1, M, A[i], --F[A[i]]); // Decrement frequency of old value
247251
A[i] = x; // Update A[i] to new value
@@ -263,15 +267,15 @@ int query(int k) {
263267
264268
If you look at the code above you can notice that each update takes $\mathcal{O}(\log M)$ time and each query takes $\mathcal{O}(\log^{2} M)$ time, but we can do better.
265269
266-
### How To Speed Up?
270+
### How To Speed Up?
267271
If you look at the segment tree solution on preceding subsection you can see that queries are performed in $\mathcal{O}(\log^{2} M)$ time. We can make is faster, actually we can reduce the time complexity to $\mathcal{O}(\log M)$ which is same with the time complexity for updates. We will do the binary search when we are traversing the segment tree. We first will start from the root and look at its left child’s sum value, if this value is greater than k, this means our answer is somewhere in the left child’s subtree. Otherwise it is somewhere in the right child’s subtree. We will follow a path using this rule until we reach a leaf, then this will be our answer. Since we just traversed $\mathcal{O}(\log M)$ nodes (one node at each level), time complexity will be $\mathcal{O}(\log M)$. Look at the code below for better understanding.
268272
269273
<figure markdown = "span">
270274
![solution of first query](img/query_soln.png){ width="100%" }
271275
<figcaption>Solution of First Query</figcaption>
272276
</figure>
273277
274-
```c++
278+
```cpp
275279
void update(int i, int x) {
276280
update(1, 1, M, A[i], --F[A[i]]); // Decrement frequency of old value
277281
A[i] = x; // Update A[i] to new value
@@ -289,4 +293,4 @@ int query(int node, int start, int end, int k) {
289293
int query(int k) {
290294
return query(1, 1, M, k); // Public interface for querying
291295
}
292-
```
296+
```

0 commit comments

Comments
 (0)