Segment Trees
Segment Trees
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
ACA, IIT Kanpur
League of Programmers
Segment Trees
Outline
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Essential Tools
Segment Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Representation
1 Used to represent almost complete binary trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Representation
1 Used to represent almost complete binary trees
2 n elements stored in log n levels
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Representation
1 Used to represent almost complete binary trees
2 n elements stored in log n levels
3 Stored in array
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Representation
1 Used to represent almost complete binary trees
2 n elements stored in log n levels
3 Stored in array
4 Indexed root to leaves, (1,...,n) or (0,...,n-1).
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Representation
1 Used to represent almost complete binary trees
2 n elements stored in log n levels
3 Stored in array
4 Indexed root to leaves, (1,...,n) or (0,...,n-1).
5 Choosing (1,...,n)
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Properties
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Properties
1 Level i starts at index 2i .
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Properties
1 Level i starts at index 2i .
2 Parent of vertex at i is b i c
2
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Properties
1 Level i starts at index 2i .
2 Parent of vertex at i is b i c
3 Children of vertex at i is 2 i and 2 i + 1
2
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Applications
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Applications
1 Binary Heap
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Applications
1 Binary Heap
2 Segment trees
League of Programmers
Segment Trees
Outline
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Essential Tools
Segment Trees
Problems
League of Programmers
Segment Trees
Motivation
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem Statement
We have an array a[0 . . . n-1].
League of Programmers
Segment Trees
Motivation
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem Statement
We have an array a[0 . . . n-1].
League of Programmers
Segment Trees
Motivation
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem Statement
We have an array a[0 . . . n-1].
We should be able to
1 Find the sum of elements l to r
League of Programmers
Segment Trees
Motivation
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem Statement
We have an array a[0 . . . n-1].
We should be able to
1 Find the sum of elements l to r
2 Change in the value of a specied element of the array a[i]=x
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
A Simple Problem
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Segment Trees
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
for(i=(1l)-1;i>0;i - -)
T[i]= sum(T[2*i],T[2*i+1]);
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
for(i=(1l)-1;i>0;i - -)
T[i]= sum(T[2*i],T[2*i+1]);
Time to do this?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
for(i=(1l)-1;i>0;i - -)
T[i]= sum(T[2*i],T[2*i+1]);
Time to do this?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
for(i=(1l)-1;i>0;i - -)
T[i]= sum(T[2*i],T[2*i+1]);
Time to do this?
O(n) since each node in the tree is modied once and uses
only a max of 2 nodes (already computed) for computation.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
If its in one of the child, query on that child
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
The input is two numbers l and r. And we have the time
O (logn). Calculate the sum of the segment a[l . . . r ] .
Can be done recursively
If your range is within the segment completely, return the
value at that node
If its completely out of range, return 0 or null
If its in one of the child, query on that child
If its in both the child, do query on both of them
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query Request(Recursive)
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query Request(Recursive)
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query Request(Recursive)
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
It is.
At any level of the tree, the maximum number of segments that
could call our recursive function when processing a request is 4.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query Request(Recursive)
Pseudocode
query(node,l,r) {
if range of node is within l and r
return value in node
else if range of node is completely outside l and r
return 0
else
return
sum(query(left-child,l,r),query(right-child,l,r))
}
But this doesn't look O(log n)
It is.
At any level of the tree, the maximum number of segments that
could call our recursive function when processing a request is 4.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
Iteratively??
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
Iteratively??
What is stored at LCA of l and r in tree.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
Iteratively??
What is stored at LCA of l and r in tree.
How do we remove excess sum. In log n time.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query
Iteratively??
What is stored at LCA of l and r in tree.
How do we remove excess sum. In log n time.
Prob: if left is not left and right is not right.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Query Request(Iterative)
query(l,r,k(levels)) {
Sum=0; l= (1k)+l; r= (1k)+r;
while(l!=r) {
if(l is not left child)
sum - = T[l];
if(r is not right child)
sum - = T[r];
l/=2,r/=2;}
return sum + T[l];
}
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Renewal Request
Given an index i and the value of x. What to do?
Update the nodes in the tree so as to conform to the new
value a[i]=x in O(log n).
How many nodes and what nodes will be aected?
The nodes from i th leaf node to the way upto the root of the
tree.
Iterative Code
Update(Index i,Change c,k(levels)) {
i= (1k)+i;
while(i!=0) {
T[i]+=c;
i/=2; } }
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
Array of 1's and O's. Find total 1's or 0's in range l to r, Flip
a bit.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
Array of 1's and O's. Find total 1's or 0's in range l to r, Flip
a bit.
Remove a part of array. Or exchange two parts of arrays.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
Array of 1's and O's. Find total 1's or 0's in range l to r, Flip
a bit.
Remove a part of array. Or exchange two parts of arrays.
Add/Subtract x from all values in range l and r
{ Keep extra variable S[i] at each node for lazy update.}
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
Array of 1's and O's. Find total 1's or 0's in range l to r, Flip
a bit.
Remove a part of array. Or exchange two parts of arrays.
Add/Subtract x from all values in range l and r
{ Keep extra variable S[i] at each node for lazy update.}
Again add at LCA remove intermediate.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Variants
Find Maximum/Minimum instead of Sum
Array of 1's and O's. Find total 1's or 0's in range l to r, Flip
a bit.
Remove a part of array. Or exchange two parts of arrays.
Add/Subtract x from all values in range l and r
{ Keep extra variable S[i] at each node for lazy update.}
Again add at LCA remove intermediate.
Similarily for 1's and 0's ip all the bits in range l to r.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Add Range(Iterative)
query(l,r,Change c,k(levels)) {
l= (1k)+l; r= (1k)+r;k1=0;
while(l!=r) {
if(k!=0)
T[l]=sum(T[2*l],T[2*l+1]);
T[r]=sum(T[2*r],T[2*r+1]);
if(l is not left child)
S[i-1]-=c;
T[i-1]-= c*(1k1);
if(r is not right child)
S[i+1]-=c;
T[i+1]-= c*(1k1);
l/=2,r/=2;k1++;}
}
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Implementation
Add Range(Iterative)
if(k!=0) T[l]=sum(T[2*l],T[2*l+1])
S[l]+=c;
T[l]+= c*(1k1);
while(l!=0) T[l]=sum(T[2*l],T[2*l+1]);
League of Programmers
Segment Trees
Outline
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Essential Tools
Segment Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
Weight of vertices or edges.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
Weight of vertices or edges.
Query and Updates on Subtree.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
Weight of vertices or edges.
Query and Updates on Subtree.
Query and Updates on paths.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
Weight of vertices or edges.
Query and Updates on Subtree.
Query and Updates on paths.
We know solution for linear array probs.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Problem
Given a rooted tree with edges E and vertices V .
Weight of vertices or edges.
Query and Updates on Subtree.
Query and Updates on paths.
We know solution for linear array probs.
Reduce these structures to linear structures.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
Edge which hangs less than half of descendants is Light.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
Edge which hangs less than half of descendants is Light.
Example ?
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
Edge which hangs less than half of descendants is Light.
Example ?
Light edges divides the tree into set of heavy paths.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
Edge which hangs less than half of descendants is Light.
Example ?
Light edges divides the tree into set of heavy paths.
Any path in tree can have how many heavy path intervals??
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Structure
Divide the whole edges into Heavy and Light Edges.
Edge which hangs more than half of descendants is Heavy.
Edge which hangs less than half of descendants is Light.
Example ?
Light edges divides the tree into set of heavy paths.
Any path in tree can have how many heavy path intervals??
O(log n)
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Variants
Dynamic Link and Cut.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Variants
Dynamic Link and Cut.
League of Programmers
Segment Trees
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Variants
Dynamic Link and Cut.
ST Trees
League of Programmers
Segment Trees
Outline
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Essential Tools
Segment Trees
Problems
League of Programmers
Segment Trees
Problems
Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems
Links:
1
2
3
4
5
6
7
8
http://www.spoj.pl/problems/GSS1/
http://www.spoj.pl/problems/GSS3/
http://www.spoj.pl/problems/HORRIBLE/
http://www.spoj.pl/problems/BRCKTS/
http://www.spoj.pl/problems/HELPR2D2/
http://www.spoj.pl/problems/KFSTD/
http://www.spoj.pl/problems/FREQUENT/
http://www.spoj.pl/problems/LITE/
League of Programmers
Segment Trees