0% found this document useful (0 votes)
26 views

Segment Trees

1. Segment trees allow performing range queries and updates in O(logn) time by storing partial sums or other values in the nodes of a tree structure. 2. To build a segment tree from an input array of size n, the minimum size of the segment tree array needed is 2n to store the values at each node. 3. Range queries on a segment tree can be performed in O(logn) time by recursively querying the appropriate child nodes until the query range is fully inside or outside the subtree.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Segment Trees

1. Segment trees allow performing range queries and updates in O(logn) time by storing partial sums or other values in the nodes of a tree structure. 2. To build a segment tree from an input array of size n, the minimum size of the segment tree array needed is 2n to store the values at each node. 3. Range queries on a segment tree can be performed in O(logn) time by recursively querying the appropriate child nodes until the query range is fully inside or outside the subtree.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Session 1

Tuesday, 4 October 2022 6:44 AM

Why do we need segment trees?


Range query logn
Update query logn

Range queries of sum


Prefix sum = O(n) o(1)

Update queries
Naïve method update in O(1)
Range query in O(n)

Data structure
Build
Range queries(l,r)
Update query(index, value)

Sum of a subarray and update in logn

U452
U453
(0,7)
1
U452
U453
(0,7)
0

(4,7)
2
1 (0,3)

6 (6,7
5 (4,5)
(2,3)
3 4
(0,1)

8 9
7

0 1

If input array size is n, what should be size of segment tree arra

8 + 4 +2 +1 = 15
N+n/2+n/4 ….. 1
N(1+1/2+1/4 …….)
N*(2)
2n is bare minimum we need
1

7) 4

ay?
N+n/2+n/4 ….. 1
N(1+1/2+1/4 …….)
N*(2)
2n is bare minimum we need

N not a power of 2
Convert it to next power of 2
8 9 16
15 16
In worst case we need to
double the size
2n *2 = 4n

N as input array what will be the height?


O(logn)

Index of a node in segment tree which is I


Children of ith index node?
2*i+1,2*i+2 when indexing is zero based
2*I, 2*i+1 in one based indexing.

We know some node's index which is i. parent's index?


(i-1)/2 zero based
i/2 one based

A = input array
Build(a,0,n-1,1,tree)

Build(a, l,r,I,tree){
If(l == r){
Tree[i] = a[l];
Build(a, l,r,I,tree){
If(l == r){
Tree[i] = a[l];
}
Int mid = l + (r-l)/2;
Build(a,l,mid,2*I,tree);
Build(a,mid+1,2*i+1,tree);
Tree[i] = Tree[2*i]+tree[2*i+1];
}

Update(tree, index, value, 1,0,n-1)

Update(tree,index,value,treeindex,l,r){
If(l == r && l == index){
Tree[treeindex] = value;
}
Else{
Int mid = l + (r-l)/2;
If(index <= mid){
Update(tree, index, value, treeindex*2, l, mid)
}
Else{
Update(tree, index, value, treeindex*2+1, mid+1, r)
}
Tree[treeindex] = tree[treeindex*2]+tree[treeindex*2+1];
}

Range(l,r,tree, 1, 0,n-1)
Range(l,r,tree, 1, 0,n-1)

Rangequery(l,r,tree,treeindex,tl,tr){
If(tl >= l && tr <= r) return tree[treeindex];
If(tl < l || tr > r) return 0;
Int mid = l + (r-l)/2;
Return Rangequery(l,r,tree, 2*treeindex, tl, mid) + rangequery(l,r
2*treeindex+1, mid+1,tr);
}

Completely inside = add to ans


Completely outside = add zero or just skip that call
Partially inside partilally outside = go to child nodes till we reach to
cases above

Space comlextiy = O(n)

New question on segment tree?


2 important things
What to store in our node?
How to merge two children nodes to make a parent node?
r,tree,

o
How to merge two children nodes to make a parent node?

Parent node
1 2 -3 1 4 -5 2 1
Max sum subarry = max(lcmss,rcmss,lcmsufs+rcmps)
Max prefix sum = 5 max(lcps,rcmps+lcs)
Max suffix sum = 3 max(lcmsuffs+rcs,rcss)
Sum = lcs+rcs

1 2 -3 1 4 -5 2 1
Max sum subarry = 3 Max sum subarry = 3
Max prefix sum = 3 Max prefix sum = 4
Max suffix sum = 1 Max suffix sum = 3
Sum = 1 Sum = 2

Point updates and range


queries

Range updates and range


queries
(R - l)*logn bad
approach
Input array = [.,.,.,.,…]
Queries
Range sum l r
Range updates l r x

You might also like