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

Segment Trees

The document discusses segment trees, which can be used to efficiently calculate sums over subranges of an array. It describes how to build a segment tree by making the leaves of the tree correspond to array elements and internal nodes represent the sum of elements in their subtrees. Segment trees allow calculating sums and updating values in O(log n) time after preprocessing the array in O(n) time to build the tree.

Uploaded by

aditandadit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Segment Trees

The document discusses segment trees, which can be used to efficiently calculate sums over subranges of an array. It describes how to build a segment tree by making the leaves of the tree correspond to array elements and internal nodes represent the sum of elements in their subtrees. Segment trees allow calculating sums and updating values in O(log n) time after preprocessing the array in O(n) time to build the tree.

Uploaded by

aditandadit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Essential Tools

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

Segment trees for Rooted Trees

Problems

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree


Representation

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

Representation
1 Used to represent almost complete binary trees

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

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

Binary Indexed Tree

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

Binary Indexed Tree

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

Binary Indexed Tree

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

Binary Indexed Tree

Properties

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

Properties
1 Level i starts at index 2i .

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

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

Binary Indexed Tree

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

Binary Indexed Tree

Applications

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

Applications
1 Binary Heap

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Binary Indexed Tree

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

Segment trees for Rooted 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

Possible Solutions (Using Simple array)

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

A Simple Problem

Possible Solutions (Using Simple array)


Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

A Simple Problem

Possible Solutions (Using Simple array)


Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

A Simple Problem

Possible Solutions (Using Simple array)


Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

A Simple Problem

Possible Solutions (Using Simple array)


Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates
What if the number of query and updates are equal?

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

A Simple Problem

Possible Solutions (Using Simple array)


Naive one: Go on from l to r and keep on adding and update
the element when you get a update request.
Running time: O(n) to sum and O(1) to update
Store sum from start to i at the i th index in an another array.
Running time: O(1) to return sum, O(n) to update
This works well if the number of query operations are large
and very few updates
What if the number of query and updates are equal?
Can we perform both the operations in O(log n) time once
given the array?
League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents all leaf nodes in its subtree.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents all leaf nodes in its subtree.
For this problem sum of all the leaf values in subtree.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents all leaf nodes in its subtree.
For this problem sum of all the leaf values in subtree.

Size of complete Binary tree with n leaves?

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents all leaf nodes in its subtree.
For this problem sum of all the leaf values in subtree.

Size of complete Binary tree with n leaves?


4n, How?

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Segment Trees

Possible Solutions (Using Binary Indexed Trees)


Representation of the tree
Leaf Nodes are the elements in the array.
Each internal node represents all leaf nodes in its subtree.
For this problem sum of all the leaf values in subtree.

Size of complete Binary tree with n leaves?


4n, How?
Levels log n

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Implementation

Building the tree

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Implementation

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
1

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Implementation

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
1

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Implementation

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
Calculate the nodes of the previous level as the sum of the two
leaves
1

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Implementation

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
Calculate the nodes of the previous level as the sum of the two
leaves
1

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

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
Calculate the nodes of the previous level as the sum of the two
leaves
1

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

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
Calculate the nodes of the previous level as the sum of the two
leaves
1

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

Building the tree


Size of tree 2l + where { for(l=0;(1l)<n;l++); }
From the bottom up: rst write the values of the elements a[i]
the corresponding leaves T[(1l)+i]
Calculate the nodes of the previous level as the sum of the two
leaves
1

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

Segment trees for Rooted Trees

Problems

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Case of Rooted trees

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

Case of Rooted trees

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

Case of Rooted trees

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

Case of Rooted trees

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

Case of Rooted trees

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

Case of Rooted trees

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

Case of Rooted trees

Query and Updates on Subtree.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Case of Rooted trees

Query and Updates on Subtree.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Case of Rooted trees

Query and Updates on Subtree.


Pre Order Numbering
Query and Updates on Path.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Case of Rooted trees

Query and Updates on Subtree.


Pre Order Numbering
Query and Updates on Path.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Case of Rooted trees

Query and Updates on Subtree.


Pre Order Numbering
Query and Updates on Path.
Heavy Light Decomposition

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

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

Heavy Light Decomposition

Variants
Dynamic Link and Cut.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Heavy Light Decomposition

Variants
Dynamic Link and Cut.

League of Programmers

Segment Trees

Essential Tools
Segment Trees
Segment trees for Rooted Trees
Problems

Heavy Light Decomposition

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

Segment trees for Rooted 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

You might also like